Please help: swap char problem in c++

D

dbuser

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 it works, but when
it has odd no of characters, then last characters is not written ,
because it does not have i+1 element to swap. Here is what I am doing:
Please suggest the correct way of doing this for any number of
character.
void swap( char * w) //array is read from a file and passed here.
{
int i,len = strlen(w);
for ( i=0; i<len; i=i+2) {
swapchar( &w, &w[i+1]);
}

cout << "swapped :" << w << endl;
}
void swapchar( char * c1, char * c2)
{
char temp=*c1; *c1=*c2; *c2=temp;
}

Thanks very much for help !
 
Z

Zara

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 it works, but when
it has odd no of characters, then last characters is not written ,
because it does not have i+1 element to swap. Here is what I am doing:
Please suggest the correct way of doing this for any number of
character.
void swap( char * w) //array is read from a file and passed here.
{
int i,len = strlen(w);
for ( i=0; i<len; i=i+2) {
swapchar( &w, &w[i+1]);
}

cout << "swapped :" << w << endl;
}
void swapchar( char * c1, char * c2)
{
char temp=*c1; *c1=*c2; *c2=temp;
}

Thanks very much for help !



No, it is not that last char is not written but that last char is
swapped with teminating '\0', thus cutting the string.

Use, for instance, the following loop instruction instead of yours
for ( i=0; i<len-1; i=i+2) {

By testing this way, last char will not be swaped when length is odd
 
J

Jacques Labuschagne

dbuser said:
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 it works, but when
it has odd no of characters, then last characters is not written ,
because it does not have i+1 element to swap. Here is what I am doing:
Please suggest the correct way of doing this for any number of
character.
void swap( char * w) //array is read from a file and passed here.
{
int i,len = strlen(w);
for ( i=0; i<len; i=i+2) {
swapchar( &w, &w[i+1]);
}


The problem is that on the last iteration you're swapping the last
normal character with the terminating '\0', in effect chopping one off
the end of the string. If you'd used std::string this wouldn't have
happened. :)

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

void swap_adjacent(string& s){
for (int i = 0; i < s.size(); i+=2){
swap(s, s[i+1]);
}
}

int main(){
string s;
cin >> s;
swap_adjacent(s);
cout << s << endl;
}
 
J

Jacques Labuschagne

Jacques said:
The problem is that on the last iteration you're swapping the last
normal character with the terminating '\0', in effect chopping one off
the end of the string. If you'd used std::string this wouldn't have
happened. :)
void swap_adjacent(string& s){
for (int i = 0; i < s.size(); i+=2){
swap(s, s[i+1]);
}
}


Oops, mine's no better. The problem is not that you didn't use
std::string, the problem is that your test should be "i < s.size() - 1"
rather than "i < s.size()".

Hope that helps,
Jacques.
 
D

dbuser

THANKS a lot to both of you... looping thru len-1 is working. However,
this causes a runtime exception on borland or intel compiler on
windows. I can safely run this code under cygwin on windows using g++.
For now, I am happy, but I need to debug my run time crash of the
entire program.
Thanks again !
 
D

Default User

dbuser said:
THANKS a lot to both of you... looping thru len-1 is working. However,
this causes a runtime exception on borland or intel compiler on
windows. I can safely run this code under cygwin on windows using g++.
For now, I am happy, but I need to debug my run time crash of the
entire program.

Show a complete, minimal program that demonstrates the problem. Also
read the .sig below.



Brian
 

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

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,046
Latest member
Gavizuho

Latest Threads

Top