Strange String Manipulation problem

Started by
14 comments, last by hammerstein_02 21 years, 6 months ago
Slowness alert:

    #include <iostream>#include <string>#include <boost/tokenizer.hpp>std::string reverseWordOrder(const std::string& sentence) {  boost::tokenizer<> tok(sentence);  //create the result string that has words in reversed order  std::string result;  for(boost::tokenizer<>::iterator beg=tok.begin(); beg!=tok.end(); ++beg){    result = *beg + " " + result;  }  if (result.length() == 0)    return "";  else    //take care of the trailing space    return result.erase(result.length()-1, result.length());}int main(){  std::cout << reverseWordOrder("Terry is a moron") << std::endl;  return 0;}  


...but it works (yeah, I'm just too lazy to debug your code)

[edited by - civguy on September 30, 2002 12:20:30 PM]
Advertisement
quote:Original post by hammerstein_02
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..


i''d say its something to do with your compiler...
when i ran your original code on VC++ 5 then, according to what sentence i try, it crashed, gave me a debug assertion error or just printed 255 characters of garbage


Runicsoft -- home of my open source Function Parser and more
There''s one more bug that wasn''t addressed here yet (I think). You need to put
newsentence[0] = ''\0'';
somewhere in there before you use newsentence. Otherwise it may contain garbage (instead of an empty string) which causes a crash at the first strcat.
Inplace.

  #include <iostream>template <typename T>void reverse(T* start, T* end) // assume end is one past the end of the characters you want to swap{	for(;start < --end; ++start)	{		T temp = *start;		*start = *end;		*end = temp;	}}template <typename T>void reverseWords(T* start, T* end) // assume end is one past the end of the characters you want to swap{	reverse(start, end);	T* walker = start;	while(walker < end)	{		while(*walker != T('' '') && walker != end) ++walker;		reverse(start, walker);		start = ++walker;	}}int main(int argc, char* argv[]){	using namespace std;	char input[] = "This is my sentence";	cout << input << endl;	reverseWords(input, input + sizeof(input) - 1);	cout << input << endl;}  

char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/
Okie dokie, folks, let me introduce you to "string.h":

    #include <iostream>#include <string.h>using namespace std;int main (){   char str[4096];   cin.getline (str, 4096);   char *pWord;   while (NULL != (pWord = strrchr (str, ' ')))   {      cout << pWord+1 << ' ';      *pWord = 0;   }   cout << str << endl;   return 0;}    


[edited by - Stoffel on September 30, 2002 3:51:00 PM]
That doesn''t actually reverse a sentence, it just prints it out in reverse order. You''d have to do that into a string or stringstream, tidy up the spaces, then write them back to the array in order to do what''s asked.

IMO.
char a[99999],*p=a;int main(int c,char**V){char*v=c>0?1[V]:(char*)V;if(c>=0)for(;*v&&93!=*v;){62==*v&&++p||60==*v&&--p||43==*v&&++*p||45==*v&&--*p||44==*v&&(*p=getchar())||46==*v&&putchar(*p)||91==*v&&(*p&&main(0,(char**)(--v+2))||(v=(char*)main(-1,(char**)++v)-1));++v;}else for(c=1;c;c+=(91==*v)-(93==*v),++v);return(int)v;}  /*** drpizza@battleaxe.net ***/

This topic is closed to new replies.

Advertisement