0

i'm trying to get input from user but when i typing more than one word it put something weird in "c" ( in case 1) i tried to replace scanf with fsacnf and fgets but nothing works! my code is:

#define MAXSIZE 50

int main()
{
    char s[MAXSIZE];
    char t[MAXSIZE];
    char c;
    int len1;
    int len2;
    int sum = 0;
    int res;


    int option;
    do
    {

        printf("\n\n1 : Check the number of occurrences in a string \n"); // printing the menu
        printf("2 : Check if string A is subsequence of string B \n");
        printf("3 : Reverse a string \n");
        printf("4 : Sum of digits in string \n");
        printf("5 : \n");
        printf("6 : Quit\n");
        printf("Enter your choice :\n");
        scanf("%d", &option);
        switch (option) //menu Implement
        {
        case 1:
            printf("please insert a string\n");
            scanf("%s",s);
            printf("please insert a char to find\n");
            scanf(" %c", &c); 
            res = cntAppearance(s, c);
            printf("the char %c appears %d time in your string\n", c, res);
            break;
        case 2:
            printf("Please insert string A\n");
            fgets(s, MAXSIZE-1, stdin);
            printf("Please insert string B\n");
            fgets(t, MAXSIZE-1, stdin);
            len1 = strlen(s);
            len2 = strlen(t);
            if (isSubSequence)
                printf("String a is subsequence of string B\n");
            else
                printf("String a isn't subsequence of string B\n");
            break;
        case 3:
            printf("Please insert string to reverse\n");
            fgets(s, MAXSIZE-1, stdin);
            RecReverse(s);

            break;
        case 4:
            printf("Please insert string to check\n");
            fgets(s, MAXSIZE-1, stdin);
            len1 = strlen(s);
            sum = DigInStringRec(s, len1, 0);
            printf("The sum of digits in this string is:%d\n", sum);

            break;
        case 5:

            break;
        }
    } while (option != 6);// exiting the program
}

can someone please help me with that?

Shay Doron
  • 179
  • 2
  • 10

0 Answers0