Understanding string basics is important because many programs use text input and output operations.
Table of Contents
What is a String in C?
A string in C is a collection of characters stored inside an array and terminated by a special character called the null character (\0).Example:
Memory representation:char name[] = "Hello";
| Index | Value |
|---|---|
| 0 | H |
| 1 | e |
| 2 | l |
| 3 | l |
| 4 | o |
| 5 | \0 |
The null character marks the end of the string.
Total memory required:
H = 1 byte
e = 1 byte
l = 1 byte
l = 1 byte
o = 1 byte
Null character = 1 byte
Total = 6 bytes
Syntax of String Declaration
Method 1: Using Character ArrayMethod 2: Character by Characterchar city[10] = "Delhi";
Both methods create the same string.char city[] = {'D','e','l','h','i','\0'};
Example:
// C program for string declaration
#include stdio.h
int main()
{
char name[] = "Welcome to TutorialforGeeks";
printf("%s", name);
return 0;
}
Output:
Explanation:Welcome to TutorialforGeeks
The %s format specifier is used to print strings in C. The string is stored in a character array and displayed using printf().
Taking String Input
Below are the two methods to take user input string:1. Using scanf()
Below is the C program to take using input string using scanf():
// C program for take string
// input using scanf()
#include stdio.h
int main()
{
char name[20];
printf("Enter name: ");
scanf("%s", name);
printf("Name: %s", name);
return 0;
}
Output:
Limitation: scanf() reads only one word and stops at spaces.Enter name: TutorialforGeeks
Name: TutorialforGeeks
2. Using fgets()
Below is the C program to take user input using fgets():
// C program for take string
// input using fgets()
#include stdio.h
int main()
{
char message[50];
printf("Enter message: ");
fgets(message, sizeof(message), stdin);
printf("%s", message);
return 0;
}
Output:
fgets() can read complete sentences including spaces.Enter message: Welcome to Python Programming
Welcome to Python Programming
Common String Operations
String operations are used to manipulate and process text stored in character arrays. C provides many predefined functions in the string.h library that make string handling easier and faster.| String Operation | Function | Description |
|---|---|---|
| String Length | strlen() | Finds the total number of characters in a string excluding the null character. |
| String Copy | strcpy() | Copies one string into another string. |
| String Concatenation | strcat() | Joins two strings into a single string. |
| String Comparison | strcmp() | Compares two strings and returns the difference result. |
| String Reverse | strrev() | Reverses the characters of a string. |
| Character Search | strchr() | Searches for a specific character in a string. |
| Substring Search | strstr() | Searches for a substring inside another string. |
| String Tokenization | strtok() | Splits a string into smaller tokens using delimiters. |
| Uppercase Conversion | strupr() | Converts all lowercase characters into uppercase. |
| Lowercase Conversion | strlwr() | Converts all uppercase characters into lowercase. |
Key Features of Strings
- Strings Use Character Arrays: Strings are stored as arrays of characters rather than a separate string data type.
- Null Character Ends the String: Every string automatically ends with \0, which helps identify where the string finishes.
- Strings Can Store Text Data: Strings are useful for storing names, addresses, passwords, and messages.
- Fixed Memory Allocation: The array size decides how many characters can be stored.
- Strings Support Library Functions: C provides many predefined functions through string.h.
Common Mistakes While Using Strings
1. Forgetting Null Character: Missing \0 may produce incorrect output because C will not know where the string ends.2. Declaring Small Array Size: If the array is too small, data may overflow.
Wrong example:
3. Using scanf() for Full Sentences: scanf() stops reading after space characters.char name[3] = "Hello";
4. Accessing Invalid Indexes: Reading beyond array limits may cause errors.
5. Not Including string.h: String functions require the header file #include string.h.
Advantages of Strings
- Easy text handling: Strings help process text based information efficiently.
- Supports multiple operations: Copying, joining, and comparing become easier with strings in C.
- Memory efficient: Strings use character arrays and occupy only required memory.
- Flexible storage: Developers can define custom array sizes.
- Library support: Many built in functions simplify string processing.
Conclusion
Strings are an important concept in C programming because they allow programs to handle text data. In C, strings are implemented using character arrays and end with a null character. Understanding declaration methods, input techniques, memory storage, and common mistakes helps build a strong foundation before learning advanced string operations and string functions.Frequently Asked Questions
1. What is a string in C?2. Why is \0 used in strings?A string is a collection of characters stored inside a character array and terminated with \0.
3. Which header file is used for string functions?The null character marks the end of the string.
4. Which function reads complete sentences?The string.h header file contains predefined string functions.
5. Does C have a built in string data type?fgets() reads complete lines including spaces.
No, C uses character arrays to represent strings.
0 Comments