BVRICE Dept, of Computer ScienceBVRICE Dept, of Computer ScienceBVRICE Dept, of Computer Science BVRICE Dept, of Computer Science Page Page Page Page 1111
STRINGS
Introduction
In C language a group characters, digits and symbols enclosed within double quotes are called as a String. The string always declared as a character array.
In other words, the character array is called as a String. To manipulate words and sentences normally strings are preferable. Every string is terminated with a null character.
Syntax to declare a String
char_data_type array_name[size];
Example
char ch[10];
In the above example the character array “ch” is able to store 9 characters and the last character is a null character (‘\0’).
We can directly initialize the string variables as follows.
char ch[]={‘H’,’E’,’L’,’L’,’O’};
char ch[]=”Hello”;
char ch[]={{‘H’},{’E’},{’L’},{’L’},{’O’}};
Each character of the string occupies one byte of memory space. The last character is always a null character (‘\0’). It is not compulsory to write a null character in the string. The compiler automatically puts the null character at the end of the string.
The character of the string are stored in continues memory locations as follows.
String Handling Functions or String Library Functions
The C-language provides the number of functions used to perform the manipulations on strings and these functions are available in the header file <string.h>.
These are explained below.
1. strlen()
This function is used to find out the length of the string.
Syntax: strlen(string_name) Example: strlen(ch);
The following program illustrate the use of strlen() function.
BVRICE Dept, of Computer ScienceBVRICE Dept, of Computer ScienceBVRICE Dept, of Computer Science BVRICE Dept, of Computer Science Page Page Page Page 2222 Program: /*strlen.c*/
#include<stdio.h> #include<conio.h> #include<string.h> void main()
{
char ch[20]; int len; clrscr();
printf("\n Enter a string:"); gets(ch);
/* finding the length of the string */ len=strlen(ch);
printf("\n The length of the given string is: %d",len); getch();
}
Output:
2. strcmp()
It is used to compare two strings. It returns 0((zero) if two strings are equal and it returns 1 if these are not equal.
Syntax: strcmp((string1,string2);
The following program illustrate the use of strcmp() function.
Aim: C-program to demonstrate strcmp() function. Program: /*strcmp.c*/
#include<stdio.h> #include<conio.h> #include<string.h> void main()
{
char s1[10],s2[10]; int flag;
clrscr();
printf("\n Enter the first string:"); gets(s1);
printf("\n Enter the second string:"); gets(s2);
/* Comparing two strings */ flag=strcmp(s1,s2);
if(flag==0)
BVRICE Dept, of Computer ScienceBVRICE Dept, of Computer ScienceBVRICE Dept, of Computer Science BVRICE Dept, of Computer Science Page Page Page Page 3333
printf("\n Strings are not equal"); getch();
}
Output:
3. stricmp()
This function is used to compare two strings by ignoring the case. It returns zero if the two strings are equal otherwise it returns 1.
Syntax: stricmp(string1,string2); Example: stricmp(“Hello”,”hello”);
It returns ‘0’ i.e., two strings are equal.
4. strncmp()
This function compares two strings upto the specified number of characters.
Syntax: strncmp(string1,string2,number_characters); Example: strncmp(“Hello”,”Hello World”,8);
It returns ‘1’, i.e., two strings are not equal.
5. strnicmp()
This function compares two strings upto the specified number of characters by ignoring the case.
Syntax: strnicmp(string1,string2,number_characters); Example: strnicmp(“Hello”,”hello world”,5);
It returns ‘0’ i.e., two strings are equal.
6. strcpy()
This function is used to copy the string2 into the string1 by deleteting the string2 data. Here the size of the string1 should be greater than the sie of the string2.
Syntax: strcpy(string2,string1);
7. strncpy()
This function is used to copy ‘n’ number of characters from string 2 to string1;
Syntax: strncppy(string2, string1, Number_charcaters);
8. strlwr()
This function is used to convert the given string into the lower case string.
Syntax: strlwr(string);
Example: strlwr(“Hello World”); Output: hello world
9. strupr()
This function is used to convert the given string into rthe upper caase string.
Syntax: strupr(string);
BVRICE Dept, of Computer ScienceBVRICE Dept, of Computer ScienceBVRICE Dept, of Computer Science BVRICE Dept, of Computer Science Page Page Page Page 4444 Output: HELLO WORLD
10.strdup()
This function is used to generate a duplicate string for the given string.
Syntax: strdup(string);
Example: s2=strdup(“Hello”);
11.strcat()
This function is used to concatenate (appending the second string at the end of the first string) two given strings.
Syntax: strcat(string1,string2); Example: strcat(“Hello” “World”); Output: HelloWorld
The following program illustrate the use of strcat() function.
Aim: C-program to demonstrate the use of strcat() function. Program: /*strcat.c*/
#include<stdio.h> #include<conio.h> #include<string.h> void main()
{
char s1[10],s2[10]; clrscr();
printf("\n Enter the first string:"); gets(s1);
printf("\n Enter the second string:"); gets(s2);
/* Performing concatenation*/ strcat(s1,s2);
printf("\n The result string is: %s",s1); getch();
}
Output:
12.strncat()
This function is used to concatenate ‘n’ number of characters from the string2 to the string1.
Syntax: strncat(string1, string2, Number_characters). Example: strncat(“Hello”,”World”,2);
BVRICE Dept, of Computer ScienceBVRICE Dept, of Computer ScienceBVRICE Dept, of Computer Science BVRICE Dept, of Computer Science Page Page Page Page 5555
13.strset()
This function is used to replace the given string with the specified character.
Syntax: strset(string,character); Example: strset(“abcde”,’y’); Output: yyyyy
14.strnset()
This function is used to replace ‘n’ number of characters in the given string by the specified character.
Syntax: strnset(string,character, number_characters) Example: strnset(“Hello World”, ‘a’, 4);
Output: aaaao World
15.strrev()
This function is used to reverse the given string.
Syntax: strrev(string); Example: strrev(“Hello”); Output: olleH
16.strspn()
This function is used to findout ipto what length the given two strings are equal.
Syntax: strspn(string1,string2);
Example: strspn(“Hello”, “Hello World”); Output: 5
17.strchr()
This function determines the first occurrence of the given character with in the given string.
Syntax: strchr(string, character) Example: strchr(“abcbcc”,’c’); Output: 2
18.strrchr()
This function is used to determine the last occurrence of the given character within the given string.
Syntax: strrchr(string, charcatyer); Example: strrchr(“abcbcc”,’c’); Output: 5
19.strstr()
This function determines the first occurrence of the string2 in string1.
BVRICE Dept, of Computer ScienceBVRICE Dept, of Computer ScienceBVRICE Dept, of Computer Science BVRICE Dept, of Computer Science Page Page Page Page 6666
Programs
1. Write a C-Program to check whether the given string is palindrome or not.
Aim: C-Program to check whether the given string is palindrome or not.
Program: /*strpal.c*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char s1[10],s2[10];
clrscr();
printf("\n Enter a string:");
gets(s1);
strcpy(s2,s1);
strrev(s1);
if(strcmp(s1,s2)==0)
printf("\n The given string is palindrome");
else
printf("\n The given string is not a palindrome");
getch();
}
BVRICE Dept, of Computer ScienceBVRICE Dept, of Computer ScienceBVRICE Dept, of Computer Science BVRICE Dept, of Computer Science Page Page Page Page 7777
2. Write a C-Program to check whether the given two strings are equal or not
without using string handling functions.
Aim: C-Program too check whether the give two strings are equal or nbot without using
string handling functions.
Program: /* strequal.c*/
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
int i,flag=0;
char s1[20],s2[20];
clrscr();
printf("\n Enter the first string:");
gets(s1);
printf("\n Enter the second string:");
gets(s2);
if(strlen(s1)!=strlen(s2))
printf("\n The two strings are not equal");
else
{
i=0;
while(s1[i]!='\0')
{
if(s1[i]!=s2[i])
{
flag=1;
break;
}
i++;
BVRICE Dept, of Computer ScienceBVRICE Dept, of Computer ScienceBVRICE Dept, of Computer Science BVRICE Dept, of Computer Science Page Page Page Page 8888
if(flag==0)
printf("Strings are equal");
else
printf("\n Strings are not equal");
}
getch();
}
Output:
Array of strings or Strings of arrays
A string is character array. If we want to refer arrays in strings we have to go for two dimensional character arrays, because strings are nothing but a character array. So to implement the string with arrays we have to take another indices [ ].
Syntax for declaring the array of strings:
char string_name[no.of strings][ size of each string];
Example: char book[2][10];
The above represents book is a string variable capable of storing two strings and each string size is 10.
The following program will explain how to use array of strings in C-Language.
Aim: C-Program to demonstrate array of strings.
Program: /*strarray.c*/
#include<stdio.h>
#include<conio.h>
void main()
{
char book[20][20];
int n,i;
BVRICE Dept, of Computer ScienceBVRICE Dept, of Computer ScienceBVRICE Dept, of Computer Science BVRICE Dept, of Computer Science Page Page Page Page 9999
printf("\n Enter the number of books:");
scanf("%d",&n);
printf("\n Enter the books tittles\n");
for(i=0;i<=n-1;i++)
{
scanf("%s",book[i]);
}
printf("\n The book tittles are....\n");
for(i=0;i<n;i++)
{
puts(book[i]);
}
getch();
}
Output:
Important Questions
1. Define string and explain about declaration and initialization of strings in C.