0% found this document useful (0 votes)
19 views6 pages

Word and Customer Count Algorithms

The document presents two case studies involving string manipulation and processing in C programming. The first case study involves counting the words, lines, and characters in a given text by reading it line by line and checking for spaces and newlines. The second case study reformats a list of customer names and phone numbers from a format with full names to a sorted alphabetical list with last names first followed by initials. Both cases demonstrate algorithms and C programs to process and manipulate string data.

Uploaded by

brain stome
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views6 pages

Word and Customer Count Algorithms

The document presents two case studies involving string manipulation and processing in C programming. The first case study involves counting the words, lines, and characters in a given text by reading it line by line and checking for spaces and newlines. The second case study reformats a list of customer names and phone numbers from a format with full names to a sorted alphabetical list with last names first followed by initials. Both cases demonstrate algorithms and C programs to process and manipulate string data.

Uploaded by

brain stome
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd

CASE STUDIES

1. Counting Words in a Text

One of the practical applications of string manipulations is counting the words in a text.
We assume that a word is a sequence of any characters, except escape characters and
blanks, and that two words are separated by one blank character. The algorithm for
counting words is as follows:

1. Read a line of text.


2. Beginning from the first character in the line, look for a blank. If a blank is found,
increment words by 1.
3. Continue steps 1 and 2 until the last line is completed.

The implementation of this algorithm is shown in Fig.8.11. The first while loop will be executed
once for each line of text. The end of text is indicated by pressing the `Return' key an extra time
after the entire text has been entered. The extra `Return' key causes a newline character as input
to the last line and as a result, the last line contains only the null character.

The program checks for this special line using the test

if ( line[0] == `\0')

and if the first (and only the first) character in the line is a null character, then counting is
terminated. Note the difference between a null character and a blank character.

COUNTING CHARACTERS, WORDS AND LINES IN A TEXT


Program
#include <stdio.h>

main()
{
char line[81], ctr;
int i,c,
end = 0,
characters = 0,
words = 0,
lines = 0;
printf("KEY IN THE TEXT.\n");
printf("GIVE ONE SPACE AFTER EACH WORD.\n");
printf("WHEN COMPLETED, PRESS 'RETURN'.\n\n");

while( end == 0)
{
/* Reading a line of text */

c = 0;
while((ctr=getchar()) != '\n')
line[c++] = ctr;
line[c] = '\0';

/* counting the words in a line */

if(line[0] == '\0')
break ;
else
{
words++;
for(i=0; line[i] != '\0';i++)
if(line[i] == ' ' || line[i] == '\t')
words++;
}

/* counting lines and characters */

lines = lines +1;


characters = characters + strlen(line);
}
printf ("\n");
printf("Number of lines = %d\n", lines);
printf("Number of words = %d\n", words);
printf("Number of characters = %d\n", characters);
}

Output

KEY IN THE TEXT.


GIVE ONE SPACE AFTER EACH WORD.
WHEN COMPLETED, PRESS 'RETURN'.

Admiration is a very short-lived passion.


Admiration involves a glorious obliquity of vision.
Always we like those who admire us but we do not
like those whom we admire.
Fools admire, but men of sense approve.

Number of lines = 5
Number of words = 36
Number of characters = 205

Fig.8.11 Counting of characters, words and lines in a text

The program also counts the number of lines read and the total number of characters in
the text. Remember, the last line containing the null string is not counted.
After the first while loop is exited, the program prints the results of counting.
2. Processing of a Customer List

Telephone numbers of important customers are recorded as follows:

Full name Telephone number

Joseph Louis Lagrange 869245


Jean Robert Argand 900823
Carl Freidrich Gauss 806788
----- ---
----- ---

It is desired to prepare a revised alphabetical list with surname (last name) first, followed by a
comma and the initials of the first and middle names. For example,

Argand,J.R

We create a table of strings, each row representing the details of one person, such as first_name,
middle_name, last_name, and telephone_number. The columns are interchanged as required
and the list is sorted on the last_name. Fig.8.12 shows a program to achieve this.

PROCESSING OF CUSTOMER LIST

Program

#define CUSTOMERS 10

main( )
{
char first_name[20][10], second_name[20][10],
surname[20][10], name[20][20],
telephone[20][10], dummy[20];

int i,j;

printf("Input names and telephone numbers \n");


printf("?");
for(i=0; i < CUSTOMERS ; i++)
{
scanf("%s %s %s %s", first_name[i],
second_name[i], surname[i], telephone[i]);

/* converting full name to surname with initials */

strcpy(name[i], surname[i] );
strcat(name[i], ",");
dummy[0] = first_name[i][0];
dummy[1] = '\0';
strcat(name[i], dummy);
strcat(name[i], ".");
dummy[0] = second_name[i][0];
dummy[1] = '\0';
strcat(name[i], dummy);
}
/* Alphabetical ordering of surnames */

for(i=1; i <= CUSTOMERS-1; i++)


for(j=1; j <= CUSTOMERS-i; j++)
if(strcmp (name[j-1], name[j]) > 0)
{
/* Swaping names */
strcpy(dummy, name[j-1]);
strcpy(name[j-1], name[j]);
strcpy(name[j], dummy);

/* Swaping telephone numbers */


strcpy(dummy, telephone[j-1]);
strcpy(telephone[j-1],telephone[j]);
strcpy(telephone[j], dummy);
}
/* printing alphabetical list */
printf("\nCUSTOMERS LIST IN ALPHABETICAL ORDER \n\n");
for(i=0; i < CUSTOMERS ; i++)
printf(" %-20s\t %-10s\n", name[i], telephone[i]);
}

Output

Input names and telephone numbers


?Gottfried Wilhelm Leibniz 711518
Joseph Louis Lagrange 869245
Jean Robert Argand 900823
Carl Freidrich Gauss 806788
Simon Denis Poisson 853240
Friedrich Wilhelm Bessel 719731
Charles Francois Sturm 222031
George Gabriel Stokes 545454
Mohandas Karamchand Gandhi 362718
Josian Willard Gibbs 123145

CUSTOMERS LIST IN ALPHABETICAL ORDER

Argand,J.R 900823
Bessel,F.W 719731
Gandhi,M.K 362718
Gauss,C.F 806788
Gibbs,J.W 123145
Lagrange,J.L 869245
Leibniz,G.W 711518
Poisson,S.D 853240
Stokes,G.G 545454
Sturm,C.F 222031

Fig.8.12 Program to alphabetize a customer list

You might also like