Reverse word by word

I can change the line. For example, I can undo "change string" to "esrever gnirts". But I canโ€™t cancel this word by word like "string a reverse".

void reverseString(char string[],char *start, char* end) { char tmp; //temporary variable to swap values int count = 0; while(start<end) { if(*start==' ') { printf("found space count %d \n",count); reverseString(string,start-count,start); } tmp = *start; *start = *end; *end = tmp; *start++; *end--; count++; } printf(" string %s \n", string); } int main() { char string[] = "reverse a string word by word"; char *start =string; char *end =start+ strlen(string) -1; reverseString(string,start,end); return 0; } 
+2
source share
9 answers

Like this. I can change the line by words, as well as the entire line. Just review the code and see if the logic helps.

 #include <stdio.h> #include <string.h> void stringrev(char *); void reverseWords(char *); void reverseString(char* , int); int main() { char string[] = "reverse a string word by word"; reverseWords(string); printf("\nWord-Wise Reversed String : %s\n",string); stringrev(string); return 0; } void reverseWords(char * str) { int i = 0, j = 0; reverseString( str, strlen(str) ); while( 1 ) // Loop forever { if( *(str+j) == ' ' || *(str+j) == '\0') // Found a word or reached the end of sentence { reverseString( str+i, ji ); i = j+1; } if( *(str+j) == '\0') { break; } j++; } } void reverseString(char* str, int len) { int i, j; char temp; i=j=temp=0; j=len-1; for (i=0; i<j; i++, j--) { temp=str[i]; str[i]=str[j]; str[j]=temp; } } void stringrev(char *str) { int i=-1,j=0; char rev[50]; while(str[i++]!='\0'); while(i>=0) rev[j++] = str[i--]; rev[j]='\0'; printf("\nComplete reverse of the string is : %s\n",rev); } 
+2
source

Do what you have already done, and then discard the whole result (without specially processing the spaces).

+3
source

Use the stack implementation for this problem.

Step 1. Write a line to a file

Step 2: read it from the file and click on the linked list

Step 3: Use the stack implementation in this linked list

Step4: Pop the linked list from head to end !!

It's the other way around ... !!

+1
source

Not very effective, but should work:

 void reverse_string_word(char *data) { char *saveptr; char *word; char *tmp = malloc(strlen(data) + 1); char *tmp2 = malloc(strlen(data) + 1); *tmp = 0; *tmp2 = 0; word = strtok_r(data, " ", &saveptr); if (word) { strcpy(tmp, word); } while (word) { word = strtok_r(NULL, " ", &saveptr); if (word) { sprintf(tmp2, "%s %s", word, tmp); strcpy(tmp, tmp2); } } strcpy(data, tmp); free(tmp); free(tmp2); } 
0
source

divide it into an array of words (char **), undo it, and then concatenate it again.

0
source

It uses a Java solution that does not use a scanner or stack to parse words. It starts at the end of the line and works in the opposite direction. It may be more elegant, but it works - if someone has a recursive Java solution that I would like to see.

 "one two three four" is returned as "four three two one" private void reverseWordsNoStackNoScanner(String str) { System.out.println("reverseWordsNoStackNoScanner "+str); String[] buff = new String[str.length()]; int end=str.length()-1; int j=end; int start=0; int ptr=0; for (int i=str.length()-1;i>=0;i--){ boolean writeBuff=false; if (str.charAt(i)!=' ') { // have we backed up to a blank? j--; //no } else { //yes! write out this word writeBuff=true; } if (i==0) writeBuff=true; //are we done (position 0)? if (writeBuff) { //time to write a word? //we've hit a delimiter (or we're done) ptr=j; //pointing at a blank or the beginning ptr++; //bump past the blank or the beginning while(ptr<=end){ //write the word from beginning to finish buff[start++]=String.valueOf(str.charAt(ptr++)); } //don't write a blank when we are on the last word (past the end) if (i>0)buff[start++]=" "; j--; //set pointers for next iteration end=j; //back up end ptr to new 'end' - to parse the next word } } //print out our reversed word string for (String s: buff) { System.out.print(s); } } 
0
source

Read the line from the beginning and push each character onto the character stack. After you come across space or a new line or eof, start popping up characters and printing them one by one at standard output or storing them in a file as you like.

0
source
 'My name is' return as 'yM eman si' 'My name is' return as 'yM eman si' #include<stdio.h> #include<malloc.h> #include<string.h> void reve(char [],int,int); void main() { char *a; int i,j=-1; a=(char*)malloc(1000*sizeof(char)); gets(a); for(i=0;a[i]!='\0';i++) { if(a[i]!=' '&&j==-1) j=i; if((a[i]==' '&&a[i+1]!=' '&&j>=0)||a[i+1]=='\0') { a[i]==' '?reve(a,j,i-1):reve(a,j,i); j=-1; } } for(i=0;a[i]!='\0';i++) printf("%c",a[i]); 

}

 void reve(char a[],int j,int i) { char temp; if(a[0]==' ') j=0; for(;i!=j&&j<i;i--,j++) { temp=a[j]; a[j]=a[i]; a[i]=temp; } 

}

0
source
 'i love india' return as 'india love i' ' i love india' return as 'india love i' #include<stdio.h> #include<malloc.h> #include<string.h> void reve(char [],int,int); void main() { char *a; int i,j=-1; a=(char*)malloc(1000*sizeof(char)); gets(a); for(i=0;a[i]!='\0';i++) { if(a[i]!=' '&&j==-1) j=i; if((a[i]==' '&&a[i+1]!=' '&&j>=0)||a[i+1]=='\0') { a[i]==' '?reve(a,j,i-1):reve(a,j,i); j=-1; } } reve(a,0,i-1); for(i=0;a[i]!='\0';i++) printf("%c",a[i]); 

}

 void reve(char a[],int j,int i) { char temp; if(a[0]==' ') j=0; for(;i!=j&&j<i;i--,j++) { temp=a[j]; a[j]=a[i]; a[i]=temp; } 

}

0
source

Source: https://habr.com/ru/post/1490910/


All Articles