Strings
In C, a string is a sequence of characters stored in contiguous memory, terminated by a special null character ('\0'). Unlike modern languages (like Python or Java) that have a built-in string type, C uses character arrays to represent strings.
Example:
Section titled “Example:”char str[] = "Hello";Memory representation:
Section titled “Memory representation:”| Index | Value |
|---|---|
| 0 | H |
| 1 | e |
| 2 | l |
| 3 | l |
| 4 | o |
| 5 | \0 |
- Every string in C must end with
'\0'.
Characteristics of Strings:
Section titled “Characteristics of Strings:”- Stored in contiguous memory.
- Terminated with null character.
- Can be manipulated using
<string.h>library functions. - Must allocate enough space for all characters plus one for
'\0'.
Ways to Declare and Initialize Strings
Section titled “Ways to Declare and Initialize Strings”String Literal
Section titled “String Literal”char str[] = "Hello";- Compiler adds
'\0'automatically.
Character Array
Section titled “Character Array”char str[6] = {'H', 'e', 'l', 'l', 'o', '\0'};Specifying Extra Size
Section titled “Specifying Extra Size”char str[20] = "Hello"; // extra space available- Always allocate an extra byte for ‘\0’.
Input and Output of Strings
Section titled “Input and Output of Strings”Using printf and scanf
Section titled “Using printf and scanf”char name[20];scanf("%s", name); // stops at spaceprintf("Name: %s", name);Using gets()
Section titled “Using gets()”gets(name);Using fgets()
Section titled “Using fgets()”fgets(name, sizeof(name), stdin);- Reads full line, including spaces.
- Safer than
gets().
Remove trailing newline:
Section titled “Remove trailing newline:”name[strcspn(name, "\n")] = '\0';String Output
Section titled “String Output”printf("%s", str);puts(str); //adds newline automatically.- Pointer points to string literal in read-only memory.
- Modifying this is undefined behavior.
String Handling Functions (<string.h>)
Section titled “String Handling Functions (<string.h>)”| Function | Description |
|---|---|
strlen(s) | Length of string (without '\0'). |
strcpy(dest, src) | Copy string. |
strncpy() | Copy up to n characters. |
strcat() | Append one string to another. |
strcmp(s1, s2) | Compare two strings. |
strchr() | Find first occurrence of a character. |
strstr() | Find substring. |
- Always include:
#include <string.h>Examples of String Functions
Section titled “Examples of String Functions”String Length
Section titled “String Length”#include <stdio.h>#include <string.h>int main() { char str[] = "C Programming"; printf("Length: %zu", strlen(str)); return 0;}Length: 13Copy String
Section titled “Copy String”char src[] = "Hello";char dest[20];strcpy(dest, src);Concatenate Strings
Section titled “Concatenate Strings”char str1[20] = "Hello ";char str2[] = "World";strcat(str1, str2);Compare Strings
Section titled “Compare Strings”if(strcmp("abc", "abc") == 0)printf("Equal");Manual Implementation of String Functions
Section titled “Manual Implementation of String Functions”strlen()
Section titled “strlen()”int my_strlen(char *str) { int length = 0; while(str[length] != '\0') length++; return length;}Strings vs Character Arrays
Section titled “Strings vs Character Arrays”| Aspect | String Literal | Character Array |
|---|---|---|
| Mutability | Cannot modify (read-only) | Can modify |
| Storage | Stored in text segment | Stored in stack or heap |
| Null Required | Yes | Yes |
Advantages
Section titled “Advantages”- Efficient representation of text.
- Wide library support.
Disadvantages
Section titled “Disadvantages”- Manual memory management.
- Risk of buffer overflow if not careful.
Real-World Applications
Section titled “Real-World Applications”- Text processing (editors, compilers).
- File paths handling.
- Command-line argument parsing.
- Network protocols (message parsing).
Example
Section titled “Example”#include <stdio.h>#include <string.h>#include <ctype.h>
int main() { char str[100]; int count = 0; printf("Enter a string: "); fgets(str, sizeof(str), stdin);
for(int i = 0; str[i] != '\0'; i++) { char ch = tolower(str[i]); if(ch=='a'||ch=='e'||ch=='i'||ch=='o'||ch=='u') count++;}
printf("Vowel count: %d\n", count);return 0;}Enter a string: helloVowel count: 2