Working with Strings & Standard Functions 247
8.4 STRING STANDARD FUNCTIONS
Every 'C' compiler supports a large number of strings handling library functions. Table 8.3 provides
list of frequently used functions and their description.
Table 8.3 Standard C String Library Functions
Functions Description
s t r le n O
Determines length of a string.
s tr c p y () Copies a string from source to destination.
strn cp y () Copies characters of a string to another string upto the specified length.
strcnpO
Compares characters of two strings (Function descriminates between small &
capital letters.)
stricnpO
Compares two strings. (Function doesn't discriminate between small & capital
letters.
stmarpO
Compares characters of two strings upto the specified length.
stmicmpO
Compares characters of two strings upto the specified length. Ignores case.
strlwr ()
Converts uppercase characters of a string to lowercase.
struprO
Converts lowercase characters of a string to uppercase.
strdupO
Duplicates a String.
strchr () Determines first occurrence of a given character in a string.
strrchr () Determines last occurrence of a given character in a string.
strstr ()
Determines first occurrence of a given string in another string.
strcatO
Appends source string to destination string.
stmcat ()
Appends source string to destination string upto specified length.
strrevO
Reverses all characters of a string.
street () Sets all characters of string with a given argument or symbol.
stmset ()
Sets specified numbers of characters of string with a given argument or symbol.
strspnO
Finds upto what length two strings are identical.
strpbrkO
Searches the first occurrence of the character in a given string and then it displays
the string starting from that character.
We shall elaborate the above-cited functions by providing few examples of each of them. Without
using the standard functions we can also write the programs. Few such programs are briefly described.
s t r le n O fu n ction s
This function counts the number of characters in a given string. The format of function is s t r le n
(s t r i n g ) . Program in this regard is illustrated below.
248 Programming and Data Structures
8.7 Write a program to count the number of characters in a given string.
# include <stdio.h>
# include <canio.h>
# include <string.h>
void mainO
{
char text [20];
int len;
clrsc rO ;
printf ("T ype Text Below .n”);
gets(text);
len=strlen(text);
printf ("Length ofString =% d ", len);
I
o u m m
Type Text Below.
Hello
Length of String = 5
Explanation In the above program s trlen () function is called. Through text array base address
will be sent and in lieu of this the function returns the length of the string. It counts the string length
without the NULL character.
8.8 Write a program to read a name through the keyboard. Determine the length of the
string and find its equivalent ASCII codes.
# include <stdib.h>
# include <conio.h>
# include <string.h>
xnainO
{
static char name [20];
int i , l ;
clrscrO ;
printf ("Enter your name:");
scanf ("% s " ,name);
l=strlen(name);
printf ("Y our Nam e is % s & " ,name);
printf ("it contains %d characters",I);
printf ("nN a m e b i t 's Ascii Equivalent.n");
printf ("==== = === ===== ========== «");
for (i=0,~i<l,n++)
printf(" %ctt%d",name[i],name[i]);
getcheO;
Working with Strings & Standard Functions 249
Q.UTEUT;
Enter your name: SACHIN
Your Name is SACHIN & it contains 6 characters.
Name & its ASCII Equivalent.
S 83
A 65
C 67
H 72
I 73
N 78
Explanation In the above program string is entered. The string length is determined using s t r 1 en
() function. The fo r loop is used for displaying characters and their equivalent ASCII codes.
8.9 Write a program to remove the occurrences of "The" word from entered text
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main ()
{
static char line [80], line2 [80];
int i, j*0 ,l;
c lr sc r();
printf ("E nter Text Below .n");
gets(line);
l=strlen(line);
fo r (i=0,-i<=l,i++)
(
if (i> = l-4 I I l= -3 && l in e [l-4 ]= = " && lin e [l-3 ]* * -'f tzfx. line[l-2]=='h'6c& l in e [l-l]= = 'e ')
continue;
if(line[i]= ='t'& & lin e [i+ l]~ 'h '& & : line[i+2]==e '&& line[i+3]= = '')
I
i+=2;
continue;
I
else
I
li)ie2lj]~line[i];
;++;
)
I
printf (" n Text With ‘The' % s", line);
printf (" n Text Without 'T he' %s",line2);
250 Programming and Data Structures
getcheO;
I
Q i m m
Enter Tex Below.
Printf write data to the screen.
Text With The' Printf write data to the screen.
Text Without "The' Printf write data to screen
Explanation The first i f condition identifies the appearance o f ' th e 9 at the end as well as when the
string contains only ' th e '. If it is found then the program terminates.
The second i f condition in the above program also identifies the appearance o f % th e ' word. If it is not
found % t h e ', elements of the first array are copied to the second array as it is. If the word x t h e 7 is
found program will continue to loop and never reach the statement 1 ine2 [ j ] = 1 in e [ i ] ;.
8.10 Write a program to delete all the occurrences of vowels in a given text. Assume that the
text length will be of one line.
# include <stdio.h>
# include <conio.h>
void main ()
{
char lin e [8 0], lin e2 [8 0];
in t i , j= 0 ;
c lr s c r ( ) ;
printf ("Enter Text Below.n");
gets(line);
for (i=0;i<80,'i++)
I
i f (l in e [i ]~ 'a ' I I lin e[i]= ='e' I I lin e [i]= ='i' I I lin e[i]= = 'o ' I I lin e [i]= = 'u ')
continue;
else
I
line2[j]=line[i];
/++;
/
I
printf (" n Text With Vowels %s", line);
printf (" n Text Without Vowels % s"rline2);
I
O U T P U T :
Enter Text Below.
Have a nice day.
Text With Vowels Have a nice day.
Text Without Vowels Hv nc dy.
Working with Strings & Standard Functions 251
Explanation The logic used here is same as described in the previous program. The i f statement
checks occurrence of vowels (a, e, o u) & i. If vowels are found loop is continued otherwise
elements are copied. Thus, vowels are ignored in the final output.
8.11 Write a program to find the length of characters in a given string including and excluding
spaces.
# include <stdio.h>
void mainQ
char text [20];
in t i*0,len«0,exe0;
clrscr ();
printf ("Enter Text Here
gets(text);
while (text[i]l='0r)
printf ("nLength o f String Including Spaces.: %d",len+ex);
printf ("nLength o f String Excluding Spaces.: % d",len);
I
OUTPUT;
Enter Text Here:
Time is Money.
Length of String Including Spaces.: 13
Length of String Excluding Spaces.: 11
Explanation In the above program text is entered. The i f statement within the whi 1 e loop checks
every element of the string and if space is observed the counter variable ' e x ' is increased otherwise
variable ' le n ' is increased. Thus, at the end the variable x e x ' contains total number of spaces in the
string and the variable * le n ' contains length of the string excluding spaces. The length including
spaces is calculated by adding both the variables.
8.12 Write a program to display the length of entered string & display it as per output shown.
# include <stdio.h>
# include <conio.h>
# include <string.h>
void main ()
{
{
else
lett++;
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.144.94.190