Back to C Programming
2026-07-136 min read

Commonly used string functions

Learn Commonly used string functions step by step with clear examples and exercises.

Why This Matters

In C programming, string manipulation functions play a crucial role in handling user input, file operations, and data structures. Understanding these functions can help you solve complex problems more efficiently and prepare for coding interviews and exams.

Prerequisites

Before diving into the core concept, it's important to have a good understanding of the following topics:

  • Basic C syntax: variables, data types, operators, control structures (if-else, loops)
  • Arrays and pointers in C
  • File handling in C

Core Concept

In C programming, strings are treated as arrays of characters with a null character (\0) at the end. The standard library provides various functions to manipulate strings. Let's explore some commonly used string functions:

  1. strlen(char *str) - Returns the length of the string pointed to by str.
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
printf("Length of the given string: %d\n", strlen(str));
return 0;
}
  1. strcpy(char *dest, const char *src) - Copies the string pointed to by src, including the terminating null character, to the array pointed to by dest.
#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "Original String";
char str2[20];
strcpy(str2, str1);
printf("Copied string: %s\n", str2);
return 0;
}
  1. strcmp(const char *str1, const char *str2) - Compares the two strings lexicographically (alphabetically). Returns zero if the strings are equal, a positive value if str1 is greater than str2, and a negative value otherwise.
#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "Apple";
char str2[20] = "Banana";
int result = strcmp(str1, str2);
printf("Comparison result: %d\n", result);
return 0;
}
  1. strcat(char *dest, const char *src) - Concatenates the string pointed to by src to the end of the string pointed to by dest.
#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "Hello";
char str2[20] = "World";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}

String Function Variations

The C standard library also provides variations of these functions to handle specific cases:

  • strncpy(char *dest, const char *src, size_t n) - Copies at most n characters from src to dest. If src is longer than n, the null character (\0) is appended to dest.
#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "Original String";
char str2[10];
strncpy(str2, str1, 5);
printf("First 5 characters of the original string: %s\n", str2);
return 0;
}
  • strncat(char *dest, const char *src, size_t n) - Concatenates at most n characters from src to dest. If src is longer than n, only the first n characters are concatenated, and the null character (\0) is appended to dest.
#include <stdio.h>
#include <string.h>

int main() {
char str1[20] = "Hello";
char str2[20] = "World";
strncat(str1, str2, 3);
printf("First 3 characters of 'World' concatenated to 'Hello': %s\n", str1);
return 0;
}
  1. memchr(const void *s, int c, size_t n) - Searches for the first occurrence of character c in the array s of length n. Returns a pointer to the character if found, or NULL otherwise.
#include <stdio.h>
#include <string.h>

int main() {
char str[] = "Hello, World!";
char *ptr = memchr(str, 'l', strlen(str));
if (ptr != NULL) {
printf("The first occurrence of 'l' is at position %d\n", ptr - str + 1);
} else {
printf("Character not found in the string.\n");
}
return 0;
}

Worked Example

Let's create a simple program that reads a user-inputted string and checks whether it is a palindrome (reads the same forwards and backwards).

#include <stdio.h>
#include <string.h>

int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);

int len = strlen(str) - 1;
for (int i = 0; i <= len / 2; ++i) {
if (str[i] != str[len - i]) {
printf("The given string is not a palindrome.\n");
return 0;
}
}
printf("The given string is a palindrome.\n");
return 0;
}

Common Mistakes

  1. Forgetting to include the header file ``.
  2. Not accounting for the null character (\0) when comparing or manipulating strings.
  3. Overflowing the destination array when concatenating or copying strings.
  4. Assuming that string functions modify the original string, whereas they return a new string.
  5. Using strcmp to check if two strings are identical instead of comparing their lengths (==).
  6. Failing to handle edge cases such as empty strings or strings with only one character.
  7. Incorrectly handling strings with odd lengths when checking for palindromes.

Common Mistakes - Subheadings

  • Forgetting to include the header file ``
  • Not accounting for the null character (\0)
  • Overflowing destination arrays
  • Assuming string functions modify original strings
  • Using strcmp for identity checks instead of length comparisons
  • Handling empty and single-character strings correctly
  • Properly handling palindromes with odd lengths

Practice Questions

  1. Write a program that finds the longest common substring between two given strings.
  2. Implement a function that reverses a given string in C.
  3. Create a program that counts the number of occurrences of a specific character in a given string.
  4. Write a function that checks if a given string is a permutation of another given string.
  5. Implement a program that removes all duplicate characters from a given string.
  6. Write a function to find the first non-repeating character in a string.
  7. Create a program that replaces all occurrences of a specific substring with another substring in a given string.
  8. Implement a function that checks if a given string is a rotation of another given string (e.g., "waterbottle" and "erbottlewat").
  9. Write a program that finds the shortest possible word that can be formed by rearranging the letters in a given word.
  10. Implement a function to find all permutations of a given string.

FAQ

Why do we need to include `` in our C programs?

  • The `` header file provides various functions for manipulating strings, such as copying, comparing, and concatenating strings.

What happens if I don't include the null character (\0) at the end of a string in C?

  • Failing to terminate a string with a null character can lead to unexpected behavior when using functions like strlen or strcpy. The program may not work correctly, and it could even cause segmentation faults.

Can I use pointers to manipulate strings directly in C?

  • Yes, you can manipulate strings using pointers in C. However, it's generally recommended to use the functions provided by the standard library for ease of use and safety.

What is the difference between strcpy and strncpy in C?

  • The main difference lies in how they handle the null character (\0). While strcpy copies the entire source string, including the null character, strncpy only copies a specified number of characters from the source string to the destination array. If the source string does not contain a null character before the end of the copied characters, strncpy will not append a null character to the destination array.

How can I compare two strings for equality in C without using strcmp?

  • You can use the equality operator (==) to check if two strings are identical by comparing their lengths and characters one by one. However, this method is less efficient than using strcmp. If you need to compare strings frequently, consider using the strcmp function for better performance.

Why should I be careful when handling string functions in C?

  • String functions in C can lead to unexpected behavior if not used correctly. For example, forgetting to include the null character (\0) or not checking for buffer overflows can cause segmentation faults and security vulnerabilities. It's essential to understand how these functions work and use them carefully.