How to input a string using scanf in c including whitespaces

后端 未结 4 659
慢半拍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条回答
  •  孤城傲影
    2021-01-18 01:42

    #include "stdio.h"
    #include "conio.h"
    
    void main()
    {
    char str[20];
    int i;
    clrscr();
    printf("Enter your string");
    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",str,i); -- display the entered string and length of string
    getch();
    
    }
    
    output :
    
    enter your string : My name is james
    display output : My name is james 16
    

提交回复
热议问题