Strings
Strings
In C programming, a string is a sequence of characters terminated with a null character \0. For
example:
When the compiler encounters a sequence of characters enclosed in the double quotation marks,
it appends a null character \0 at the end by default.
char s[5];
Here, we are trying to assign 6 characters (the last character is '\0') to a char array having 5
characters. This is bad and you should never do this.
The scanf() function reads the sequence of characters until it encounters whitespace (space,
newline, tab etc.).
Output
Even though Dennis Ritchie was entered in the above program, only "Ritchie" was stored in the
name string. It's because there was a space after Dennis.
You can use the fgets() function to read a line of string. And, you can use puts() to display
the string.
Output
Here, we have used fgets() function to read a string from the user.
The sizeof(name) results to 30. Hence, we can take a maximum of 30 characters as input which
is the size of the name string.
Note: The gets() function can also be to take input from the user. However, it is removed from
the C standard.
It's because gets() allows you to input any length of characters. Hence, there might be a buffer
overflow.
int main()
{
char str[50];
printf("Enter string: ");
fgets(str, sizeof(str), stdin);
displayString(str); // Passing string to a function.
return 0;
}
void displayString(char str[])
{
printf("String Output: ");
puts(str);
}
Similar like arrays, string names are "decayed" to pointers. Hence, you can use pointers to
manipulate elements of the string. check C Arrays and Pointers before you check this example.
int main(void) {
char name[] = "Harry Potter";
char *namePtr;
namePtr = name;
printf("%c", *namePtr); // Output: H
printf("%c", *(namePtr+1)); // Output: a
printf("%c", *(namePtr+7)); // Output: o
}
To solve this, C supports a large number of string handling functions in the standard library
"string.h".
#include <string.h>
Note: You have to include the code below to run string handling functions.
Functions gets() and puts() are two string functions to take string input from the user and display
it respectively as mentioned in the previous chapter.
#include<stdio.h>
int main()
{
char name[30];
printf("Enter name: ");
gets(name); //Function to read string from user.
printf("Name: ");
puts(name); //Function to display string.
return 0;
}
Note: Though, gets() and puts() function handle strings, both these functions are defined in
"stdio.h" header file.
String Examples in C Programming
Find the Frequency of a Character
#include <stdio.h>
int main() {
char str[1000], ch;
int count = 0;
Output
Then, the user is asked to enter the character whose frequency is to be found. This is stored in
variable ch.
Then, a for loop is used to iterate over characters of the string. In each iteration, if the character
in the string is equal to the ch, count is increased by 1.
Output
Here, the string entered by the user is stored in the line variable.
Initially, the variables vowel, consonant, digit, and space are initialized to 0.
Then, a for loop is used to iterate over characters of a string. In each iteration, whether the
character is vowel, consonant, digit, and space is checked. Suppose, the character is a vowel, in
this case, the vowel variable is increased by 1.
When the loop ends, the number of vowels, consonants, digits and white spaces are stored in
variables vowel, consonant, digit and space respectively.
Here, using a for loop, we have iterated over characters of the string from i = 0 to until '\0'
(null character) is encountered. In each iteration, the value of i is increased by 1.
When the loop ends, the length of the string will be stored in the i variable.
Output
This program takes a string input from the user and stores in the line variable. Then, a for loop
is used to iterate over characters of the string.
If the character in a string is not an alphabet, it is removed from the string and the position of the
remaining characters are shifted to the left by 1 povariable