String in C Programming

What is strings?
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 World\n”); 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
Example:
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.
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 World\n”); 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];
orchar string_name[string_size] = "STRING";
orExample:
char str[11]="Bangladesh";
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:
#include <stdio.h>
int main()
{
char ch[40];
scanf("%s",ch);
printf("%s",ch);
return 0;
}
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.
scanf("%[^\n]",ch);
Now run the following program in your c compiler.
#include <stdio.h>
int main()
{
char ch[40];
scanf("%[^\n]",ch);
printf("%s\n",ch);
return 0;
}
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.