Please help: swap char problem in c++ 
October 11th, 2005, 07:35 AM
| | | 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 ! | 
October 11th, 2005, 07:55 AM
| | | 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;
} | 
October 11th, 2005, 07:55 AM
| | | 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 | 
October 11th, 2005, 07:55 AM
| | | 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. | 
October 11th, 2005, 08:25 AM
| | | 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 ! | 
October 11th, 2005, 04:25 PM
| | | 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. | | Thread Tools | Search this Thread | | | |
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 220,840 network members.
|