472,995 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,995 software developers and data experts.

swap char array.

NK
char *a="nnnnnnn fgjfjgjf";
char *b="ABC jjjjjjjjjhhhh";

to swap these two string......
this swap fn is working

template<class T>
void Swap( const T *&a,const T *&b)//no need to write const
{
T *temp;
temp=a;

a=b;

b=temp;
}

but to swap
char a[]="nhfhdhfhh fgjfjgjf";
char b[]="ABC jjjjjjjjjhhhh";

these char array its not.
why?
how a[] and b[] can be swapped?
give effficient soln.
don't use swap fn of string library(string1.swap(string2))

Thanx in advance
Dec 26 '07 #1
3 12321
NK wrote:
char *a="nnnnnnn fgjfjgjf";
char *b="ABC jjjjjjjjjhhhh";

to swap these two string......
this swap fn is working

template<class T>
void Swap( const T *&a,const T *&b)//no need to write const
{
T *temp;
temp=a;

a=b;

b=temp;
}

but to swap
char a[]="nhfhdhfhh fgjfjgjf";
char b[]="ABC jjjjjjjjjhhhh";

these char array its not.
why?
how a[] and b[] can be swapped?
give effficient soln.
don't use swap fn of string library(string1.swap(string2))

Here you go:

http://www.parashift.com/c++-faq-lit...t.html#faq-5.2
Dec 26 '07 #2
On Dec 26, 11:33 pm, NK <nagendra...@gmail.comwrote:
char *a="nnnnnnn fgjfjgjf";
char *b="ABC jjjjjjjjjhhhh";

to swap these two string......
this swap fn is working

template<class T>
void Swap( const T *&a,const T *&b)//no need to write const
{
T *temp;
temp=a;

a=b;

b=temp;

}

but to swap

char a[]="nhfhdhfhh fgjfjgjf";
char b[]="ABC jjjjjjjjjhhhh";

these char array its not.
why?
how a[] and b[] can be swapped?
give effficient soln.
don't use swap fn of string library(string1.swap(string2))

Thanx in advance
swap function could also check if both the reference are pointing to
same variable or not...
Dec 27 '07 #3
On 26 Dec, 18:33, NK <nagendra...@gmail.comwrote:
char *a="nnnnnnn fgjfjgjf";
* char *b="ABC jjjjjjjjjhhhh";

to swap these two string......
*this swap fn is working

template<class T>
void Swap( const T *&a,const *T **&b)//no need to write const
{
* * *T *temp;
* * *temp=a;

* * *a=b;

* * *b=temp;

}

but to swap

char a[]="nhfhdhfhh fgjfjgjf";
* * * *char b[]="ABC jjjjjjjjjhhhh";

these char array its not.
why?
how a[] and b[] can be swapped?
give effficient soln.
don't use swap fn of string library(string1.swap(string2))

Thanx in advance
Obviously homework, but it's Christmas ...

You can't do this directly - arrays are not assignable.
You could do a character-by-character swap, but then
what if the arrays are of different lengths?

But all of this is academic, you can achieve what you
need like this:

char a[]="nhfhdhfhh fgjfjgjf";
char b[]="ABC jjjjjjjjjhhhh";

char* ap = a;
char* bp = b;

// use ap for a, bp for b

std::swap(ap, bp);

// ap now points to b[], bp to a[]
Dec 27 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: JKop | last post by:
union SignedChoice{ long with_sign; unsigned long without_sign; }; int main() { SignedChoice data;
5
by: dbuser | last post by:
Hi, I have to implement every even and odd character swapping of a stream. My problem is with the following code is that if the stream has total even no of characters in it(including spaces) then...
12
by: Eugen J. Sobchenko | last post by:
Hi! I'm writing function which swaps two arbitrary elements of double-linked list. References to the next element of list must be unique or NULL (even during swap procedure), the same condition...
8
by: meendar | last post by:
This is my program on swap of strings.i just used pointer for swapping the string. can anyone help me why it is not working? #include<iostream.h> #include<string.h> void swap(char *char1,...
9
by: ma740988 | last post by:
Consider: # include <vector> # include <iostream> # include <cstdlib> # include <ctime> bool ispow2i ( double n ) {
5
by: Bo Berglund | last post by:
Preamble: I am a Delphi (Pascal) programmer who has stumbled into the C++ world by necessity since Borland has no 64 bit compiler yet out.... I used to do a lot of ANSI C programming for embedded...
8
by: silversnake | last post by:
hi all, I'm trying to swap two array location within a 2d array using the swap function. I know that how to swap with in a 1d array eg " swap(array, inner, inner+1); " but how do I use swap within a...
11
by: Dennis Jones | last post by:
Hi all, 1) Let's say you have two char 's of the same size. How would you write a no-fail swap method for them? For example: class Test { char s; void swap( Test &rhs ) {
4
by: Robert LaMarca | last post by:
Hi, I am using numpy and wish to create very large arrays. My system is AMD 64 x 2 Ubuntu 8.04. Ubuntu should be 64 bit. I have 3gb RAM and a 15 GB swap drive. The command I have been trying...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.