How to input a string using scanf in c including whitespaces

后端 未结 4 778
慢半拍i
慢半拍i 2021-01-18 00:50

Example if user enters:

My name is James.

Using scanf, I have to print the full line, i.e. My name is James., then I have to get t

4条回答
  •  萌比男神i
    2021-01-18 01:48

    #include "stdio.h"
    int main()
    {
        char str[20];
        int i,t;
        scanf("%d",&t); 
        while(t--){
            fflush(stdin);
            scanf(" %[^\t\n]s",str);// --scanf to accept multi-word string
            i = strlen(str);// -- variable i to store length of entered string
            printf("%s %d\n",str,i);// -- display the entered string and length of string
        }
    
        return 0;
    }
    

提交回复
热议问题