In this article String in C we give the information about String is a sequence of characters and it is terminated by null (‘\0’). Strings are always kept inside double quotes.
String in C
Introduction
In C programming, a string is a sequence of characters that ends with a null character (‘\0’).
This null character indicates the end of the string.
Definition:
“A string is a one-dimensional array of characters terminated by a null character (‘\0’).”
Example:
char c[] = “yash”;
Here, the compiler automatically appends the null character \0 at the end of “yash”.
Difference between Character and String
| Character | String |
| Enclosed in single quotes | Enclosed in double quotes |
| Example: ‘A’ | Example: “A” |
Each character in the string occupies one byte of memory.
Declaration of String in C
A string is declared as an array of characters using the following syntax:
char str_name[size];
- str_name → name of the string variable
- size → number of characters to be stored (including \0)
Example:
char name[20];
Initialization of String
Strings can be initialized in several ways:
char str[] = “Rajveer”;
char str[50] = “Rajveer”;
char str[] = {‘R’,’a’,’j’,’v’,’e’,’e’,’r’,’\0′};
char str[50] = {‘R’,’a’,’j’,’v’,’e’,’e’,’r’,’\0′};
Memory Representation
For char str[] = “Rajveer”;
| R | a | j | v | e | e | r | \0 |
The null character \0 marks the end of the string.
Simple String Program
#include <stdio.h>
#include <conio.h>
void main()
{
// declare and initialize string
char str[50] = “easyconcept.in”;
// print string
printf(“%s”, str);
getch();
}
Output:
easyconcept.in
String Functions in C
All standard string functions are declared in the string.h header file.
| Function | Description |
| strcat(s1, s2) | Concatenates string s2 to the end of s1. |
| strcpy(s1, s2) | Copies string s2 into s1. |
| strlen(s1) | Returns the length of s1. |
| strcmp(s1, s2) | Compares two strings. Returns 0 if equal. |
| strdup(s1) | Duplicates the given string. |
| strlwr(s1) | Converts the string to lowercase. |
| strupr(s1) | Converts the string to uppercase. |
| strrev(s1) | Reverses the string. |
Example: String Functions Program
// C Program to demonstrate String Functions
#include <stdio.h>
#include <conio.h>
#include <string.h>
void main()
{
char str1[15], str2[15], str3[20];
clrscr();
printf(“\n Enter the first string: “);
scanf(“%s”, str1);
printf(“\n Enter the second string: “);
scanf(“%s”, str2);
printf(“\n Length of first string: %d”, strlen(str1));
printf(“\n Lowercase: %s”, strlwr(str1));
printf(“\n Uppercase: %s”, strupr(str1));
printf(“\n Concatenation: %s”, strcat(str1, str2));
printf(“\n Reverse: %s”, strrev(str1));
printf(“\n Copy (str1 to str3): %s”, strcpy(str3, str1));
getch();
}
Output:
Enter the first string: Ram
Enter the second string: Sham
Length of first string: 3
Lowercase: ram
Uppercase: RAM
Concatenation: RAMSham
Reverse: mahSMAR
Copy (str1 to str3): mahSMAR
Key Points:
- Strings are terminated with a null character (‘\0’).
- Declared using char arrays.
- Must include <string.h> to use string functions.
- Each function helps in string manipulation easily.
Some More:
POP- Introduction to Programming Using ‘C’
OOP – Object Oriented Programming
DBMS – Database Management System
RDBMS – Relational Database Management System
Join Now: Data Warehousing and Data Mining