-
Hajipur, Bihar, 844101
In C programming, a string is a sequence of characters stored in contiguous memory locations. Unlike other languages, C does not have a dedicated string type. Instead, strings are represented as arrays of characters terminated by a special character called the null character (\0). The null character marks the end of the string, allowing C functions to know where the string finishes.
Strings are essential in programming for handling text, user input, file data, and messages. They allow you to store, modify, and manipulate sequences of characters efficiently.
A string in C is declared as a character array. You must leave extra space for the null character:
char name[20]; // can store up to 19 characters + '\0'
name can hold 19 characters; the 20th position is reserved for \0.
The null character is automatically appended if a string is initialized with double quotes.
Example:
char greeting[6] = {'H', 'e', 'l', 'l', 'o', '\0'};
This creates the string "Hello" explicitly including the null character.
Strings can be initialized at the time of declaration using double quotes:
char greeting[] = "Hello";
The compiler automatically adds the null character at the end.
The array size is inferred from the string length plus one (5 + 1 = 6).
Individual characters of a string can be accessed using indices, just like arrays:
#include <stdio.h>
int main() {
char word[] = "Hello";
printf("First character: %c\n", word[0]);
printf("Third character: %c\n", word[2]);
return 0;
}
Output:
First character: H
Third character: l
Indices start at 0.
Strings are mutable if declared as character arrays, allowing modification of characters.
You can change characters at specific positions:
word[0] = 'h'; // Change 'H' to 'h'
printf("%s\n", word); // hello
The null character ensures that printing stops at the end of the string.
Modifying string literals directly (e.g., char *str = "Hello";) is not safe, as they may reside in read-only memory.
C provides a standard library <string.h> for common string operations:
strlen() – returns the length of the string (excluding \0).
strcpy() – copies one string into another.
strcat() – concatenates (joins) two strings.
strcmp() – compares two strings.
strchr() – finds the first occurrence of a character.
strstr() – finds the first occurrence of a substring.
Example:
#include <stdio.h>
#include <string.h>
int main() {
char str1[20] = "Hello";
char str2[20] = "World";
printf("Length of str1: %lu\n", strlen(str1));
strcat(str1, str2); // join str2 to str1
printf("Concatenated string: %s\n", str1);
return 0;
}
Output:
Length of str1: 5
Concatenated string: HelloWorld
To read a string from the user, you can use scanf() or fgets():
Using scanf():
char name[50];
printf("Enter your name: ");
scanf("%s", name); // reads input until the first space
printf("Hello, %s\n", name);
Limitation: stops reading at the first space.
Using fgets() (preferred for spaces):
char name[50];
printf("Enter your full name: ");
fgets(name, sizeof(name), stdin);
printf("Hello, %s", name);
fgets() reads the entire line including spaces, stopping at newline or max size.
You can iterate over a string using loops, as strings are arrays of characters:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello";
for(int i = 0; i < strlen(str); i++) {
printf("Character %d: %c\n", i, str[i]);
}
return 0;
}
This prints each character with its index.
Loops make it possible to search, count, or modify characters in a string.
#include <stdio.h>
#include <string.h>
int main() {
char str[100];
int count = 0;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
for(int i = 0; i < strlen(str); i++) {
char ch = str[i];
if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u'||
ch=='A'||ch=='E'||ch=='I'||ch=='O'||ch=='U') {
count++;
}
}
printf("Number of vowels: %d\n", count);
return 0;
}
This program counts vowels in a string, demonstrating character-level processing.
Forgetting the null character (\0), causing undefined behavior.
Using scanf() for strings with spaces, which stops at the first space.
Modifying string literals directly, leading to runtime errors.
Not allocating enough space for the string and null character.
Using string functions without including <string.h>.
In C, strings are arrays of characters terminated with a null character. They allow programs to store, manipulate, and process text efficiently. By combining strings with loops, conditional statements, and standard string functions, you can perform operations like concatenation, comparison, and searching. Understanding strings is critical for building user interfaces, processing input, and handling textual data in C programs.
Write a program to read a string from the user and print it in reverse order.
Create a program to count the number of vowels in a user-entered string.
Write a program to concatenate two strings entered by the user.
Develop a program to compare two strings and print whether they are equal or not.
Write a program to find the length of a string without using strlen().
Create a program to convert all lowercase letters to uppercase in a given string.
Write a program to check if a string is a palindrome.
Develop a program to count the number of words in a string entered by the user.
Write a program to find the first occurrence of a character in a string.
Create a program to replace all spaces with underscores in a given string.