-1

I tried modifying this code to print

no solution

if there is no input by user. That is, if I run the program and simply press enter it should print no solution. I added the code that's meant to do that to check if the string length is 0 then print but it doesn't work

#include <stdio.h>
#include <string.h>
int main()
{
    char str1[100];
    char newString[10][10];
    int i, j, ctr;

    fgets(str1, sizeof str1, stdin);
    j = 0; ctr = 0;

    if (strlen(str1) == 0) {
        printf_s("no solution");
    }
    else 
    for (i = 0; i <= (strlen(str1)); i++)
    {

        // if space or NULL found, assign NULL into newString[ctr]
        if (str1[i] == ' ' || str1[i] == '\0')
        {
            newString[ctr][j] = '\0';
            ctr++;  //for next word
            j = 0;    //for next word, init index to 0
        }
        else if (str1[i] == '.' || str1[i] == ',')
        {
            newString[ctr][j] = '\0';
            ctr--;  //for next word
            j = - 1;   
        }

        else
        {
            newString[ctr][j] = str1[i];
            j++;
        }

    }
    printf("\n\n");
    for (i = 0; i < ctr; i++)
        printf(" %s\n", newString[i]);
    return 0;
}
Stidgeon
  • 2,673
  • 8
  • 20
  • 28
  • 1
    How s this `C#`? – Sourav Ghosh Apr 16 '20 at 06:05
  • 1
    _"...it doesn't work..."_ is **not** a problem description. Error messages? Exceptions? What happens instead of what you expected? Did you [debug](https://stackoverflow.com/q/25385173/5528593)? – René Vogt Apr 16 '20 at 06:07
  • 1
    `fgets` will retain the new-line character at the end of the line and just hitting return will produce `"\n"`. Hence, `strlen(str1)` will not be zero, at least not when reading input fro the terminal. Print the message after your main loop when `ctr == 0`. – M Oehm Apr 16 '20 at 06:07
  • 1
    The first thing that the code needs to do is verify that the return value from `fgets` is not NULL. Because, if `fgets` returns NULL, the contents of `str1` will be random garbage. Assuming that `fgets` returns a non-NULL pointer, the next step is to [remove the newline that fgets puts in the buffer](https://stackoverflow.com/questions/2693776). Then, and only then, you can check the length of the string, and see if it's 0. – user3386109 Apr 16 '20 at 06:17

1 Answers1

1

fgets() will add a new line to your string (see this link for more information) which means when you simply press enter your string length(since your string includes \n) is 1 , you should say:

    if (strlen(str1) == 1) {
        printf_s("no solution");
         // it's better to add a return 0; here if you don't want to continue the program
    }

or use this instead:

    if (!strcmp(str1,"\n")) {
        printf_s("no solution");
    }
hanie
  • 1,863
  • 3
  • 9
  • 19
  • 1
    Or avoid the function calls and simply test `if (*str1 == '\n') { puts ("no solution"); break; }` which simply test if the first character in `str1` is the newline character. (`*str1` is simply `*(str1 + 0)` or `str1[0]` -- which ever you prefer) – David C. Rankin Apr 16 '20 at 06:50