469,646 Members | 1,180 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,646 developers. It's quick & easy.

Please help: swap char problem in c++

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[i], &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 !

Oct 11 '05 #1
5 1848
dbuser wrote:
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[i], &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[i], s[i+1]);
}
}

int main(){
string s;
cin >> s;
swap_adjacent(s);
cout << s << endl;
}
Oct 11 '05 #2
On 11 Oct 2005 00:29:06 -0700, "dbuser" <ra*****@gmail.com> wrote:
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[i], &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
Oct 11 '05 #3
Jacques Labuschagne wrote:
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. :-)
<snip> void swap_adjacent(string& s){
for (int i = 0; i < s.size(); i+=2){
swap(s[i], 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.
Oct 11 '05 #4
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 !

Oct 11 '05 #5
dbuser wrote:
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

--
Please quote enough of the previous message for context. To do so from
Google, click "show options" and use the Reply shown in the expanded
header.
Oct 11 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by kingyof2thejring | last post: by
21 posts views Thread by siya | last post: by
20 posts views Thread by paolo.arimado | last post: by
7 posts views Thread by zoro | last post: by
30 posts views Thread by Alf P. Steinbach | last post: by
3 posts views Thread by NK | last post: by
11 posts views Thread by Dennis Jones | last post: by
4 posts views Thread by rola248 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.