swap char array.

N

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
 
R

red floyd

NK said:
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-lite/how-to-post.html#faq-5.2
 
R

Rahul

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...
 
T

tragomaskhalos

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[]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top