By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,918 Members | 2,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,918 IT Pros & Developers. It's quick & easy.

A problem on string swap

P: n/a
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, char *char2)
{

char *temp;
temp=char1;
char1=char2;
char2=temp;
}

void main()
{

char*char1="sidd";
char* char2="pras";
cout<<"string 1=" <<char1<<endl;
cout<<"string 2=" <<char2<<endl;

swap( char1, char2);

cout<<"string 1=" <<char1<<endl;
cout<<"string 2=" <<char2<<endl;

}

thanks in advance.

Dec 26 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
On 25 Dec 2005 22:27:20 -0800 in comp.lang.c++, "meendar"
<as****************@gmail.com> wrote,
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, char *char2)
{

char *temp;
temp=char1;
char1=char2;
char2=temp;
}


char1 and char2 are local variables in the function swap().
Altering their values does not change anything outside that
function.

Dec 26 '05 #2

P: n/a
thanks for your help.
But we are using pointer to denote these two variables.why doesn't it
work.

Dec 26 '05 #3

P: n/a
meendar wrote:
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, char *char2)
{

char *temp;
temp=char1;
char1=char2;
char2=temp;
}

void main()
{

char*char1="sidd";
char* char2="pras";
cout<<"string 1=" <<char1<<endl;
cout<<"string 2=" <<char2<<endl;

swap( char1, char2);

cout<<"string 1=" <<char1<<endl;
cout<<"string 2=" <<char2<<endl;

}

thanks in advance.


C++ has strings as part of the standard library. Use those instead of
icky character arrays. Your code looks more like C than C++. Also, main
should return an integer, not void.

#include <iostream>
#include <string>
#include <cstdlib>

int main()
{
// Creating strings
std::string first = "sidd";
std::string second = "pras";

// Displaying strings
std::cout << first << std::endl;
std::cout << second << std::endl;

// Swapping strings
first.swap(second);

// Displaying strings
std::cout << first << std::endl;
std::cout << second << std::endl;

return EXIT_SUCCESS;
}
Dec 26 '05 #4

P: n/a
On 25 Dec 2005 22:33:04 -0800 in comp.lang.c++, "meendar"
<as****************@gmail.com> wrote,
thanks for your help.
But we are using pointer to denote these two variables.why doesn't it
work.


Reason stated. Your pointer variables are local to the function.

Dec 26 '05 #5

P: n/a
On Mon, 26 Dec 2005 06:35:59 +0000, W Marsh
<wayneDOTmarshATgmailDOTcom@decipher> wrote:
meendar wrote:
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?
(...)


C++ has strings as part of the standard library. Use those instead of
icky character arrays. Your code looks more like C than C++. Also, main
should return an integer, not void.

(...)

And, if you persist on using character pointers, do write the program
as folllows:
#include<iostream>
void swap(const char *& char1, const char *& char2)
{

const char *temp;
temp=char1;
char1=char2;
char2=temp;
}

void main()
{
using std::cout;

const char*char1="sidd";
const char* char2="pras";
cout<<"string 1=" <<char1<<endl;
cout<<"string 2=" <<char2<<endl;

swap( char1, char2);

cout<<"string 1=" <<char1<<endl;
cout<<"string 2=" <<char2<<endl;

}

But, of course, it is much better to use std::string, as stated W
Marsh

Regards,

-- Zara
Dec 26 '05 #6

P: n/a
David Harmon wrote:
On 25 Dec 2005 22:27:20 -0800 in comp.lang.c++, "meendar"
<as****************@gmail.com> wrote,
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, char *char2)
{

char *temp;
temp=char1;
char1=char2;
char2=temp;
}

char1 and char2 are local variables in the function swap().
Altering their values does not change anything outside that
function.


Since the pointers are local in scope to the swap function, upon
returning from swap your variables are unchanged. Try this:

void swap(char ** char1, char ** char2)
{
char * temp = *char1;
*char1 = *char2;
*char2 = temp;
}
Dec 26 '05 #7

P: n/a

"Zara" <yo****@terra.es> wrote in message
news:n4********************************@4ax.com...
On Mon, 26 Dec 2005 06:35:59 +0000, W Marsh
<wayneDOTmarshATgmailDOTcom@decipher> wrote:
meendar wrote:
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?
(...)
C++ has strings as part of the standard library. Use those instead of
icky character arrays. Your code looks more like C than C++. Also, main
should return an integer, not void.

(...)

And, if you persist on using character pointers, do write the program
as folllows:
#include<iostream>
void swap(const char *& char1, const char *& char2)


Will this const work, since the variables are actually being changed?
Doesn't he want them non constant?
{

const char *temp;
temp=char1;
char1=char2;
char2=temp;
}

void main()
{
using std::cout;

const char*char1="sidd";
const char* char2="pras";
cout<<"string 1=" <<char1<<endl;
cout<<"string 2=" <<char2<<endl;

swap( char1, char2);

cout<<"string 1=" <<char1<<endl;
cout<<"string 2=" <<char2<<endl;

}

But, of course, it is much better to use std::string, as stated W
Marsh

Regards,

-- Zara

Dec 26 '05 #8

P: n/a
On Mon, 26 Dec 2005 00:41:10 -0800, "Jim Langston"
<ta*******@rocketmail.com> wrote:

"Zara" <yo****@terra.es> wrote in message
news:n4********************************@4ax.com.. .
On Mon, 26 Dec 2005 06:35:59 +0000, W Marsh
<wayneDOTmarshATgmailDOTcom@decipher> wrote:
meendar wrote:
(...)

void swap(const char *& char1, const char *& char2)


Will this const work, since the variables are actually being changed?
Doesn't he want them non constant?
{

const char *temp;
temp=char1;
char1=char2;
char2=temp;
}

(...)
Yes, this function will work, because the chars are constant, not the
pointers. The chars are really constant, as they are assigned as
char * p="whatever";

I have added the const qualifying to be strict.

But. although chars pointed are constant, char pointers are variable
and may be swapped.
Best regards,

-- Zara
Dec 26 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.