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 ! 5 1983
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;
}
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
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.
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 !
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: OZ |
last post by:
Hi,
I am new C++ and need a little help with a public domain program that
is suppose to perform a byte swap. I am receiving the following error
messages during the compile process with Microsoft...
|
by: kingyof2thejring |
last post by:
Hi there ,
I am facing problem i need to represent an array of cards, to add Knuth’s
shuffle routine to it.
Thanks in advance if someone could help.
#include <stdio.h>
int main()
{
int...
|
by: siya |
last post by:
I'm new to c and i've a problem with arrays. I want to sum two arrays
into a third array like this:
array a: 5 6 8 9 1 2 4 7 3 8
array b: 5 2 1 2 4 6 8 5 4 3
sum: 1 0 9 0 1 5 9 3 2 8 1
I...
|
by: paolo.arimado |
last post by:
Dear everyone,
Can someone please help me on my HW. I'm trying to write a program
that will display the following:
*
***
*****
*******
*********
|
by: zoro |
last post by:
how many recursive calls will quicksort make in the worst case for a file
of N items?
thank you
|
by: Alf P. Steinbach |
last post by:
I once suggested in that SomeOne Else(TM) should propose
a string value class that accepted literals and char pointers and so on,
with possible custom deleter, and in case of literal strings just...
|
by: NK |
last post by:
char *a="nnnnnnn fgjfjgjf";
char *b="ABC jjjjjjjjjhhhh";
to swap these two string......
this swap fn is working
template<class T>
void Swap( const T *&a,const T *&b)//no need to write...
|
by: Dennis Jones |
last post by:
Hi all,
1) Let's say you have two char 's of the same size. How would you write a
no-fail swap method for them? For example:
class Test
{
char s;
void swap( Test &rhs )
{
|
by: rola248 |
last post by:
i need to write a c program that reads information from a txt file into two parallel multidiminsional arrays,the problem is that the data is organised like:
Zeinab
100 90 80
Ahmad
70 75 45
Manal...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |