1

I'm trying to write a little program to reverse the words in a string (the classic interview question) but I'm hitting a bit of a snag. On another portion of the site, I came across a really cool way to reverse strings in place using bitwise operations, so I'm attempting to use a program that only utilizes a single reverse() that will be called in reverse the entire string and then the substrings.

Unfortunately, my program will only reverse the first substring and I'm having trouble finding out why. Any help would be much appreciated.

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void reverse(char string[], int beg, int end) {
  int length = end-beg-1;
  for(int i=beg; i < length/2; i++) {
    string[i] = string[i] ^string[length-i];
    string[length-i] = string[i] ^ string[length - i];
    string[i] = string[i] ^string[length-i];
  }

 }

int main (int argv, char * arrv[]) {
   int beg, end;
   char string[] = "This is the string to reverse";
   int length = strlen(string);
   printf("%s\n", string);
   printf("%d\n", length);
 //reverse whole string
   reverse(string, 0, length);
   printf("%s\n", string);
   beg =0;
   end = 0;
  //reverse strings at index's beg and end
    for(int i = 0; i <= length-1; i++) {
      if (string[i] == ' ') {                                      
       reverse(string, beg, end);
       beg = i+1;  
     }
     end++;
     printf("%s\n", string);
   }
   printf("%s\n", string);
    return 1;
}

Thanks I.E Expected Output: reverse to string the is this My Output: reverse ot gnirts eht si siht

hichris123
  • 10,145
  • 15
  • 56
  • 70
  • I don't understand your question, the code you've posted is ok (I ran it in my computer), the only thing that's out of place is the `for` loop in `main` becaus the string was already reversed earlier in the code. – ichramm Mar 24 '15 at 21:45
  • I'm not just trying to reverse the entire string, I'm trying to reverse all the words in the string. For example: "This is a string" should become "string a is This". – Charles Davis Mar 24 '15 at 21:47
  • have you tried stepping though this with a debugger and putting some watches out on the variables, memory ? – Philip Stuyck Mar 24 '15 at 21:54
  • Do you not expect/get a capital T on 'This'? – Jonathan Leffler Mar 24 '15 at 21:59
  • Try your reverse() function against the string "ab". It should yield "ba" but it does not. Instead it yields "ab", the original string unchanged. Fix that and you will be in better shape. – jarmod Mar 24 '15 at 22:00
  • Use the idiomatic `for (int i = 0; i < length; i++)` in place of the equivalent but non-idiomatic `for (int i = 0; i <= length - 1; i++)`. – Jonathan Leffler Mar 24 '15 at 22:00

1 Answers1

0

Adding up @alexd 's suggestion, following is the updated program with few fixes:

#include<stdlib.h>
#include<stdio.h>
#include<string.h>
void reverse(char string[], int beg, int end) {
    int length = end - beg;
    for (int i = 0; i < length / 2; i++) {
        string[beg + i] = string[beg + i] ^ string[end - 1 - i]; 
        string[end - 1 - i] = string[beg + i] ^ string[end - 1 - i]; 
        string[beg + i] = string[beg + i] ^ string[end - 1 - i]; 
    }   
}

int main (int argv, char * arrv[]) {
   int beg, end;
   char string[] = "This is the string to reverse";
   int length = strlen(string);
   printf("%s\n", string);
   printf("%d\n", length);
 //reverse whole string
   reverse(string, 0, length);
   printf("%s\n", string);
   beg =0; 
   end = 0;
  //reverse strings at index's beg and end
    for(int i = 0; i <= length-1; i++) {
      if (string[i] == ' ') {    
       reverse(string, beg, end);
       beg = i+1;  
       printf("%s %d %d\n", string, beg, end);
     } else if(i==length-1) {
       end = length;
       reverse(string, beg, end);
     }   
     end++;
 }
   printf("%s\n", string);
    return 1;
}
bprasanna
  • 2,423
  • 3
  • 27
  • 39