A string in C is a sequence of characters terminated by a null character ('\0'). Strings are stored in character arrays.
For example, the string "Hello" is stored as:
H e l l o \0
1. Declaration of a String
Syntax
char string_name[size];
Example
char name[20];
This declares a string that can store up to 19 characters plus the null character ('\0').
2. Initialization of a String
Method 1: Using String Literal
char name[] = "Pradeep";
Method 2: Using Character Array
char name[] = {'P','r','a','d','e','e','p','\0'};
3. Reading and Displaying Strings
Using scanf() and printf()
#include <stdio.h>
int main()
{
char name[20];
printf("Enter your name: ");
scanf("%s", name);
printf("Hello, %s", name);
return 0;
}
Output
Enter your name: RahulHello, Rahul
Note:
scanf("%s", name)reads only one word (stops at the first space).
4. Using fgets() to Read a Full Line
fgets() can read a string containing spaces.
#include <stdio.h>
int main()
{
char name[50];
printf("Enter your full name: ");
fgets(name, sizeof(name), stdin);
printf("Name: %s", name);
return 0;
}
Sample Output
Enter your full name: Rahul KumarName: Rahul Kumar
5. Common String Functions (<string.h>)
To use string functions, include:
#include <string.h>
| Function | Description |
|---|---|
strlen() | Returns the length of a string |
strcpy() | Copies one string to another |
strcat() | Concatenates (joins) two strings |
strcmp() | Compares two strings |
strchr() | Finds the first occurrence of a character |
strstr() | Finds a substring in a string |
6. strlen() Example
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "Programming";
printf("Length = %lu", strlen(str));
return 0;
}
Output
Length = 11
7. strcpy() Example
#include <stdio.h>
#include <string.h>
int main()
{
char source[] = "C Language";
char destination[20];
strcpy(destination, source);
printf("%s", destination);
return 0;
}
Output
C Language
8. strcat() Example
#include <stdio.h>
#include <string.h>
int main()
{
char str1[30] = "Hello ";
char str2[] = "World";
strcat(str1, str2);
printf("%s", str1);
return 0;
}
Output
Hello World
9. strcmp() Example
#include <stdio.h>
#include <string.h>
int main()
{
char str1[] = "Apple";
char str2[] = "Apple";
if(strcmp(str1, str2) == 0)
printf("Strings are Equal");
else
printf("Strings are Not Equal");
return 0;
}
Output
Strings are Equal
10. Example Program
#include <stdio.h>
#include <string.h>
int main()
{
char name[30];
printf("Enter your name: ");
fgets(name, sizeof(name), stdin);
printf("Your Name: %s", name);
printf("Length: %lu", strlen(name));
return 0;
}
Sample Output
Enter your name: Priya SharmaYour Name: Priya SharmaLength: 13
Note: If
fgets()reads the newline (\n),strlen()includes it in the count. You can remove the newline if needed before processing the string.
Difference Between Character Array and String
| Character Array | String |
|---|---|
| Stores individual characters | Stores a sequence of characters ending with '\0' |
| May or may not be null-terminated | Always null-terminated |
Example: {'A', 'B', 'C'} | Example: "ABC" |
Advantages of Strings
- Easy to store and manipulate text.
- Built-in library functions simplify string operations.
- Widely used for names, addresses, messages, and file handling.
Category: MCA
