INFORMATION & COMPUTER SCIENCE DEPARTMENT, KFUPM

ICS – 201 SECTION 55 & 56  (992 Semester)

INTRODUCTION TO COMPUTER SCIENCE

LAB  #03  String & Character Processing

Instructor: Bashir M. Ghandi

Objective:       Review how to declare, initialize, and process strings & characters

Apply basic string /character functions in problem solving

 


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 NUL

·        Example:  Even though the string ”I like C” has 8 characters, we need to declare an array of size 9.

char str[9]=”I like C”;

’I’

’ ’

’l’

’i’

’k’

’e’

’ ’

’C’

’\0’

Note 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 NUL character to work correctly.

·         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

·        Like array, the individual characters of a string can be accessed by specifying the index.  Example,  assuming the above assignment, str[4] is the character ‘k’

 

String I/O

·        The easiest way to input string is by using the gets function

Example:          char name[81];

printf(”Input your name”);

gets(name);

gets allows you to read a string that could include blanks.  It terminates reading on encountering the return character

·        Also the easiest way to output string is by using the puts function as in:

puts(name);

·        For scanning from file, we have fgets(str, n, file) which expects 3 arguments: string variable,  number of characters to scan and the file to scan from.  If end of line is encountered before scanning the n characters, it stops.

·        Also we can print into a file using fputs(str, file).

·        You can also use the scanf/printf functions for string I/O using the %s format specifier.

·        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);

·        Note that the address of operator, &, is not used in the scanf function.

 

Built-in string functions:

Built-in string processing functions can be accessed through the header file, string.h .  Some of these are:

·        strcat(string1, string2) à appends strings2 to string1

·        strcpy(string1, string2) à replaces string1 with string2

·        strlen(string1) à returns the length of string1 excluding NUL

·        strchr(string, ch) à searches for the occurrence of ch in string

·        strcmp(string1, string2) à compares string1 and string2 and  returns:

o       Negative integer if string1 < string2

o       0 if string1 = = string2

o       positive integer if string1 > string2.

 

The following program illustrates the use of some 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:

·        Built-in character functions can be accessed through the header file, ctype.h

·        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

 

Program Exercises:

1.      (a)  The file LAB3_1.C reads a string from the user [maximum 80 charcters] and prints the number of uppercase letters and lower case letters in the string.  Copy the file from my home page, study it and test-run it

(b)  Modify the program in (a) above so that it also count the number of vowels in the string.  Note that your program should include a function isvowel() that returns true if a given character is a vowel.

(c)  Modify the program in (b) above so that it counts the number of vowels, upper case and lower case from the file TEXT.TXT.

2.      The file LAB2_2.C is a solution to question (2) of last week’s assignment (Lab 2).

      The file EXAMS2.DAT contains the same data as EXAMS.DAT but with names added.

      Assuming that the names are not more than 17 characters, modify Lab2_2.C so that it works on

      EXAMS2.DAT. (i.e the names are printed in the output).

 

Home Work:

1.      (a)  Study the function strtok() from the Help system and use it improve the program in 1. (c) so that it also counts the number of words in the file.  Note: A word is any sequence of characters followed by a blank. 

(b)   Assuming a sentence terminates with ‘.’, ‘?’ or ‘;’  let your program count the number of sentences in the file.

2.      Modify the employee program in last week’s home work to include the names of the employees.  Assume that the names are after the ID number in the input file.