Connecting Tech Pros Worldwide Forums | Help | Site Map

Please help: swap char problem in c++

dbuser
Guest
 
Posts: n/a
#1: Oct 11 '05
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 !


Jacques Labuschagne
Guest
 
Posts: n/a
#2: Oct 11 '05

re: Please help: swap char problem in c++


dbuser wrote:[color=blue]
> 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]);
> }[/color]

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;
}
Zara
Guest
 
Posts: n/a
#3: Oct 11 '05

re: Please help: swap char problem in c++


On 11 Oct 2005 00:29:06 -0700, "dbuser" <ranjanr@gmail.com> wrote:
[color=blue]
>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 ![/color]


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


Jacques Labuschagne
Guest
 
Posts: n/a
#4: Oct 11 '05

re: Please help: swap char problem in c++


Jacques Labuschagne wrote:[color=blue]
> 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. :-)
>[/color]
<snip>[color=blue]
> void swap_adjacent(string& s){
> for (int i = 0; i < s.size(); i+=2){
> swap(s[i], s[i+1]);
> }
> }[/color]

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.
dbuser
Guest
 
Posts: n/a
#5: Oct 11 '05

re: Please help: swap char problem in c++


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 !

Default User
Guest
 
Posts: n/a
#6: Oct 11 '05

re: Please help: swap char problem in c++


dbuser wrote:
[color=blue]
> 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.[/color]

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.
Closed Thread