- Write a C program to read a string and print string on screen
- Write a C program for input and output of string.
Reading a String
Print string in c: We can use scanf function with %s format specifier to read string from user. Here is the syntax of scanf to read a string
scanf("%s", char *inputCharArray);
scanf reads the input from keyboard and adds a ‘\0’ character at the end of array.
Points to Remember
- Length of the input string should not be more than character array used for storing string. C doesn’t perform any array index bound checking, hence it may overwrite something important.
- scanf() cannot use read space separated multi-word string like “Tech Crash Course”. However we can use gets() to read multi-word string.
- gets() function can only read one string at a time.
Printing a String
To print a string we can either use printf with %s format specifier or puts() function. While printf can print multiple strings at a time puts can only print one string at a time.
Points to Remember
- printf can print multiple string at a time, whereas puts can only print one string at a time.
- After printing string puts places the cursor on next line, whereas printf doesn’t move cursor to next line.
C program to read and print string using scanf and printf
How to print string in c: This program first takes a string as input from user using scanf function and stores it in a character array inputString. It automatically adds a null terminating character at the end of input string. Then it uses printf function to print inputString on screen.
/* * C Program to read and print string using scanf and printf */ #include <stdio.h> #include <conio.h> int main(){ char inputString[100]; printf("Enter a string\n"); /* Read string from user using scanf and store it in inputString char array */ scanf("%s", inputString); /* Print string stored in inputString using printf */ printf("%s\n", inputString); getch(); return 0; }
Program Output
Enter a string TechCrashCourse TechCrashCourse
Enter a string Tech Crash Course Tech
C program to read and print string using gets and puts function
Print a string in c: This program first takes a string as input from user using gets function and stores it in a character array inputString. The advantage of using gets function is that, it can read string containing white cpace characters but gets can only read one string at a time. Then it uses puts function to print inputString on screen.
/* * C Program to read and print string using gets and puts */ #include <stdio.h> #include <conio.h> int main(){ char inputString[100]; printf("Enter a string\n"); /* Read string from user using gets and store it in inputString char array */ gets(inputString); /* Print string stored in inputString using puts */ puts(inputString); getch(); return 0; }
Program Output
Enter a string Tech Crash Course Tech Crash Course
C program to read and print string using getchar and putchar function
How to print a string in c: getchar() function reads one character at a time. We can use getchar function inside a loop to read characters one by one till we don’t read newline character (\n). Once we read newline character we break loop and add ‘\0’ character at the end of string.
/* * C Program to read and print string using getchar and putchar */ #include <stdio.h> #include <conio.h> int main(){ char inputString[100], c; int index = 0; printf("Enter a string\n"); /* Read string from user using getchar inside while loop */ while((c = getchar()) != '\n'){ inputString[index] = c; index++; } inputString[index] = '\0'; /* Print string stored in inputString using putchar */ index = 0; while(inputString[index] != '\0'){ putchar(inputString[index]); index++; } getch(); return 0; }
Program Output
Enter a string C Programming C Programming