#include <iostream>
#include <stdlib.h>
#include <string.h>
char * reversestring(char*);
int main(int argc, char *argv[])
{
char *sentence = new char[255];
char *newsentence = new char[255];
char *wrd;
char *token = " ";
cout << "Enter a sentence: ";
cin.get(sentence,255);
sentence = reversestring(sentence);
cout << "Reversed Sentence: " << sentence << "\n";
wrd = strtok(sentence,token);
wrd = reversestring(wrd);
strcat(newsentence,wrd); //First word
while((wrd = strtok(NULL,token)))
{
wrd = reversestring(wrd);
strcat(newsentence," "); //Need a space
strcat(newsentence,wrd); //Add the word!
}
cout << "New Sentence: " << newsentence << "\n";
system("PAUSE");
delete newsentence;
delete sentence;
}
//Reverse a given string character by character
char * reversestring(char *strn)
{
char *mystr = new char[strlen(strn)];
char *cur;
int idx = strlen(strn) - 1;
int id2 = 0;
do
{
cur = &strn[idx];
mystr[id2++] = cur[0];
idx--;
}while(idx > -1);
return mystr;
}
Strange String Manipulation problem
Ok.. well I am just trying to assist a friend with a program that reverses a sentence. I wrote the program and my test data was to enter ''Terry is a moron'' , this string returns
moron a is noromTerry
So.. I thought my program wasn''t behaving quite correctly.. so I tried with several sets of data including
Timmy is a moron, result:
moron a is noromTimmy
Bob is a moron, result:
moron a is Bob
I can''t work out why the strange behaviour, several other sentences worked fine.. why when 5 letters are used for the first word does it add the reversed moron on the end?
for(int i=strlen(reversetring)-1,int a=0;i>=0,int i=strlen(reversetring);i--,a++){newstring[a]=reversestring[i];}
try something like this
[edited by - Basiror on September 30, 2002 11:35:42 AM]
[edited by - Basiror on September 30, 2002 11:36:21 AM]
That would reverse the characters in a sentence.
hammerstein_02 is trying to reverse the word order I think.
hammerstein_02 is trying to reverse the word order I think.
First thought: that code doesn''t work. Period. It stops functioning after pressing enter on the cin.get()
Other thoughts:
Your not deleting your strings properly:
delete sentence;
should be
delete [] sentence;
for example.
This may not be your problem, but I do notice that in your reversestring function, you are returning the address of a locally allocated pointer which is likely destroyed at the end of the function. That and the fact that the string that it returns a pointer to is NOT null terminated.
I would do something like this:
So that the reversed string is returned through the out pointer which points do an array that already exists in the main function (newsentence for example)
Other thoughts:
Your not deleting your strings properly:
delete sentence;
should be
delete [] sentence;
for example.
This may not be your problem, but I do notice that in your reversestring function, you are returning the address of a locally allocated pointer which is likely destroyed at the end of the function. That and the fact that the string that it returns a pointer to is NOT null terminated.
I would do something like this:
void reversestring(char *strn, char* out){ int count=strlen(strn) - 1; for (int i=count; i>=0; i--) { out[i] = strn[count-i]; } out[count + 1] = ''\0''; return;}
So that the reversed string is returned through the out pointer which points do an array that already exists in the main function (newsentence for example)
Hi
your problem lies in that your reversestring function you make the string one character too short and dont initialize it to 0. This has the effect that there is no ''\0'' quitting the string.
just change your code to this:
although i think your loop is still overly complicated
this would look nicer:
Runicsoft -- home of my open source Function Parser and more
your problem lies in that your reversestring function you make the string one character too short and dont initialize it to 0. This has the effect that there is no ''\0'' quitting the string.
just change your code to this:
char * reversestring(char *strn){ char *mystr = new char[strlen(strn)+1]; memset ( mystr, 0, strlen(strn)+1); char *cur; int idx = strlen(strn)-1; int id2 = 0; do { cur = &strn[idx]; mystr[id2++] = cur[0]; idx--; }while(idx > -1); return mystr;}
although i think your loop is still overly complicated
this would look nicer:
char * reversestring(char *strn){ int len = strlen(strn); char *mystr = new char[len+1]; memset ( mystr, 0, len+1); for ( int i = len-1; i >= 0; i-- ) mystr[len-i-1] = strn[i]; return mystr;}
Runicsoft -- home of my open source Function Parser and more
Thank-you for the reply man.. yeah that was it. Put the code together really without much thought. (Am at work and occupying myself with this stuff!)
I don't do much string manipulation and overall am still a learner at this stage. Thanks for the lessons tho, they are greatly appreciated.
and the code did work before, just where I had five characters as my first word it misbehaved! I wrote it in DevC++ today and it compiled and worked fine the copy I pasted.
[edited by - hammerstein_02 on September 30, 2002 11:55:30 AM]
[edited by - hammerstein_02 on September 30, 2002 11:57:30 AM]
I don't do much string manipulation and overall am still a learner at this stage. Thanks for the lessons tho, they are greatly appreciated.
and the code did work before, just where I had five characters as my first word it misbehaved! I wrote it in DevC++ today and it compiled and worked fine the copy I pasted.
[edited by - hammerstein_02 on September 30, 2002 11:55:30 AM]
[edited by - hammerstein_02 on September 30, 2002 11:57:30 AM]
quote:Original post by -Thork-
This may not be your problem, but I do notice that in your reversestring function, you are returning the address of a locally allocated pointer which is likely destroyed at the end of the function.
well its the purpose of the new operator to allocate memory that does NOT get destroyed when leaving the scope, but only when using the delete operator on it, so this is no problem, although not the best coding practice since you can easily forget to delete [] a string that was new''d in another function.
Runicsoft -- home of my open source Function Parser and more
quote:Original post by Burning_Ice
your problem lies in that your reversestring function you make the string one character too short and dont initialize it to 0. This has the effect that there is no ''\0'' quitting the string.
Unless that''s a new behavior that I have never heard of but Zeroing out a string won''t necessarily put a ''\0'' at the end of the string. If you want a ''\0'' you have to put it yourself.
[Cyberdrek | the last true sorcerer | Spirit Mage - mutedfaith.com]
quote:Original post by Cyberdrek
Unless that''s a new behavior that I have never heard of but Zeroing out a string won''t necessarily put a ''\0'' at the end of the string. If you want a ''\0'' you have to put it yourself.
well if i have e.g. a char[10] string, memset all of its bytes to 0 (and ''\0'' is a zero byte!) and then read in less then 10 characters the char after the last read char IS 0 so the string functions know how to quit there.
Runicsoft -- home of my open source Function Parser and more
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement