Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

hammerstein_02

Strange String Manipulation problem

This topic is 5773 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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?
  
#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;
}
  

Share this post


Link to post
Share on other sites
Advertisement

    
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]

Share this post


Link to post
Share on other sites
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:

  
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)

Share this post


Link to post
Share on other sites
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:

  
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Yeah.. I know my coding practice isn''t to hot on memory allocation right now, I will give that some time and experience hopefully. However, can you answer me this, why did it work for a lot of strings but not for any I had 5 letters in? And it was consistent with these..

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!