STRINGS IN C
Strings
The way a group of integers can be stored in an integer array,
similarly a group of characters arrays may some time also called
“strings”
A string constant is a one-dimensional array of characters terminated
by a null(‘\0’).For performing “c” programs with strings ,a header
file called “string.h” should be included.
Example:
Char name[]={‘v’,’I’,’j’,’a’,’y’,’\0’};
String handling functions in “c”:
There are 4 types ,
[Link]()function
[Link]()function
[Link]()function
[Link]()function
1. strlen( ) Function:
It is used to find the length of a character string.
Ex:
Int n;
char st[20]=”Bangalore”;
n=strlen(st);
2. strcpy( ) Function:
It is used to copy from one string to another string.
Ex:
char city[15];
strcpy(city,”BANGALORE”);
This will assign the string “BANGALORE” to the character variable
string
* Note that character value like
city = “BANGALORE”; cannot be assigned in C language.
3. strcat( ) Function:
strcat( ) function is used to join character, Strings. When
two character strings are joined, it is referred as concatenation of
strings.
Ex: char city[20] = “BANGALORE”;
char pin[8] = “-560001”; strcat (city,pin);
This will join the two strings and store the result in city as
“BANGALORE – 560001”.
Note that the resulting string is always stored in the left side string
variable.
4. strcmp( ) Function:
strcmp ( ) function is used to compare two character
strings.
It returns a 0 when two strings are identical. Otherwise it returns a
numerical value which is the different in ASCII values of the first
mismatching character of the strings being compared.
char city[20] = “Madras”; char town[20] = “Mangalore”; strcmp(city,
town);
This will return an integer value „- 10‟ which is the difference in the
ASCII values of the first mismatching letters „D‟ and „N‟
Note that the integer value obtained as the difference may be
assigned to an integer variable as follows: int n; n = strcmp(city,
town);
READING / WRITING STRINGS:
*By using scanf( )
Eg:printf(“enter the string”);
scanf(“%s”,string);
*By using gets( )
Eg:printf(“enter the string”);
gets(string);
atoi( ) Function:
atoi( ) function is a C library function which is used to
convert a string of digits to the integer value. char st[10] =
“24175” ; int n; n = atoi(st); This will assign the integer value 24175
to the integer variable n.
STRING PALINDROME
#include<stdio.h>
#include<conio.h>
#include<string>h>
void main( )
{
char st[20],rst[20];
int i,j;
printf(“enter the string”);
scanf(“%s”,st);
i=0;
j=strlen(st)-1;
while(j=0)
{
rst[i]=st[j];
i++;
j--;
}
rst[i]=’\0’;
if(strcmp(st,rst)==0)
printf(“%s is a palindrome”);
else
printf(“%s is not a palindrome”);
}
Write a C program to count the occurrence of a particular character in the given
string.
Solution:
Consider a string “MISSISSIPPI”. Count the appearance of a character, say „S‟.
Program:
\* program to count a character in a string * /
# include<stdio.h>
# include<conio.h>
# include<string.h>
main( )
{
char st[20], ch;
int count, l,i;
clrscr( );
printf(“ \n Enter the string:”);
gets(st);
printf(“ \n which char to be counted ?”);
scanf(“ %c”, &ch);
/* loop to check the occurrence of the character * /
l = strlen(st);
count = 0;
for( i=0; i < l; i++)
if(st[ i ] = = ch)
count ++;
printf(“ \n the character %c occurs %d times”, ch, count);
getch( ) ;
}
When this program is executed, the user has to enter the string and
the
character to be counted in the given string.
Output:
Enter the string : MISSISSIPPI
Which char to be counted? S
The character S occurs 4 times.
Write a C program to count the number of vowels present in a
sentence.
Solution
Consider a sentence “this is a book”. Count the appearance of
vowels AEIOU
in capital or small letters.
Program
/* program to count vowels * /
# include<stdio.h>
# include<conio.h>
# include<string.h>
main( )
{
char st[80], ch;
int count = 0, i;
clrscr( );
/* loop to read a string * /
printf(“ \n Enter the sentence: \n”);
gets(st);
/* loop to count the vowels in the string * /
for( i=0; i<strlen(st); i++)
switch(st [i ])
{
case „A‟:
case „E‟:
case „I‟:
case „O‟:
case „U‟:
case „a‟:
case „e‟:
case „i‟:
case „o‟:
case „u‟:
count ++;
break;
}
printf(“\n %d vowels are present in the sentence”, count);
getch( );
}
When this program is executed, the user has to enter the sentence.
Note that gets( ) function is used to read the sentence because the string
has
white spaces between the words.
The vowels are counted using a switch statement in a loop.
Note that a count++ statement is given only once to execute it for the cases
in
the switch statement.
Output:
Enter the sentence:
This is a book
5 vowels are present in the sentence.
Write a C program to compare two strings which are given as input
through
keyboard and print the alphabetically greater string.
Solution
The alphabetically greater string is the one whose ASCII value of the
first letter
is greater than that of the second string.
Consider the two character strings.
St1 = “ALPHA”
St2 = “BEETA”
St2 is greater than st1 because the ASCII value of „B‟ in “BETA” is greater
than that of „A‟ in „ALPHA‟.
Note that when the first letters of the two strings are identical, the
second
letters of the strings are compared.
/* PROGRAM TO PRINT ALPHABETICALLY GREATER STRING * /
# include<stdio.h>
# include<conio.h>
# include<stirng.h>
main( )
{
char st1[20], st2[20];
clrscr( );
printf(“ \n Enter string 1:”);
scanf( “ %s ”,st1);
printf(“ \n Enter string 2:”);
scanf( “ %s ”, st2);
if(strcmp(st1,st2)> 0)
printf(“\n %s is alphabetically greater string”, st1);
else
printf(“ \n %s is alphabetically greater string”, st2);
getch( );
}
When this program is executed, the user has to enter the two strings.
Note that strcmp( ) function returns a positive value when string 1 is
greater &
a negative value when string 2 is greater.
Output:
Enter string 1 : ACPHA
Enter string 2 : BETA
Write a C program to convert a line in the lower case text to upper case.
Solution
Consider ASCII values of lower and upper case letters
A – 65 B – 66 …………………… Z – 90
a - 97 b – 98 …………………… z – 122
* Note that the difference between the lower and upper case letters (ie. – 32) is
used for conversion.
/* PROGRAM TO CONVERT LOWER CASE TEST TO UPPER CASE * /
# include<stdio.h>
# include<conio.h>
# include<string.h>
main( )
{
char st[80];
int i;
clrscr( );
printf(“ \ n Enter a sentence : \ n”);
gets(st);
/* loop to convert lower case alphabet to upper case * /
for( i=0; i<strlen(st); i++)
if(st[ i ] >= „a‟ && st[ i ] <= „z‟)
st[ i ] = st[ i ] – 32;
printf(“ \n the converted upper case strings \n %s”, st);
getch( );
}
OUTPUT:
Enter a sentence:
Logical thinking is a must to learn programming
The converted upper case string is
LOGICAL THINKING IS A MUST TO LEARN PROGRAMING.
Write a C program to count no of lines, words and characters in a given text.
Solution:
A while loop is used to read the text. The character „$‟ is used to
terminate the reading of text.
Program
# include<stdio.h>
# include<string.h>
# include<conio.h>
main( )
{
char txt[250], ch, st[30];
int ins, wds, chs, i;
clrscr( ) ;
printf(“ \n Enter the text, type $ st end \n \n”);
i=0;
while((txt[i++]= getchar( ) ) ! = „$‟);
i--;
st[ i ] = „\0‟ ;
ins = wds = chs = 0;
/ * loop to count lines, words & characters in text * /
i=0;
while(txt[ i ]!=‟$‟)
{
switch(txt[ i ])
{
case „,‟:
case „!‟:
case „\t‟:
case „ ‟:
{
wds ++;
chs ++;
break;
}
case „?‟:
case „.‟:
{
wds ++;
chs ++;
break;
}
default: chs ++;
break;
}
i++;
}
printf(“\n\n no of char ([Link]) = %d”, chs);
printf(“\n No. of words = %d”, wds);
printf(“\n No of lines = %d”, lns);
getch( ) ;
}
Output:
Enter the text, type $ at end
What is a string? How do you initialize it? Explain with example.
With example. $
No of char (inch. Blanks) = 63
No of words = 12
No of lines = 3.
Additional String Handling Functions:
Some „C‟ compilers will accept the following string handling functions
which are available in header files string.h and ctype.h
Functions:
(i) strupr( ): to convert all alphabets in a string to upper case letters.
Ex:
strupr(“ delhi ”) “ DELHI”
(ii) strlwr( ): To convert all alphabets in a string to lower case letters.
Ex:
strlwr(“ CITY ”) “city”
(iii) strrev( ): To reverse a string
Ex: strrev(“ SACHIN ”) “NIHCAS”
(iv) strncmp( ): To compare first n characters of two strings.
Ex:
m = strncmp (“ DELHI ”, “ DIDAR ”, 2);
m = -4
(v) strcmpi( ): To compare two strings with case in sensitive (neglecting
upper
/ lower case)
Ex:
m=strcmpi(“ DELHI ”, “ delhi ”); m = 0.
(vi) strncat( ): To join specific number of letters to another string.
Ex.
char s1[10] = “New”;
char s2[10] = “Delhi -41”;
strncat(s1,s2,3);
s1 will be “NewDel”.