C Language Strings Explained
1. What Is a String
In C, strings are stored as character arrays and terminated by a null character \0. The C standard library provides various functions for operating on strings. Strings are declared as follows:
char str1[] = "Hello, World!";
char str2[20] = "Hello, C!";
char str3[20]; // Only declared, not yet assigned
2. String Input and Output
In C, you can use printf and scanf functions to output and input strings.
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
scanf("%s", str); // Note: scanf can only read strings without spaces
printf("You entered: %s\n", str);
return 0;
}
Reading Strings with Spaces
If you need to read strings containing spaces, you can use the gets function or the fgets function.
#include <stdio.h>
int main() {
char str[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin); // fgets can read strings containing spaces
printf("You entered: %s\n", str);
return 0;
}
3. String Length
Use the strlen function to get the length of a string (excluding \0).
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
int length = strlen(str);
printf("Length of the string: %d\n", length);
return 0;
}
4. String Copy
Use the strcpy function to copy one string to another.
#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;
}
5. String Concatenation
Use the strcat function to concatenate two strings together.
#include <stdio.h>
#include <string.h>
int main() {
char str1[50] = "Hello, ";
char str2[] = "World!";
strcat(str1, str2);
printf("Concatenated string: %s\n", str1);
return 0;
}
6. String Comparison
Use the strcmp function to compare two strings. If equal, it returns 0; if the first is greater than the second, it returns a positive value; if the first is less than the second, it returns a negative value.
#include <stdio.h>
#include <string.h>
int main() {
char str1[] = "Hello";
char str2[] = "World";
int result = strcmp(str1, str2);
if (result == 0) {
printf("Strings are equal.\n");
} else if (result > 0) {
printf("str1 is greater than str2.\n");
} else {
printf("str1 is less than str2.\n");
}
return 0;
}
7. Substring Search
Use the strstr function to search for one string within another.
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello, World!";
char substr[] = "World";
char *position = strstr(str, substr);
if (position != NULL) {
printf("Substring found at position: %ld\n", position - str);
} else {
printf("Substring not found.\n");
}
return 0;
}
8. String Conversion
C provides some functions to convert strings to other data types, such as atoi (convert string to integer), atof (convert string to floating-point number), etc.
#include <stdio.h>
#include <stdlib.h>
int main() {
char str[] = "12345";
int num = atoi(str);
printf("Integer: %d\n", num);
return 0;
}
9. Common String Functions Summary
| Function | Description |
|---|---|
| strlen | Calculate the length of a string |
| strcpy | Copy a string |
| strncpy | Copy the first n characters of a string |
| strcat | Concatenate strings |
| strncat | Concatenate the first n characters of a string |
| strcmp | Compare strings |
| strncmp | Compare the first n characters of a string |
| strstr | Search for a substring within a string |
| strchr | Search for a character within a string |
| strrchr | Search for a character within a string from the end |
| strtok | Split a string |
| atoi | Convert a string to an integer |
| atof | Convert a string to a floating-point number |
| sprintf | Format a string and store it in another string |
| sscanf | Read formatted input from a string into variables |
| fgets | Read a line from a file or standard input into a string |
| fputs | Write a string to a file or standard output |
10. Example Program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main() {
// 1. String input and output
char str1[100];
printf("Enter a string: ");
fgets(str1, sizeof(str1), stdin);
printf("You entered: %s\n", str1);
// 2. String length
printf("Length of the string: %lu\n", strlen(str1));
// 3. String copy
char str2[100];
strcpy(str2, str1);
printf("Copied string: %s\n", str2);
// 4. String concatenation
char str3[100] = "Hello, ";
strcat(str3, str2);
printf("Concatenated string: %s\n", str3);
// 5. String comparison
char str4[] = "Hello";
int cmpResult = strcmp(str4, "Hello");
if (cmpResult == 0) {
printf("Strings are equal.\n");
} else {
printf("Strings are not equal.\n");
}
// 6. Substring search
char *pos = strstr(str3, "World");
if (pos) {
printf("Substring found at position: %ld\n", pos - str3);
} else {
printf("Substring not found.\n");
}
// 7. String conversion
char numStr[] = "12345";
int num = atoi(numStr);
printf("Converted number: %d\n", num);
return 0;
}