C Programming
In this tutorial we will learn about string manipulation in C programming language.
We have already covered how to create and initialise string in the previous tutorial. So, we know that a string is a sequence of characters enclosed in a double quotes.
We save a string in a character array and we know that array elements are indexed. So, we can access them using their index number.
In the following example we have a variable str
which holds a string "Hello" and we will print each character in the string.
Strings ends with a NULL \0
character in C.
The trick here is to keep printing the characters as long as we don't hit the NULL character which marks the end of a string in C.
So, lets write a C program.
#include <stdio.h>
int main(void)
{
//variable
char str[] = "Hello";
int i;
//output
i = 0;
while(str[i] != '\0') {
printf("%c\n", str[i]);
i++;
}
printf("End of code\n");
return 0;
}
Output
H
e
l
l
o
End of code
To find the total number of characters in a string we use the strlen()
function which is from the string.h
header file.
Syntax of finding the length of a string is given below.
int len = strlen(str);
Where, str
is the name of the variable holding the string.
The following code will print 5 as there are five characters in the string "Hello".
#include <stdio.h>
#include <string.h>
int main(void)
{
//variable
char str[] = "Hello";
printf("%ld", strlen(str)); //this will give us 5
return 0;
}
To compare two strings in C we use the strcmp()
method from the string.h
header file.
The strcmp()
function compares two strings lexicographically and will return an integer value.
We can't use (str1 == str2) or ("Hello" == "Hi") to compare strings.
In the following example we will take two strings as input from the user and check if they are equal or not.
#include <stdio.h>
#include <string.h>
int main(void)
{
//variable
char str1[100], str2[100];
//input
printf("Enter string 1: ");
gets(str1);
printf("Enter string 2: ");
gets(str2);
if (strcmp(str1, str2) == 0) {
printf("Both are same.\n");
}
else {
printf("Both are different.\n");
}
printf("End of code\n");
return 0;
}
Output: Same string
Enter string 1: Apple
Enter string 2: Apple
Both are same.
End of code
Output: Different string
Enter string 1: Apple
Enter string 2: Mango
Both are different.
End of code
We use the concat()
function from the string.h
header file to concatenate two strings.
In the following example we will concatenate "Hello" and "World".
#include <stdio.h>
#include <string.h>
int main(void)
{
//variable
char
str1[] = "Hello",
str2[] = "World",
str3[100] = "";
//concat
strcat(str3, str1); //concat "Hello" to str3 so, str3 = "Hello"
strcat(str3, " "); //concat " " to str3 so, str3 = "Hello "
strcat(str3, str2); //concat "World" to str3 so, str3 = "Hello World"
printf("Concatenated string: %s\n", str3);
printf("End of code\n");
return 0;
}
Output
Concatenated string: Hello World
End of code
To reverse a string all we have to do is swap the last character with the first character, the second last character with the second character and so on till we reach the middle of the string.
So, if we have a string "Hello" then its reverse is "olleH".
In the following example we will take a string having less than 100 characters from user and will reverse it.
#include <stdio.h>
#include <string.h>
int main(void)
{
//variable
char str[100], tmp;
int i, len, mid;
//input
printf("Enter a string: ");
gets(str);
//find number of characters
len = strlen(str);
mid = len/2;
//reverse
for (i = 0; i < mid; i++) {
tmp = str[len - 1 - i];
str[len - 1 - i] = str[i];
str[i] = tmp;
}
//output
printf("Reversed string: %s\n", str);
printf("End of code\n");
return 0;
}
Output
Enter a string: Hello World
Reversed string: dlroW olleH
End of code
ADVERTISEMENT