-2

There are some comments in the code for human-readable code:

#include <stdio.h>
#include <string.h>

#define SIZE 100   //size of the input array and output array
#define ACCUM_CHAR_SIZE 25  //size of the temp array
int main(){
   char i[SIZE];
   char acc[ACCUM_CHAR_SIZE];
   char o[SIZE];
   int it_l = 0, it_a = 0, it_r = 0;
   //it_l is the iterator to the input sentence,
   //it_a is the iterator to the temp array
   //it_r is the iterator to the output sentence
   printf("Enter a sentence:");
   gets(i);
   int len = strlen(i) - 1;
   while(it_l <= len){
        if(i[len - it_l] != ' '){
            acc[it_a] = i[len - it_l];  //add letters to acc until space
            it_a++;
        }
        else{
            it_a -= 1;
//acc is reversed, I reversed it again to the output sentence
            while(it_a >= 0){
                o[it_r] = acc[it_a];
                it_r++;
                it_a--;
            }
            it_r += 1;
            o[it_r] = 32;   //put a space
            it_a = 0;  //reset the temp array
            strcpy(acc, ""); //clear the acc
        }
        it_l++;
   }
   printf("%s", o);
}

The program theoretically looks fine, but when it is executed, it sometimes print garbage values, only some words, or sentence which has only reversed half with garbage value instead of spaces.

The program above is to save each word to the temp, and reverse temp (temp is reversed when storing the word) back to the output. However, it fails.

Thank you for your help.

  • 4
    I don’t see the `o` getting a null terminator anywhere, that will at least cause issues if it’s missing. – Sami Kuhmonen Sep 22 '18 at 04:47
  • Not the bug, but `' '` is guaranteed to be a space character while `32` isn't. – molbdnilo Sep 22 '18 at 04:49
  • It would be interesting to know what the input and output is *supposed* to look like. Can you [edit] the question to add sample input and output? – user3386109 Sep 22 '18 at 05:27
  • Fill `o` with something recognisable, like `'?'`, and observe the result. Pay close attention to `it_r`. Add some `printf` debugging to see what's happening. And think about how to handle the first word – there's probably not a space in front of it. – molbdnilo Sep 22 '18 at 05:27
  • Please read the documentation for `gets()`. That said, for a [mcve], your code shouldn't contain any input at all, unless the input itself is the problem. Replace it with some hardcoded values instead, so that nobody needs to interpret or guess anything in order to reproduce your problem. – Ulrich Eckhardt Sep 22 '18 at 10:04

4 Answers4

1

There are at least three problems.

The first problem is that you never terminate the string o To do that change:

   printf("%s", o);

into

   o[it_r] = '\0';
   printf("%s", o);

The second problem is that you increment it_r incorrectly. Change

        it_r += 1;
        o[it_r] = 32;   //put a space

into

        o[it_r] = ' ';  // Use ' ' instead of 32
        it_r += 1;

The third problem is that you don't handle the first word of the input (because there is no space in front). I'll leave that problem to you as an exercise.

BTW: Don't use gets for reading input. Use fgets instead.

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
1
#include <stdio.h>
#include <string.h>

#define SIZE 100   //The size of input array is prefered to be equal to ouput array.
int main(){
   char input[SIZE];
   char output[SIZE];
   int i = 0, j = 0;
   //i is the iterator to the input sentence,
   //j is the iterator to the output sentence
   printf("Enter a sentence:");
   gets(input);
   int len = strlen(input) - 1; //Total length.
   j = len;
   while(input[i]!= NULL){
            output[j] = input[i];  
            i++;
            j--;            
   }
    output[len+1]= NULL;
    printf("%s", output);
    printf("Finished");
 }
Zahid Khan
  • 2,130
  • 2
  • 18
  • 31
  • Your program reverse the whole input string. I think OP wants to reverse the words, i.e. "Hello World" shall result in "World Hello". – Support Ukraine Sep 22 '18 at 06:17
  • 1
    I think he is trying to reverse the letters of sentences. – Zahid Khan Sep 22 '18 at 06:21
  • If you look at OPs code you can see that OP copies a word in a way that reverse it and then reverse the word again to get it back to the normal form. – Support Ukraine Sep 22 '18 at 06:33
  • Very late reply,, sorry. Yes, user 4386427 is right. I would like to have the words to be reveresed instead of all letters. My description may be very ambiguious. But nevertheless thank you for your effort! –  Sep 22 '18 at 09:42
0

Try the modified code given below, the changed sections has been commented(all other comments were removed for readability)

#include <stdio.h>
#include <string.h>

#define SIZE 100
#define ACCUM_CHAR_SIZE 25  
int main(){
   char i[SIZE];
   char acc[ACCUM_CHAR_SIZE];
   char o[SIZE];
   int it_l = 0, it_a = 0, it_r = 0;

   printf("Enter a sentence:");
   gets(i);
   int len = strlen(i) - 1;
   while(it_l <= len){
        if(i[len - it_l] != ' '){
            acc[it_a] = i[len - it_l];  
            it_a++;
        }
        else{
            it_a -= 1;
            while(it_a >= 0){
                o[it_r] = acc[it_a];
                it_r++;
                it_a--;
            }
            /*it_r += 1; There is no need to increment it_r here as
              it is already incremented in the above loop*/
            o[it_r] = 32;
            it_r += 1;  /* The above increment statement was moved here(only 
            incremented when a new value is loaded in to output array) */
            it_a = 0;  
            strcpy(acc, ""); 
        }
        it_l++;
   }
  /* The below section reverses and stores the first word of the
   input which was not getting operated in the above loop */
   it_a -= 1;
    while(it_a >= 0){
        o[it_r] = acc[it_a];
        it_r++;
        it_a--;
    }
   o[it_r] = '\0'; // Terminating output array
   printf("%s", o);
}

The above code will work as expected but has some small issues(given below)

  1. Using gets():- gets is not recommended for inputting strings. See this link for more info.

  2. The Size of temporary array acc :- If a word greater than 25 characters is inputted the program may output garbage value.

  • Sorry for being late! Thank you for pointing out my problems and the flaws. Although not having a computer with me right now, I think it will work beautifully. Thanks! –  Sep 22 '18 at 09:38
0

Version without temporary storage.

  • read the input string from start (0)
  • copy any word from input into output, starting from the end of output - words are not reversed
  • copy any space in output, from position in output, starting from the end

Code

#define SIZE 100   //size of the input array and output array

int main(){
    char i[SIZE];
    char o[SIZE];

    printf("Enter a sentence: ");
    fgets(i, SIZE, stdin);     // fgets instead of gets

    int j,len = strlen(i) - 1; // assuming you eat the EOL character
    o[len] = '\0';             // start by marking end of output string

    for(j=0 ; j<len ; j++) {   // navigating the input string
        if (i[j] == ' ') {
            o[len-j-1] = i[j]; // if space, copy (from end) in output
        }
        else {
            int k=j;
            do {
                k++;           // count(+1) the word, non-space, characters
            } while (k<len && i[k] != ' ');
            strncpy(&o[len-k], &i[j], k-j); // copy the word
            j = k-1;
        }
    }

   printf("%s\n", o);
}
Déjà vu
  • 28,223
  • 6
  • 72
  • 100