Lecture 6: String Processing
Objectives of this lecture
q Learn how to declare, initialize, and process strings
q Learn the basic string functions
q Be able to apply strings in problem solving
Declaration & Initialization
q A string is a sequence of characters enclosed in
double quotes
q It is represented in C as an array of characters
q However, one additional cell is required for storing
the terminating character, ’\0’,called NUL
q Example: Even
though the string ”I like C” has 8 characters, we need to declare a character
array of size 9.
char str[9]=”I like C”;
’I’
|
’ ’
|
’L’
|
’i’
|
’k’
|
’e’
|
’ ’
|
’C’
|
’\0’
|
q Note that in the example above, the null character is
automatically inserted by the compiler
q All the string functions in C require the presence of
the NUL character to work correctly.
q Note that strings (like arrays) depends on the
programmer to give enough size. If size
is not enough, data will be read anyway, replacing memory cells not meant for
the string – leading to unpredictable results.
q Strings can also be declared without specifying the
size as in:
char str[]=”I like C”;
In
this case, sufficient storage is allocated including that for NUL
q Because a string is an array of characters, an array
of strings is a 2-D array of characters.
q Example: To
declare an array of strings to store week days.
Char day[7][10]={”Monday”, ”Tuesday”,
”Wednesday”,
Thusday”,
”Friday”, ”Saturday”, ”Sunday”}
String I/O
q The easiest way to input string is by using the gets function
Example: char name[81];
printf(”Input your name”);
gets(name);
q Also the easiest way to output string is by using the puts function as in:
puts(name);
q gets allows you to read a string that could include blanks. It terminates reading on encountering the
return character.
q puts advances to the next line after printing
q You can also use the scanf/printf functions for string
I/O using the %s format specifier.
q However, scanf terminates reading on encountering the blank
character, so first and last names must be read separately:
char
first[10],last[10];
printf(”\nEnter
your first name: ”);
scanf(”%s”,first);
printf(”\nEnter
your last name: ”);
scanf(”%s”,last);
printf(”\nYour
full name is : %s %s”,first,last);
q Note that the address of operator, &, is not used
in the scanf function – the name of a string (or array) is an
address.
Built-in string functions:
q Built-in string processing functions can be accessed
through the header file, string.h
q Some of the common functions are:
strcat(string1, string2) à appends strings2 to
string1
strcpy(string1, string2) à replaces string1 with
string2
strcmp(string1,
string2) à compares string1 and string2
and returns:
Negative
integer if string1 < string2
0
if string1 == string2
positive
integer if string1 > string2.
strlen(string1) à returns the length of string1 excluding NUL
strchr(string, ch) à searches for the occurrence of ch in string
The
following program illustrates the use of string functions:
#include <string.h>
#include <stdio.h>
main()
{ char s1[81], s2[81];
gets(s1);
gets(s2);
printf(”lengths: %d %d\n”, strlen(s1), strlen(s2));
if (!strcmp(s1, s2))
printf(”The strings are equal\n”);
strcat(s1,s2);
printf(”%s\n”,s1);
return 0;
}
Note: It is important to remember that strcmp() returns false if the strings are equal, so be sure to
use ! to reverse the condition
Built-in character functions:
q Built-in character functions can be accessed through
the header file, ctype.h
q Some of the common functions are:
isupper(ch) à returns 1 if ch is uppercase, returns 0 otherwise
isalpha(ch) à returns 1 if ch is an alphabet (lower or upper case)
tolower(ch) à converts a character to its lower case if it has one
toupper(ch) à converts a character to its upper case if it has one
Example:
Problem: Write a program that reads a string and print the
number of vowels, number of upper case and number of lower case letters . It should also print the string with the
first letter capitalized and the remaining in lower case.
Reasoning: Need to use
string & character processing functions such as strlen, tolower, etc.
Input: the string
Output: number of lower, upper and vowels and the formatted
string.
Process: apply string/character functions to count as well as
converts characters in the string.
Solution:
Step 1: INPUT str
Step 2: find the
length of str (n)
Step 3: Initialize
counters (vowels, uppers, lowers) to zero
Step 4: LOOP n
times
IF str[i] is a vowel
Vowels=vowels+1
Step 5: LOOP n
times
IF str[i] is an alphabet
IF str[i] is upper
Uppers=uppers+1
ELSE
Lowers=lowers+1
Step 6: coverts
first character to upper
Step 7: LOOP n-1
times
Convert str[i] to lower
Step 8: OUTPUT
vowels, lowers, uppers, str
Step 9: STOP
/*
prints the number of vowels, upercase & lowercase in a
given string. Also capitalize the first
letter of string */
#include
<stdio.h>
#include
<string.h>
#include
<ctype.h>
int
isvowel(char ch);
main()
{ char str[81];
int len, i, nvowel=0, nupper=0, nlower=0;
printf("Enter your string >");
gets(str);
len=strlen(str);
for (i=0; i<=len; i++)
if (isvowel(str[i]))
nvowel++;
for (i=0; i<=len; i++)
if isalpha(str[i])
if (isupper(str[i]))
nupper++;
else
nlower++;
str[0]=toupper(str[0]);
for (i=1; i<=len; i++)
str[i]=tolower(str[i]);
printf("Number of vowels = %d\n", nvowel);
printf("Number of lower case =
%d\n", nlower);
printf("Number of upper case =
%d\n", nupper);
printf("Capitalised string = %s\n", str);
return 0;
}
/*
returns true if a given character is a vowel */
int isvowel(char ch)
{
int vowel;
vowel=tolower(ch)=='a' ||
tolower(ch)=='i' ||
tolower(ch)=='o' ||
tolower(ch)=='u' ||
tolower(ch)=='e';
return vowel;
}