Unit-4
C STRINGS:
A group of characters enclosed in double quotes is called String
Eg: “Hello”
In C language, we represent strings as character arrays.
The basic syntax of declaring a string is as follows:
char string_name[size];
Here the ‘size’ specifies the number of characters in the string.
Eg: char name[20];
Storing strings:
In C, a string is stored as a character array.
It is terminated by a null character (‘\0’).
The following figure shows how a string is stored in memory:
End of the string
Beginning of the string (null character)
H e l l o \0
Initializing strings:
We can initialize strings when they are defined. We can assign a string literal to a character array
as follows:
char message[10] = ”Hello!”;
We can also initialize a string as an array of characters as follows:
char message[10] = {‘H’, ’e’, ’l’, ’l’, ’o’, ’!’, ’\0’};
H e l l o ! \0
Reading and printing Strings:
1. Reading and printing using scanf() and printf() functions:
We can read strings from the keyboard at run time using scanf() statement also. The format
specifier used to read strings is ‘%s’.
We can omit the address (&) operator in scanf() while reading strings as follows:
Example: The following program reads a string from keyboard and displays it on the screen.
#include<stdio.h>
void main(){
char str[20];
printf(“\n Enter a String: “);
scanf(“%s”, str);
printf(“\n The string you entered: %s”, str);
}
Output:
Enter a String: welcome to C
The string you entered: welcome
Note: scanf() function has disadvantage that it can’t read string after white space character.
2. Reading a Line of Text using Edit set conversion code:
We can read a line of string using edit set conversion code%[..], which is used to read a line
containing a variety of characters, including whitespaces.
Strings Page 1
Unit-4
Example: The following program reads a line from keyboard and displays it on the screen.
#include<stdio.h>
void main(){
char str[20];
printf(“\n Enter a String: “);
scanf("%[^\n]", str);
printf(“\n The string you entered: %s”, str);
}
Output:
Enter a String: welcome to C
The string you entered: welcome
3. Reading and printing using gets() and puts() :
We can read string using gets() instead of scanf() function. It is used a read a line of string
and it overcomes the problem of scanf() function.
We can print string using puts() function similar to printf()
Example: The following program reads a line from keyboard and displays it on the screen.
#include<stdio.h>
void main(){
char str[20];
printf(“\n Enter a String: “);
gets(str);
printf(“\n The string you entered: ”);
puts(str);
}
Output:
Enter a String: welcome to C
The string you entered: welcome to C
String Handling Functions:
C supports a large number of string handling functions in the standard library "string.h", and these
functions are used to manipulate strings.
The following are some of String Handling Functions:
1. strlen() – finds the length of the given string
2. strcpy() – copies source string to destination string
3. strcmp() – compare two strings
4. strcat() – it appends one string at the end of another string
5. strrev() – it reverses the given string.
6. strupr() – it converts to uppercase
7. strlwr() – it converts to lower case
8. strstr() – it finds the first occurrence of the given string
9. strncpy() – it copies first n characters from source string to destination
10. strncat() – it appends first n characters of one string at the end of another string
11. strncmp() - compare first n characters of two strings
12. strnicmp() - compare first n characters of two strings by ignoring case
13. strchr() – it finds the first occurrence of the character.
14. strrchr() - it finds the first occurrence of the character from reverse.
Strings Page 2
Unit-4
strlen():
It is used to find the length of the given string, That means it counts the number of characters of
the given string.
Syntax: strlen(string)
Program:
#include<stdio.h>
main(){
char n[30];
int len;
printf("\n Enter a String: ");
gets(n);
len=strlen(n);
printf("\n Given String is %s",n);
printf("\n Length is %d",len);
}
Output:
Enter a String: computer system
Given String is computer system
Length is 15
strcpy():
It is used to copyt the given string into another string.
Syntax: strcpy(string1, string 2);
This functions copies the content of string 2 into string 1.
Program:
#include<stdio.h>
#include<string.h>
main(){
char s1[30],s2[30]="";
printf("\n Enter a String: ");
gets(s1);
printf("\ before copy");
printf("\n s1=%s",s1);
printf("\n s2=%s",s2);
strcpy(s2,s1);
printf("\n after copy");
printf("\n s1=%s",s1);
printf("\n s2=%s",s2);
}
Output:
Enter a String: computer system
before copy
s1=computer system
s2=
after copy
s1=computer system
s2=computer system
strcmp():
It is used to compare two strings. It returns 0 if both strings are equal. If they are not. It has the
numeric difference between the first no matching characters in the strings.
Strings Page 3
Unit-4
Syntax: strcmp(string1,string2)
Program:
#include<stdio.h>
#include<string.h>
main(){
char s1[30],s2[30];
int d;
printf(“enter string1”);
gets(s1);
printf(“enter string2”);
gets(s2);
d=strcmp(s1,s2);
if(d==0)
Printf(“two strings are equal”);
else
printf(“Two strings are not equal”);
}
Output:
Enter string1: ram
Enter string2: rom
Two strings are not equal
Enter string1: ram
Enter string2: ram
Two strings not equal
strcat():
It is used to concatenate(join) two strings .
Syntax: strcat(string1,string2);
Here string1 and string2 are character arrays. When the function strcat is executed, string2 is
appended to string1.
Program:
#include<stdio.h>
#include<string.h>
main(){
char s1[30]="raja";
char s2[30]="sekhar";
printf("\n s1=%s",s1);
printf("\n s2=%s",s2);
printf("\n after concatenation");
strcat(s1,s2);
printf("\n s1=%s",s1);
printf("\n s2=%s",s2);
}
Output:
s1=raja
s2=sekhar
after concatenation
Strings Page 4
Unit-4
s1=rajasekhar
s2=sekhar
strrev():
It is used to reverse a given string.
Syntax: strrev(string1);
Porgram:
#include<stdio.h>
#include<string.h>
main(){
char s1[30];
printf("Enter string\n");
gets(s1);
printf("Given String =%s",s1);
strrev(s1);
printf("\n Given string after string reverse %s ",s1);
}
Output:
Enter string
computer
Given String =computer
Given string after string reverse retupmoc
strlwr():
It is used to convert the characters of given string into lower case letters.
Syntax: strlwr(string1);
Program:
#include<stdio.h>
#include<string.h>
main(){
char s1[30];
printf("Enter string\n");
gets(s1);
printf("\n Given String =%s",s1);
strlwr(s1);
printf("\n Given string in lower case %s ",s1);
}
Output:
Enter string
COMPUTER
Given String =COMPUTER
Given string in lower case computer
strupr():
It is used to convert the characters of given string into upper case letters.
Syntax: strupr(string1);
Program:
#include<stdio.h>
#include<string.h>
main(){
char s1[30];
Strings Page 5
Unit-4
printf("Enter string\n");
gets(s1);
printf("\n Given String =%s",s1);
strupr(s1);
printf("\n Given string in upper case %s ",s1);
}
Output:
Enter string
computer
Given String =computer
Given string in lower case COMPUTER
strncpy():
It is used to copies only the leftmost n characters of the source string to the target string
variables.
Syntax: strncpy(s1,s2,n);
This function copies first n character of s2 into s1.
Program:
#include<stdio.h>
#include<string.h>
main(){
char s1[30]="";
char s2[30]="raja rao";
printf("\nbefore copy");
printf("\ns1 =%s",s1);
printf("\ns1 =%s",s2);
printf("\nafter copy");
strncpy(s1,s2,4);
printf("\ns1 =%s",s1);
printf("\ns1 =%s",s2);
}
Output:
before copy
s1 =
s1 =raja rao
after copy
s1 =raja
s1 =raja rao
before copy
s1 =
s1 =raja rao
after copy
s1 =raja
s1 =raja rao
strncat():
It is used to concatenate first n characters of one string into another string.
Syntax: strncat(s1,s2,n):
Here it copies leftmost n characters of s2 into s1.
Strings Page 6
Unit-4
Program:
#include<stdio.h>
#include<string.h>
main(){
char s1[30]="rama";
char s2[30]="krishna rao";
printf("\nbefore concatenate");
printf("\ns1 =%s",s1);
printf("\ns2 =%s",s2);
printf("\nafter concatenate");
strncat(s1,s2,7);
printf("\ns1 =%s",s1);
printf("\ns2 =%s",s2);
}
Output:
before concatenate
s1 =rama
s2=krishna rao
after concatenate
s1 =ramakrishna
s2 =krishna rao
strncmp():
It is used to compare first n characters of two strings. It returns 0 if both are equal. If they are not
equal, it returns +ve or –ve value.
Syntax: strncmp(s1,s2,n);
Program:
#include<stdio.h>
#include<string.h>
main(){
char s1[30]="rama rao",s2[30]="rama krishna";
int d;
d=strncmp(s1,s2,5);
printf("string difference is %d",d);
}
Output:
string difference is 0
Strings Page 7