String in C Programming Part-1
String is a sequence of characters. In previous we know character is a single alphabet/number/sign within single quotes. But a string is made by one or more characters within double quotes.
Example, “Hello”, “I want to be a programmer” are different strings. Do you remember that you have used printf(“Hello Worldn”); statement to print Hello World in your terminal by using C? There, which you have used withing double quotes is a string with whitespace.
Declaration of Strings
char string_name[string_size] = "STRING";
Size of string must be greater than total characters and 1. Because, there has one null character at last in character array string.
String Input Process:
Generally we can input in string in 3 ways.
1. By using scanf() function:
Limitations: By using specifier %s scanf() function can input only one string. It can’t input another string with whitespace. To solve this problem, we can use by replacing edit set conversion code or %[^n] in %s.
Example: If we use the following statement, it can input more string with whitespace.
Now run the following program in your c compiler.
Remember, If you use scanf() function to input string, then you have to use only printf(), puts() to print this string. You can’t use putchar() function.