
December 26th, 2005, 06:35 AM
| | | A problem on string swap
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. | 
December 26th, 2005, 06:45 AM
| | | Re: A problem on string swap
On 25 Dec 2005 22:27:20 -0800 in comp.lang.c++, "meendar"
<askjavaprogrammers@gmail.com> wrote,[color=blue]
>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;
> }[/color]
char1 and char2 are local variables in the function swap().
Altering their values does not change anything outside that
function. | 
December 26th, 2005, 06:45 AM
| | | Re: A problem on string swap
thanks for your help.
But we are using pointer to denote these two variables.why doesn't it
work. | 
December 26th, 2005, 06:45 AM
| | | Re: A problem on string swap
meendar wrote:[color=blue]
> 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.
>[/color]
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;
} | 
December 26th, 2005, 07:35 AM
| | | Re: A problem on string swap
On 25 Dec 2005 22:33:04 -0800 in comp.lang.c++, "meendar"
<askjavaprogrammers@gmail.com> wrote,[color=blue]
>thanks for your help.
>But we are using pointer to denote these two variables.why doesn't it
>work.[/color]
Reason stated. Your pointer variables are local to the function. | 
December 26th, 2005, 08:05 AM
| | | Re: A problem on string swap
On Mon, 26 Dec 2005 06:35:59 +0000, W Marsh
<wayneDOTmarshATgmailDOTcom@decipher> wrote:
[color=blue]
>meendar wrote:[color=green]
>> 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?
>>[/color][/color]
(...)[color=blue][color=green]
>>[/color]
>
>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.
>[/color]
(...)
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 | 
December 26th, 2005, 08:15 AM
| | | Re: A problem on string swap
David Harmon wrote:[color=blue]
> On 25 Dec 2005 22:27:20 -0800 in comp.lang.c++, "meendar"
> <askjavaprogrammers@gmail.com> wrote,
>[color=green]
>>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;
>> }[/color]
>
>
> char1 and char2 are local variables in the function swap().
> Altering their values does not change anything outside that
> function.
>[/color]
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;
} | 
December 26th, 2005, 08:55 AM
| | | Re: A problem on string swap
"Zara" <yozara@terra.es> wrote in message
news:n48vq1tot8cnc7facia8sj631pqerj3c84@4ax.com...[color=blue]
> On Mon, 26 Dec 2005 06:35:59 +0000, W Marsh
> <wayneDOTmarshATgmailDOTcom@decipher> wrote:
>[color=green]
>>meendar wrote:[color=darkred]
>>> 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?
>>>[/color][/color]
> (...)[color=green][color=darkred]
>>>[/color]
>>
>>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.
>>[/color]
> (...)
>
> And, if you persist on using character pointers, do write the program
> as folllows:
>
>
> #include<iostream>
>
>
> void swap(const char *& char1, const char *& char2)[/color]
Will this const work, since the variables are actually being changed?
Doesn't he want them non constant?
[color=blue]
> {
>
> 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[/color] | 
December 26th, 2005, 04:15 PM
| | | Re: A problem on string swap
On Mon, 26 Dec 2005 00:41:10 -0800, "Jim Langston"
<tazmaster@rocketmail.com> wrote:
[color=blue]
>
>"Zara" <yozara@terra.es> wrote in message
>news:n48vq1tot8cnc7facia8sj631pqerj3c84@4ax.com.. .[color=green]
>> On Mon, 26 Dec 2005 06:35:59 +0000, W Marsh
>> <wayneDOTmarshATgmailDOTcom@decipher> wrote:
>>[color=darkred]
>>>meendar wrote:[/color][/color][/color]
(...)[color=blue][color=green]
>>
>>
>> void swap(const char *& char1, const char *& char2)[/color]
>
>Will this const work, since the variables are actually being changed?
>Doesn't he want them non constant?
>[color=green]
>> {
>>
>> const char *temp;
>> temp=char1;
>> char1=char2;
>> char2=temp;
>> }
>>[/color][/color]
(...)
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 |
Posting Rules
| You may not post new threads You may not post replies You may not post attachments You may not edit your posts HTML code is Off | | | | | | What is Bytes?
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over network members.
|