C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In this tutorial, we'll explore some of the string processing functions in the C programming language, focusing on functions available in the string.h
header. We'll cover the basic syntax and provide examples to demonstrate their usage.
Including the string.h
Header
Before using the string processing functions, you should include the string.h
header at the beginning of your C program:
#include <string.h>
Common String Processing Functions
Here are some of the most commonly used string processing functions available in the string.h
header:
strlen()
: Returns the length of a string.size_t strlen(const char *str);
strcpy()
: Copies a string from a source to a destination.char *strcpy(char *dest, const char *src);
strncpy()
: Copies a specified number of characters from a source string to a destination.char *strncpy(char *dest, const char *src, size_t n);
strcat()
: Appends a source string to a destination string.char *strcat(char *dest, const char *src);
strncat()
: Appends a specified number of characters from a source string to a destination string.char *strncat(char *dest, const char *src, size_t n);
strcmp()
: Compares two strings lexicographically and returns an integer indicating their relationship.int strcmp(const char *str1, const char *str2);
strncmp()
: Compares a specified number of characters of two strings lexicographically and returns an integer indicating their relationship.int strncmp(const char *str1, const char *str2, size_t n);
Examples
Here are some examples demonstrating the use of these string processing functions in C:
strlen()
:#include <stdio.h> #include <string.h> int main() { char str[] = "Hello, World!"; size_t length = strlen(str); printf("Length of the string: %zu\n", length); return 0; }
strcpy()
:#include <stdio.h> #include <string.h> int main() { char src[] = "Hello, World!"; char dest[50]; strcpy(dest, src); printf("Destination string: %s\n", dest); return 0; }
strcat()
:#include <stdio.h> #include <string.h> int main() { char dest[50] = "Hello, "; char src[] = "World!"; strcat(dest, src); printf("Resulting string: %s\n", dest); return 0; }
strcmp()
:#include <stdio.h> #include <string.h> int main() { char str1[] = "apple"; char str2[] = "banana"; int result = strcmp(str1, str2); if (result < 0) { printf("%s comes before %s\n", str1, str2); } else if (result > 0) { printf("%s comes after %s\n", str1, str2); } else { printf("%s and %s are equal\n", str1, str2); } return 0; }
Standard string functions in the C library:
strlen
, strcpy
, strcat
, and strcmp
.#include <stdio.h> #include <string.h> int main() { char str1[] = "Hello"; char str2[] = "World"; // Using standard string functions printf("Length: %zu\n", strlen(str1)); strcpy(str1, str2); strcat(str1, "!"); printf("Concatenated: %s\n", str1); return 0; }
Manipulating and modifying strings in C language:
#include <stdio.h> #include <string.h> int main() { char source[] = "Hello"; char destination[20]; // Copying strings strcpy(destination, source); printf("Copied: %s\n", destination); // Modifying strings strcat(destination, " World!"); printf("Modified: %s\n", destination); return 0; }
Commonly used string handling functions in C:
strlen
, strcpy
, strcmp
, strcat
, strchr
, and strstr
.#include <stdio.h> #include <string.h> int main() { char str[] = "Hello, World"; // Using commonly used string functions printf("Length: %zu\n", strlen(str)); printf("First occurrence of 'o': %s\n", strchr(str, 'o')); printf("Substring 'World': %s\n", strstr(str, "World")); return 0; }
String input/output functions in C programming:
fgets
for string input and puts
or printf
for output.#include <stdio.h> int main() { char buffer[50]; // String input printf("Enter a string: "); fgets(buffer, sizeof(buffer), stdin); // String output printf("You entered: %s\n", buffer); return 0; }
Searching and comparing strings using C functions:
strcmp
for string comparison and strchr
for searching characters.#include <stdio.h> #include <string.h> int main() { char str1[] = "Hello"; char str2[] = "World"; // Comparing strings if (strcmp(str1, str2) == 0) { printf("Strings are equal.\n"); } else { printf("Strings are not equal.\n"); } // Searching for a character if (strchr(str1, 'o') != NULL) { printf("'o' found in the string.\n"); } else { printf("'o' not found in the string.\n"); } return 0; }
Concatenating and splitting strings in C language:
strcat
for concatenation and various methods for splitting, such as strtok
.#include <stdio.h> #include <string.h> int main() { char str1[20] = "Hello"; char str2[] = "World"; // Concatenating strings strcat(str1, " "); strcat(str1, str2); printf("Concatenated: %s\n", str1); // Splitting strings char *token = strtok(str1, " "); while (token != NULL) { printf("Token: %s\n", token); token = strtok(NULL, " "); } return 0; }
String manipulation with pointers in C programming:
#include <stdio.h> int main() { char str[] = "Hello"; char *ptr = str; // Using pointers for string manipulation while (*ptr != '\0') { printf("%c ", *ptr); ++ptr; } return 0; }
C code examples demonstrating string processing functions:
#include <stdio.h> #include <string.h> int main() { char str[] = "Programming in C"; // Using string functions for processing printf("Length: %zu\n", strlen(str)); printf("Substring 'in': %s\n", strstr(str, "in")); return 0; }