String Manipulation Functions
String Manipulation Functions
1
String Manipulation Functions
• The standard library ‘string.h’ contains many functions for the
string manipulation
String Functions Description of each function
strlen(str) Returns length of the string str
strcpy(dest,src) Copies the source string src to destination string dest
strncpy(dest,src,n) Copies n characters of the source string src to destination
string dest
strcat(s1,s2) Append string s2 to string s1
strncat(s1,s2) Append first n characters of string s2 to string s1
strcmp(s1,s2) Compare two strings s1 and s2
strncmp(s1,s2,n) Compare n characters of two strings s1 and s2
strrev(string) Reverse the given string
strupr(string) Used to convert the string to uppercase
strlwr(string) Used to convert the string to lowercase
2
String Manipulation Functions
strlen(str) and sizeof(): The length and size of a string
• The ‘strlen()’ function can be used to find the length of the
string in bytes.
• This function calculates the length of the string up to but
not including the ‘null character’.
Syntax: length=strlen(str);
Where, ‘str’ is a string.
‘length’ is an integer representing the length of ‘str’ in bytes
excluding the null character.
3
String Manipulation Functions
strlen(str) and sizeof(): The length and size of a string
#include<stdio.h>
#include<string.h>
void main()
{
char str[10]= “hello”;
int length;
length=strlen(str);
printf(“Length of the string is=%d\n”,length);
}
4
String Manipulation Functions
strlen(str) and sizeof(): The length and size of a string
• The ‘sizeof’ operator can be used to determine the size of a
declared string.
Syntax: sizeof(str);
#include<stdio.h>
#include<string.h>
void main()
{
char str1[25]= “RAMA”;
printf(“Size of string is=%d”,sizeof(str));
}
5
String Manipulation Functions
strlen(str) and sizeof(): The length and size of a string
#include<stdio.h>
#include<string.h>
void main()
{
char str[];
int length=0;
printf(“Enter the string\n”);
gets(str);
while(str[length]!=’\0’)
{
length ++;
}
printf(“Length of the string is=%d\n”, length);
}
6
String Manipulation Functions
strcpy ( ): String Copy
• The ‘strcpy()’ function copies the contents of source string src to destination
string dest including ‘\0’.
• The strcpy() function copies characters from the source string to the
destination string until it finds null character.
Syntax: strcpy(dest,src);
Ex:
#include<stdio.h>
#include<string.h>
void main()
{
char src[10]= “string”, dest[10];
strcpy(dest,src);
printf(“The Source String=%s\n The Destination String=%s”,src,dest);
}
7
String Manipulation Functions
strcpy ( ): String Copy
#include<stdio.h>
#include<string.h>
void main()
{
char src[100],dest[100];
int i;
printf(“Enter the string\n”);
gets(str);
for(i=0; str1[i]!=’\0’; i++)
{
str2[i] = str1[i];
}
str2[i]=’\0’;
printf(“copied string is=%d\n”, str2);
}
8
String Manipulation Functions
strncpy(dest,src,n): String Number Copy
• ‘strncpy()’ function is used to copy ‘n’ characters from the source
string src to the destination string dest.
Syntax: strncpy(dest,src,n);
Ex:
#include<stdio.h>
#include<string.h>
void main()
{
char src[10]= “Computer”, dest[10];
strncpy(dest,src,3);
printf (“The Source String=%s\n”,src);
printf (“The Destination String=%s”,dest);
} 9
String Manipulation Functions
strcat(s1,s2): String Concatenate(Joining two strings together)
• The ‘strcat()’ function is used to concatenate or join the two
strings.
• The ‘strcat()’ function copies all the characters of string s2 to the
end of string s1. The NULL character of string s1 is replaced by
the first character of s2.
Syntax: strcat(s1,s2);
10
String Manipulation Functions
strcat(s1,s2): String Concatenate(Joining two strings together)
#include<stdio.h>
#include<string.h>
void main()
{
char s1[15]= “Good”;
char s2[15]= “Morning”;
strcat(s1,s2);
printf(“The concatenated String=%s”,s1);
}
11
String Manipulation Functions
#include<stdio.h>
#include<string.h> for (i=0; s2[i]!=’\0’; i++)
void main() {
{ s3[i] = s2[i];
char s1[15]= “Good”; k = k +1;
char s2[15]= “Morning”, s3[20]; }
int k,i; s3[k] = ‘\0’;
for (i=0; s1[i]!=’\0’; i++) printf(“The concatenated String=%s”,s3);
{ }
s3[i] = s1[i];
k = k +1;
}
12
String Manipulation Functions
strncat(s1,s2,n)- String Number Concatenate
• The ‘strncat()’ function is used to concatenate or join n characters of
string s2 to the end of string s1.
Syntax: strncat(s1,s2,n);
Ex
#include<stdio.h>
#include<string.h>
void main()
{
char s1[15]= “Good”;
char s2[15]= “Morning”;
strncat(str1,str2,4);
printf(“The concatenated String=%s”,str1);
}
13
String Manipulation Functions
strcmp(s1,s2): String Compare
• This function is used to compare two strings.
• The comparison starts with first character of each string. The
comparison continues till the corresponding characters differ or
until the end of the character is reached.
• The following values are returned after comparison:
1. If two strings are equal, the function returns 0.
2. If s1 is greater than s2, a positive value is returned.
3. If s1 is less than s2, then the function returns a negative value.
Syntax: strcmp(s1,s2);
14
String Manipulation Functions
#include<stdio.h>
#include<string.h>
void main()
{
char s1[10]=”Hello”;
char s2[10]=”Hey”;
if(strcmp(s1,s2)==0)
printf(“The two strings are identical”);
else
printf(“The two strings are not identical”);
}
15
String Manipulation Functions
#include<stdio.h> else
#include<string.h> {
void main() for (i=0; s1[i]!=’\0’; i++)
{ {
int i; if(s1[i]!=s2[i])
char s1[10]=”Hello”; printf(“strings are different”);
char s2[10]=”Hey”; break;
len1 = strlen(s1); }
len2 = strlen(s2); }
if (strlen(s1) != strlen(s2)) printf(“The two strings are identical”);
printf(“The two strings are different”); }
16
String Manipulation Functions
strncmp(s1,s2,n): String Number Compare
• This function is used to compare first n number of characters in
two strings.
• The comparison starts with first character of each string. The
comparison continues till the corresponding characters differ or
until the end of the character is reached or specified numbers of
characters have been tested.
• The following values are returned after comparison:
1. If two strings are equal, the function returns 0.
2. If s1 is greater than s2, a positive value is returned.
3. If s1 is less than s2, then the function returns a negative value.
Syntax: strncmp(s1,s2,n);
17
String Manipulation Functions
#include<stdio.h>
#include<string.h>
void main()
{
char s1[10]=”Hello”;
char s2[10]=”Hey”;
if(strcmp(s1,s2,2)==0)
printf(“The first two characters in the strings are identical”);
else
printf(“The first two characters in the strings are not identical”);
}
18
String Manipulation Functions
Converting Characters of a String into Upper Case
• We have already discussed that in the memory ASCII codes are
stored instead of the real values.
• The ASCII code for A–Z varies from 65 to 91 and the ASCII
code for a–z ranges from 97 to 123. So, if we have to convert a
lower case character into uppercase, we just need to subtract 32
from the ASCII value of the character.
• The toupper() in C function defined in <ctype.h> header file
is used to convert lowercase alphabets to uppercase alphabets. It
does not change uppercase letters and special characters or
digits.
• If we want to convert all alphabets of a string from lowercase to
uppercase, we need to traverse over the characters of the string
one by one and apply toupper() function to each character.
19
String Manipulation Functions
Converting Characters of a String into Upper Case
Syntax: int toupper(int c);
Here c is the character to be converted from lowercase to
uppercase. The ASCII value of the character is passed to
the toupper() function.
20
String Manipulation Functions
#include <ctype.h>
#include <stdio.h>
int main()
{
char s[] = "Example";
int i = 0;
while(s[i]) {
putchar (toupper(s[i]));
i++;
}
return 0;
}
21
String Manipulation Functions
#include<stdio.h>
int main() {
char str[100]; int i = 0;
printf("Convert lower case into upper case Char using C \n");
printf("Please enter a string: ");
gets(str);
while( str[i] != '\0' )
{
if( str[i] >= 'a' && str[i] <= 'z' )
{
str[i] = str[i] - 32;
}
i++;
}
printf("String after converting into upper case : \n%s", str);
return 0; }
22
String Manipulation Functions
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("Convert lower case into upper case Char using C \n");
printf("Please enter a string: ");
gets(str);
strupr(str);
printf("String after converting into uppercase = %s",str);
return 0;
}
23
String Manipulation Functions
Converting Characters of a String into Lower Case
• If we have to convert an upper case character into lower case, we
need to add 32 to the ASCII value of the character.
• tolower() is a C library function that returns a given character
argument to lowercase. If an uppercase alphabet is passed as an
argument then the tolower() function returns a lowercase
alphabet.
• For a lowercase alphabet or special symbol argument, the same
value is returned. Also, we can pass only a single character at a
time as an argument of the tolower() function.
• The tolower() function is defined in the ctype.h header file.
Syntax: int tolower(int argument);
24
String Manipulation Functions
#include <ctype.h>
#include <stdio.h>
int main()
{
char s[] = "EXAMPLE";
int i = 0;
while(s[i]) {
putchar (tolower(s[i]));
i++;
}
return 0;
}
25
String Manipulation Functions
int main() {
char str[100]; int i = 0;
printf("Please enter a string: "); gets(str);
while( str[i] != '\0' )
{
if( str[i] >= 'A' && str[i] <= 'Z' )
{
str[i] = str[i] + 32;
}
i++;
}
printf("String after converting into upper case : \n%s", str);
return 0; }
26
String Manipulation Functions
#include<stdio.h>
#include<string.h>
int main()
{
char str[100];
printf("Convert lower case into upper case Char using C \n");
printf("Please enter a string: ");
gets(str);
strlwr(str);
printf("String after converting into uppercase = %s",str);
return 0;
}
27
String Manipulation Functions
Reverse a String in C
• If S1="HELLO", then reverse of S1="OLLEH". To reverse a string,
we just need to swap the first character with the last, second
character with the second last character, and so on.
Reverse a String Using the strrev() Function
• The strrev() function directly reverses the given string, and this
function is present in string.h library.
28
String Manipulation Functions
#include <stdio.h>
#include <string.h>
int main()
{
char s[100]; //string declaration
printf("Enter a string:");
gets(s); //input
strrev(s); //reversing string
printf("The reverse of the string: %s\n", s);
return 0;
}
29
String Manipulation Functions
Reverse a string without using the library function
#include<stdio.h>
#include<string.h>
void main()
{ int i,n;
char str[20];
printf("Enter the String to get reversed\n");
gets(str);
n=strlen(str);
printf("\nReversed string is \n");
for(i=n-1;i>=0;i--)
{ printf("%c",str[i]);
}
}
30
String Manipulation Functions
Extracting a Substring from Left
• In order to extract a substring from the main string we need to
copy the content of the string starting from the first position to
the nth position where n is the number of characters to be
extracted.
• Example
if S1= “Hello World”, then Substr_Left(S1,7)=Hello w.
31
String Manipulation Functions
#include<stdio.h> while(str[i]!='\0' && i<n)
int main() {
{ substr[i]=str[i];
char str[100],substr[100]; i++;
int i=0,n; }
printf("enter the string:\n"); substr[i]='\0';
gets(str); printf("the substring is:");
printf("enter the no of puts(substr);
characetrs to be copied\n"); return 0;
scanf("%d",&n); }
32
String Manipulation Functions
Extracting a Substring from Right
• In order to extract a substring from the right side of the main
string we need to first calculate the position from the left.
• Example,
if S1= “Hello World” and we have to copy 7 characters starting
from the right then we have to actually start extracting characters
from the 4th position. This is calculated by total number o
characters - n.
• For example, if S1= “Hello World”, then Substr_Right(S1, 7) = o
World
33
String Manipulation Functions
#include<stdio.h> while(str[j]!='\0')
int main() {
{ substr[i]=str[j];
char str[100],substr[100]; i++,j++;
int i=0,j=0,n; }
printf("enter the string:\n"); substr[i]='\0';
gets(str); printf("the substring is:");
printf("enter the no of puts(substr);
characetrs to be copied\n");
return 0;
scanf("%d",&n);
}
j=strlen(str)-n;
34
String Manipulation Functions
Extracting a Substring from the Middle of a string
• To extract a substring from a given string requires information
about three things. The main string, the position of the first
character of the substring in the given string and the number of
characters/length of the substring.
• For example, if we have a string, str[] = “Welcome to the world
of programming” then SUBSTRING (str, 15, 5) = World
35
String Manipulation Functions
#include<stdio.h> while(str[j]!='\0' && n>=0)
int main() { {
char str[100],substr[100]; substr[j]=str[i];
int i=0,j=0,m,n; i++,j++,n--;
printf("enter the string:\n"); }
gets(str); substr[i]='\0';
printf("enter the position from printf("the substring is:");
which to strt the substring\n");
puts(substr);
scanf("%d",&m);
return 0;
printf("enter the length of the
}
string\n");
scanf("%d",&n);
i=m;
36
String Manipulation Functions
Inserting a String in Another String
• The insertion operation inserts a string S in the main text T at the
kth position.
• The general syntax of this operation is
INSERT(text, position, string).
• For example,
INSERT("XYZXYZ",3, "AAA") = "XYZAAAXYZ"
37
String Manipulation Functions
#include<stdio.h>
#include<string.h>
void main()
{
char str1[20], str2[20];
int l1, l2, n, i;
printf("Enter the string 1\n");
gets(str1);
l1 = strlen(str1);
printf("Enter the string 2\n");
gets(str2);
l2 = strlen(str2);
38
String Manipulation Functions
printf("Enter the position where the string is to be inserted\n");
scanf("%d", &n);
for(i = n; i < l1; i++)
{
str1[i + l2] = str1[i];
}
for(i = 0; i < l2; i++)
{
str1[n + i] = str2[i];
}
str2[l2 + 1] = '\0';
printf("After inserting the string is %s", str1);
}
39
String Manipulation Functions
Indexing
• This operation returns the position in the string where the string
pattern first occurs.
• For example, INDEX("Welcome to the world of programming",
"world") = 15
• However, if the pattern does not exist in the string, the INDEX
function returns 0.
40
String Manipulation Functions
Deleting a string from the Main String
• The deletion operation deletes a substring from a given text.
• We can write it as DELETE(text, position, length).
• For example,
DELETE("ABCDXXXABCD", 4, 3) = "ABCDABCD"
41
String Manipulation Functions
#include<stdio.h> #include<string.h>
void main() { char str[20]; int i, n, l, pos;
printf("Enter the string\n"); gets(str);
printf("Enter the position where the characters are to be deleted\n");
scanf("%d", &pos);
printf("Enter the number of characters to be deleted\n");
scanf("%d", &n);
l = strlen(str);
for(i = pos + n; i < l; i++)
{ str[i - n] = str[i]; }
str[i - n] = '\0';
printf("The string is %s", str);
}
42
String Manipulation Functions
Replacing a Pattern with Another Pattern in a String
• The replacement operation is used to replace the pattern P1 by
another pattern P2.
• This is done by writing REPLACE(text, pattern1 , pattern2 ).
• For example,
("AAABBBCCC", "BBB", "X") = AAAXCCC
("AAABBBCCC", "X", "YYY")= AAABBBCC
43