Sei sulla pagina 1di 4

ICS 103 Computer Programming in C

Lab #12: Strings


Objective:
Practice how to use Strings.
String Declaration & Initialization
A string is a list of characters enclosed in double quotes. It is represented in C as an
array of characters
However, one additional cell is required for storing the terminating character,
\0,called NULL
Example Even though the string !I li"e C! has # characters, we need to declare an
array of si$e %.
char str[9]=I like C;
I l i k e C \0
&ote that in the example above, the null character is automatically inserted by the
compiler
All the string functions in C require the presence of the NULL character to wor"
correctly.
'trings can also be declared without specifying the si$e as in
char str[]=I like C;
In this case, sufficient storage is allocated including that for NULL
(i"e array, the individual characters of a string can be accessed by specifying the
index. Example, assuming the above assignment, str[4] is the character

Consider the following example where the program reads the characters of the string
character by character. )he last character read is *+n, -Enter "ey.. /nce outside the loop we
assign the &0(( character *+1, at the location of *+n, to ma"e the sequence of characters a
string. &ow run the example and see what happens when you comment or uncomment the
statement assigning the *+1, character.
#include <stdio.h>
int ain !" #
char st[$0];
int i=0;
%rint&!'(nter )e*t >'";
scan&!'+c',-st[i]";
.hile !st[i]/=0\n0" #
i11;
scan&!'+c',-st[i]";
2
2
33st[i]=0\00;
%uts!st";
return 0;
2

'tatement commented statement uncommented
As you see when the null character is not assigned to the location after the last character of
the string, the 234 array of type char is not considered as a string. )his is clear from the first
case showing some strange characters printed after the *+n, character.
String I/O
)he easiest way to input string is by using the gets function
Example char nae[$4];
%rint&!In%ut 5our nae";
6ets!nae";
gets allows you to read a string that could include blan"s. It terminates reading on
encountering the return character. )he return character will not be part of the string.
Also the easiest way to output string is by using the puts function as in
%uts!nae";
5or scanning from file, we have !gets"str# n# !i$e% which expects 6 arguments string
variable, number of characters to scan and the file to scan from. If end of line is
encountered before scanning n32 characters, fgets will stop reading.
Also we can print into a file using !puts"str# !i$e%&
7ou can also use the s'an!(print! functions for string I8/ using the )s format
specifier.
However, s'an! terminates reading on encountering the blan" character, so first and
last names must be read separately
char &irst[40],last[40];
%rint&!\n(nter 5our &irst nae7 ";
scan&!+s,&irst";
%rint&!\n(nter 5our last nae7 ";
scan&!+s,last";
%rint&!\n8our &ull nae is 7 +s +s,&irst,last";
&ote that the address of operator, *, is not used in the s'an! function.

Built-in string functions:
9
:uilt3in string processing functions can be accessed through the header file, string&+ & 'ome
of these are
str'at"string1# string2% appends strings9 to string2
str'p,"string1# string2% replaces string2 with string9
str$en"string1% returns the length of string2 e-'$u.ing &0(( character
str'+r"string# '+% searches for the occurrence of ch in string
strstr "string1# string2% searches for the occurrence of string9 in string2
str'mp"string1# string2% compares string2 and string9 and returns
o &egative integer if string2 ; string9
o 1 if string2 < < string9
o positive integer if string2 = string9.

)he following program illustrates the use of some string functions
#include <strin6.h>
#include <stdio.h>
ain!" #
char s4[$4], s9[$4];
6ets!s4";
6ets!s9";
%rint&!len6ths7 +d +d\n, strlen!s4", strlen!s9"";
i& !/strc%!s4, s9""
%rint&!)he strin6s are e:ual\n";
strcat!s4,s9";
%rint&!+s\n,s4";
return 0;
}
Note: It is important to remember that str'mp"% returns false if the strings are equal, so be
sure to use / to reverse the condition
Built-in character functions:
:uilt3in character functions can be accessed through the header file, 't,pe&+
'ome of the common functions are
isupper"'+% returns non$ero if ch is uppercase, returns 1 otherwise
isa$p+a"'+% returns non$ero if ch is an alphabet -lower or upper case. or 1
otherwise
isa$num"'+% returns non$ero if ch is an alphabet -lower or upper case. or a
digit -1,2,..%., or 1 otherwise
is.igit"'+% returns non$ero if ch is a digit -1, 2, >, %. or 1 otherwise
to$o0er"'+% converts a character to its lower case if it has one
toupper"'+% converts a character to its upper case if it has one
Exercises:

6
Exercise 1:
Write a function search_char that receives a string and a character and return the index of
the character if it is present in the string or -1 otherwise. The function is similar to linear
search function but works with characters.
Write a main function to test our function as shown below.
Exercise :
The following is an implementation of the function strcmp.
int strcmp (char s1[], char s2[]) {
int i;
for (i=0; s1[i]!=\0&&s2[i]!=\0; i++ ) {
if(s1[i]!=s2[i])
return s1[i]s2[i];
!
return s1[i]s2[i];
!

As shown in the second run, the function strcmp is case sensitive. 7ou need to modify strcmp and call it
my?strcmp so that it is case insensitive. @rite a main function to test it as shown below.
Exercise !:
Write a function that receives a string and sort it using selection sort. The sorting should be
case insensitive as in exercise !. "ou need to modif selection_sort function used for
numbers so that ou appl it for characters.
Test our function as shown below.
A

Potrebbero piacerti anche