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. 8 7985
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.
thanks for your help.
But we are using pointer to denote these two variables.why doesn't it
work.
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;
}
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.
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
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;
}
"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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Karl Ebener |
last post by:
Hi!
I asked a similar question before but then changed everything to using
char-Arrays instead of the string class, but I would rather not do this
again.
So, does anyone know of a...
|
by: Dev |
last post by:
Hello,
In the following class definition,
the ZString destructor is invoked two times.
This crashes the code.
class ZString
{
public:
ZString(char* p)
|
by: Rakesh |
last post by:
Hi,
I have this function to reverse the given string.
I am just curious if that is correct and there could be better way of
doing it / probable bugs in the same.
The function prototype is...
|
by: anonymous |
last post by:
Hi CLCers,
I tried the following code for swapping a string, but it is
not working. Inside the swap function the strings are
printed correctly, but when back in main() the strings are
not swapped...
|
by: brian_harris |
last post by:
I have a function that passes a string class pointer to a function, this
function is then suppose to fill it and the outer function uses it. But I
believe that I am running into problem due to...
|
by: Albert |
last post by:
Why doesn't:
#include <stdio.h>
void reverse(char, int);
main()
{
char s;
|
by: sudharsan |
last post by:
could any one please give me a code to reverse a string of more than
1MB .???
Thanks in advance
|
by: Dan Holmes |
last post by:
I have a need to reverse a date to show as a "date code". For example
today, 090507 would be coded as 905070 (reversing the parts of the date.
This is what i have but there has to be a better...
|
by: dominantubergeek |
last post by:
Hello, I'm a highly experienced expert C programmer and I've written
this code to reverse a string in place. I think you could all learn
something from it!
int reverse(char* reverseme){
int...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |