Introduction
C provides several useful string functions through the <string.h> header file. These functions make common string operations easier, such as finding length, copying strings, comparing strings, joining strings, and searching for characters. In this chapter, you will practice the most important C string functions with simple examples. The programs also show how these functions work so beginners can understand what happens instead of simply memorizing function names. String Functions in C Practice questions with solutions to help you understand the concepts.
Q1. Find the Length of a String Using strlen()
Problem Statement
Write a C program to find the length of a string using the strlen() function.
C Program
#include <stdio.h>
#include <string.h>
int main()
{
char name[] = "Programming";
int length = strlen(name);
printf("Length = %d", length);
return 0;
}
Sample Output
Length = 11
Explanation
The strlen() function is used to find the number of characters in a string.
strlen(name)
For:
Programming
there are 11 characters.
The null character '\0' is not included in the returned length.
The header file is required:
#include <string.h>
Concepts Covered
strlen()<string.h>- String length
- Character arrays
Q2. Copy One String Using strcpy()
Problem Statement
Write a C program to copy one string into another using strcpy().
C Program
#include <stdio.h>
#include <string.h>
int main()
{
char original[] = "C Programming";
char copy[50];
strcpy(copy, original);
printf("Original: %s\n", original);
printf("Copy: %s", copy);
return 0;
}
Sample Output
Original: C Programming
Copy: C Programming
Explanation
The syntax of strcpy() is:
strcpy(destination, source);
In this example:
strcpy(copy, original);
means:
original → copy
The contents of original are copied into copy, including the terminating '\0'.
The destination array must have enough space to hold the copied string.
Concepts Covered
strcpy()- String copying
- Source and destination
<string.h>
Q3. Compare Two Strings Using strcmp()
Problem Statement
Write a C program to compare two strings using strcmp().
C Program
#include <stdio.h>
#include <string.h>
int main()
{
char first[] = "apple";
char second[] = "apple";
int result = strcmp(first, second);
if (result == 0)
{
printf("Strings are equal.");
}
else
{
printf("Strings are not equal.");
}
return 0;
}
Sample Output
Strings are equal.
Explanation
The strcmp() function compares two strings.
strcmp(first, second)
Its return value is:
0 → strings are equal
< 0 → first string comes before second
> 0 → first string comes after second
For example:
strcmp("apple", "apple")
returns 0.
Therefore:
if (result == 0)
is true.
Concepts Covered
strcmp()- String comparison
- Return values
if-else
Q4. Join Two Strings Using strcat()
Problem Statement
Write a C program to join two strings using the strcat() function.
C Program
#include <stdio.h>
#include <string.h>
int main()
{
char first[50] = "Hello ";
char second[] = "World";
strcat(first, second);
printf("Result: %s", first);
return 0;
}
Sample Output
Result: Hello World
Explanation
strcat() means string concatenation.
The syntax is:
strcat(destination, source);
Here:
strcat(first, second);
adds second to the end of first.
Before:
first = Hello
second = World
After:
first = Hello World
The destination array must have enough unused space for the final string.
Concepts Covered
strcat()- String concatenation
- Destination array
- Source string
Q5. Convert a String to Lowercase Using tolower()
Problem Statement
Write a C program to convert uppercase letters in a string to lowercase using tolower().
C Program
#include <stdio.h>
#include <ctype.h>
int main()
{
char text[] = "HELLO WORLD";
int i = 0;
while (text[i] != '\0')
{
text[i] = tolower((unsigned char)text[i]);
i++;
}
printf("Lowercase: %s", text);
return 0;
}
Sample Output
Lowercase: hello world
Explanation
tolower() converts an uppercase alphabetic character to lowercase.
For example:
'A' → 'a'
'B' → 'b'
'C' → 'c'
The function is provided by:
#include <ctype.h>
The loop processes each character until it reaches '\0'.
The cast:
(unsigned char)text[i]
is a safe way to pass a potentially signed char to the character-classification functions.
Concepts Covered
tolower()<ctype.h>- String traversal
- Character conversion
Q6. Convert a String to Uppercase Using toupper()
Problem Statement
Write a C program to convert lowercase letters in a string to uppercase using toupper().
C Program
#include <stdio.h>
#include <ctype.h>
int main()
{
char text[] = "hello world";
int i = 0;
while (text[i] != '\0')
{
text[i] = toupper((unsigned char)text[i]);
i++;
}
printf("Uppercase: %s", text);
return 0;
}
Sample Output
Uppercase: HELLO WORLD
Explanation
The toupper() function converts a lowercase alphabetic character to uppercase.
For example:
'a' → 'A'
'b' → 'B'
'c' → 'C'
The program visits every character and converts it where applicable.
Spaces are not changed.
Concepts Covered
toupper()<ctype.h>- String traversal
- Character conversion
Q7. Find the First Occurrence of a Character Using strchr()
Problem Statement
Write a C program to find the first occurrence of a character in a string using strchr().
C Program
#include <stdio.h>
#include <string.h>
int main()
{
char text[] = "programming";
char search = 'g';
char *result = strchr(text, search);
if (result != NULL)
{
printf("Character found.");
}
else
{
printf("Character not found.");
}
return 0;
}
Sample Output
Character found.
Explanation
The strchr() function searches for the first occurrence of a character in a string.
Syntax:
strchr(string, character);
Here:
strchr(text, search);
searches for:
'g'
inside:
programming
If the character is found, strchr() returns a pointer to its location.
If it is not found, it returns:
NULL
Concepts Covered
strchr()- Character searching
- Pointers
NULL<string.h>
Q8. Find a Substring Using strstr()
Problem Statement
Write a C program to check whether a particular word or substring exists inside another string using strstr().
C Program
#include <stdio.h>
#include <string.h>
int main()
{
char text[] = "I am learning C programming.";
char search[] = "C programming";
char *result = strstr(text, search);
if (result != NULL)
{
printf("Substring found.");
}
else
{
printf("Substring not found.");
}
return 0;
}
Sample Output
Substring found.
Explanation
strstr() searches for one string inside another string.
Syntax:
strstr(main_string, search_string);
Here:
strstr(text, search);
searches for:
C programming
inside:
I am learning C programming.
If the substring exists, the function returns a pointer to its first character.
If it doesn’t exist, it returns NULL.
Concepts Covered
strstr()- Substring searching
- Pointers
NULL
Q9. Read a Full Sentence and Find Its Length
Problem Statement
Write a C program that takes a sentence from the user and finds its length using strlen().
C Program
#include <stdio.h>
#include <string.h>
int main()
{
char sentence[100];
printf("Enter a sentence: ");
fgets(sentence, sizeof(sentence), stdin);
sentence[strcspn(sentence, "\n")] = '\0';
printf("Length = %zu", strlen(sentence));
return 0;
}
Sample Output
Enter a sentence: I love C programming
Length = 20
Explanation
We use:
fgets(sentence, sizeof(sentence), stdin);
because the sentence may contain spaces.
When fgets() reads a line, it can also store the newline character '\n' if there is room.
This line removes that newline:
sentence[strcspn(sentence, "\n")] = '\0';
Then:
strlen(sentence)
returns the number of characters in the sentence, excluding the terminating '\0'.
Notice that strlen() returns a size_t, so %zu is the appropriate printf() format specifier.
Concepts Covered
fgets()strlen()strcspn()- Strings with spaces
size_t
Q10. Create a Small Student Name Program Using Multiple String Functions
Problem Statement
Write a C program that takes a student’s first name and last name, joins them, and displays the complete name and its length.
C Program
#include <stdio.h>
#include <string.h>
int main()
{
char firstName[50];
char lastName[50];
char fullName[100];
printf("Enter first name: ");
scanf("%49s", firstName);
printf("Enter last name: ");
scanf("%49s", lastName);
strcpy(fullName, firstName);
strcat(fullName, " ");
strcat(fullName, lastName);
printf("\nFull Name: %s\n", fullName);
printf("Name Length: %zu", strlen(fullName));
return 0;
}
Sample Output
Enter first name: Rahul
Enter last name: Kumar
Full Name: Rahul Kumar
Name Length: 11
Explanation
This program combines multiple string functions.
First, the first name is copied into fullName:
strcpy(fullName, firstName);
Then a space is added:
strcat(fullName, " ");
Finally, the last name is added:
strcat(fullName, lastName);
The final string becomes:
Rahul Kumar
Then strlen() calculates its length.
The program demonstrates how different string functions can be combined to solve a practical problem.
Concepts Covered
strcpy()strcat()strlen()- Multiple string operations
- Practical string handling
Key Takeaways
- String functions make common string operations easier in C.
- Most C string functions are available through
<string.h>. strlen()finds the number of characters in a string.strcpy()copies one string into another.strcmp()compares two strings.strcat()joins one string to another.strchr()searches for a character.strstr()searches for a substring.strcspn()can help remove the newline read byfgets().toupper()andtolower()are available through<ctype.h>.- String destination arrays must have enough space when using copying or concatenation functions.
strcmp()returns0when two strings are equal.- Understanding manual string operations is useful even when using library functions.
- Always remember that a C string ends with
'\0'.
FAQs
1. Which header file is used for C string functions?
Most standard C string functions are declared in:
#include <string.h>
2. What does strlen() do in C?
strlen() returns the number of characters in a string, excluding the terminating '\0'.
Example:
strlen("Hello")
returns 5.
3. What is the difference between strcpy() and strcat()?
strcpy() copies a string:
strcpy(destination, source);
strcat() appends one string to another:
strcat(destination, source);
4. How do you compare two strings in C?
Use strcmp():
if (strcmp(first, second) == 0)
{
printf("Equal");
}
5. What does strcmp() return when strings are equal?
It returns 0 when the two strings are equal.
6. What is the difference between strchr() and strstr()?
strchr() searches for a single character.
strchr(text, 'a');
strstr() searches for a string or substring.
strstr(text, "program");
7. Why does strcat() require enough space in the destination array?
strcat() adds the source string to the destination string. The destination must have enough available space for the original contents, appended contents, and terminating '\0'. It does not automatically allocate additional memory.
Written by Shubhranshu Shekhar, who has trained 20000+ students in coding.
