1

I have created a function that reverses all the words in a sentence, meaning that if the input is "Hello World" the output is supposed to be "World Hello". The code below is the function.

char*  reversesentence(char sent[]) {
  int lth = strlen(sent);
  int i;

  for(i = lth -1; i >= 0; i--) {
    if(sent[i] == ' ') {
        sent[i] = '\0'; 
        printf("%s ", &(sent[i]) + 1); 
    }
  }

  printf("%s", sent);
}

In the main I am trying to ask the user for the sentence and calling the function in the main.

  int main(void)
  {
    char sentence[2000];

    printf("Please enter the sentence you want to be reversed.\n");

    scanf("%s", sentence);
    reversesentence(sentence);
    printf("%s", sentence);
  }

It seems that the array is only storing the first word of the sentence only.

Output:

Please enter the sentence you want to be reversed.
hello my name is 
hellohello
Process finished with exit code 0`

Can someone help me fix this please? Searched online and found nothing useful.

bruno
  • 32,421
  • 7
  • 25
  • 37
  • 2
    Use [`fgets`](http://www.cplusplus.com/reference/cstdio/fgets/) instead of `scanf`. – Jabberwocky Jan 07 '19 at 09:24
  • Possible duplicate of [Scanf allow space between words](https://stackoverflow.com/questions/9437919/scanf-allow-space-between-words) – Mathieu Jan 07 '19 at 09:29

2 Answers2

2

scanf stops reading when it occurs whitespace,tabs or newline.

Matches a sequence of non-white-space characters; the next pointer must be a pointer to character array that is long enough to hold the input sequence and the terminating null byte ('\0'), which is added automatically. The input string stops at white space or at the maximum field width, whichever occurs first.

Thus you are not reading the entire string as you input.

Try using fgets as below.

fgets(sentence, sizeof(sentence), stdin); 

Note fgets appends \n to end of the string. see how to trim the new line from fgets

kiran Biradar
  • 12,700
  • 3
  • 19
  • 44
  • Tried using it now, but it didn't give me time to enter the input. Output: Please enter the sentence you want to be reversed. Process finished with exit code 0 – Francesca Attard Jan 07 '19 at 09:30
  • @FrancescaAttard you also forgot to `return` from `reversesentence()` – artm Jan 07 '19 at 09:37
0

You have two problems

  • as it was already said you only read one word using scanf
  • reversesentence just replace all spaces by a null character, so even you read a full line you cut it at the first space. so if you enter "hello world" the result will be "hello", and if you enter " hello world" the result will be an empty string

The simple way is to read words using scanf in a loop until it returns EOF, memorizing them, to at the end produce the list of words returned

For instance :

#include <stdlib.h>
#include <string.h>

int main()
{
  size_t len = 0;
  char * result = 0;
  char word[256];

  while (scanf("%256s", word) != EOF) {
    if (result == 0) {
      result = strdup(word);
      len = strlen(word);
    }
    else {
      size_t l = strlen(word);
      char * r = malloc(len + l + 2);

      strcpy(r, word);
      r[l] = ' ';
      strcpy(r + l + 1, result);
      free(result);
      result = r;
      len += l + 1;
    }
  }

  puts(result);
  free(result);
  return 0;
}

The reading finishes at the end of the input (^d under a linux shell), the words can be given on several lines.

With the input

hello world
how
are you
?

that prints

? you are how world hello

bruno
  • 32,421
  • 7
  • 25
  • 37