0

I am supposed to print out Number of letters in a word given by the user and also print it out in reverse. I struggle to find out how i am supposed to declare the array when i dont have the size of the string. I thought of using gets and then a for loop but can’t figure out how to define char in both the function for printing number of letters and printed in reverse.

  • Please give the full description with exact wording of the original task given to you. Details matter and your re-worded description is not entirely clear. A common approach is just to use a MAX sized array which is assumed to be big enough to store the longest string. But it is unknown whether that meets your exact requirements. Another approach is to read one or X characters at a time until a specific delimiter or end of input. – kaylum Oct 02 '21 at 11:15
  • Does this answer your question? [How to read string from keyboard using C?](https://stackoverflow.com/questions/7709452/how-to-read-string-from-keyboard-using-c) – einpoklum Oct 02 '21 at 11:19
  • Counting characters in a word entered by the user is easy. Set a counter to zero. Read one character at a time (e.g., using `getchar`). If the character is a letter, increment the counter and continue the loop. If it is not a letter or the attempt to read fails (e.g., `getchar` returns `EOF`), exit the loop and print the value of the counter. For embellishment, you might add code to ignore white space before the letters start, to ignore white space after the letters end (until a new-line character is seen), and/or to report a warning if any other characters are seen. – Eric Postpischil Oct 02 '21 at 11:27
  • Is that sufficient for your assignment? It is sufficient for the problem you have posed here. But we do not know if your assignment has additional requirements such as reading a whole line before counting letters, because you have not provided that information. – Eric Postpischil Oct 02 '21 at 11:27

2 Answers2

1

When you obtain the string - say, with scanf() or fgets() - you can specify a maximum "field width", i.e. the maximum number of characters to read into your buffer. So, you first set the buffer size, then you read into it.

Now, after you've read the string, you can determine it's length the usual C way with strlen() on your buffer. Alternatively, if you're using scanf(), you can use the "%n" specifier to store the number of characters consumed, so that scanf("%49s%n", buffer, &count); will scan the string into the 50-chararacter-long buffer and the number of characters into count.


PS - Don't use the gets() function though...
Why is the gets function so dangerous that it should not be used?

einpoklum
  • 118,144
  • 57
  • 340
  • 684
0

Counting characters in a word entered by the user is easy. Set a counter to zero. Read one character at a time (e.g., using getchar). If the character is a letter, increment the counter and continue the loop. If it is not a letter or the attempt to read fails (e.g., getchar returns EOF), exit the loop and print the value of the counter. For embellishment, you might add code to ignore white space before the letters start, to ignore white space after the letters end (until a new-line character is seen), and/or to report a warning if any other characters are seen. –

Eric Postpischil

anatolyg
  • 26,506
  • 9
  • 60
  • 134