1

I'm suppose to print an abbreviated version of words that are strictly larger than 10 ("first letter", number of letters between the first letter and the last letter, "last letter" for ex - international should become i11l ), if not than print the word as it is. I'm able to do it using scanf.

scanf("\n%s", longWord);

but when I try to read the string with fgets, I'm not getting the desired output. (it doesn't show any error though), I'm not sure if the problem is with reading the input or generating the desired formatted output.

I'm new to programming and I'm having hard time understanding strings and their manipulations. Any help will be appreciated.

#include<stdio.h>
#include<string.h>
#define buffer_size 100

int main()
{
    int n;
    scanf("%d", &n);

    while(n--)
    {
        char longWord[buffer_size];
        int wordSize;

        fgets(longWord, sizeof(longWord), stdin); 
        wordSize = strlen(longWord);

        if(wordSize > 10)
        {
            printf("\n%c%d%c", longWord[0], wordSize-2, longWord[wordSize-1]);
        }
        else
        {
            printf("\n%s", longWord);
        }
    }
    return 0;
}
0_0perplexed
  • 85
  • 11
  • 2
    The line read with `fgets` is terminated by a `'\n'`. – Jabberwocky Sep 30 '20 at 07:30
  • 1
    After `fgets()` do `longWord[strcspn(longWord, "\n")] = 0;` – David C. Rankin Sep 30 '20 at 07:31
  • Interesting reading: https://stackoverflow.com/questions/2693776/removing-trailing-newline-character-from-fgets-input – Jabberwocky Sep 30 '20 at 07:32
  • Also: https://stackoverflow.com/questions/5918079/fgets-doesnt-work-after-scanf – Bob__ Sep 30 '20 at 08:11
  • @DavidC.Rankin It did work, but what exactly happened with that line of code. – 0_0perplexed Sep 30 '20 at 10:48
  • 1
    `strcspn(longWord, "\n")` returns the number of initial characters in `longWord` that does not include `'\n'`. (effectively `strlen(longWord) - 1`). So `longword[that] = 0;` simply overwrites the `'\n'` with `'\0'` (ASCII `0`) terminating `longWord` where `'\n'` was -- removing the `'\n'` from the string. It is the safest and easiest way to do so. If there is no `'\n'` (or the string is empty) - no problems - it just overwrites the `'\0'` with '`\0'` – David C. Rankin Sep 30 '20 at 18:19
  • OT: for ease of readability and understanding: please consistently indent the code. Indent after every opening brace '{'. Unindent before every closing brace '}'. Suggest each indent level be 4 spaces. – user3629249 Sep 30 '20 at 19:55
  • OT: when writing the format string for `printf()`, in general, the format string should end with `'\n'` so the data is immediately passed to the terminal rather than left sitting in the stdout stream buffer until the program exits. – user3629249 Sep 30 '20 at 19:58
  • NOTE: the function: `fgets()` also inputs the trailing newline `\n` into the input buffer. Your code needs to remove that newline. Suggest: `longword[ strcspn( longword, "\n" ) ] = '\0';` – user3629249 Sep 30 '20 at 20:00
  • OT: before the call to `fgets()` or `scanf()` it is (usually) best to prompt the user, so they know what the program is expecting. – user3629249 Sep 30 '20 at 20:05

0 Answers0