0% found this document useful (0 votes)
38 views

Lab 10 Strings

The document describes a lab module on strings in C programming. It introduces strings and string functions, explains how to declare and initialize string variables, and provides examples of string processing programs. It also lists tasks for students to write programs that manipulate strings, such as counting characters in a string, reversing a string, filtering strings by their starting character, and extracting web addresses from input text.

Uploaded by

ariff mohd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views

Lab 10 Strings

The document describes a lab module on strings in C programming. It introduces strings and string functions, explains how to declare and initialize string variables, and provides examples of string processing programs. It also lists tasks for students to write programs that manipulate strings, such as counting characters in a string, reversing a string, filtering strings by their starting character, and extracting web addresses from input text.

Uploaded by

ariff mohd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

EKT 120 – Computer Programming Laboratory Module

LAB 10
STRINGS

School of Computer and Communication Engineering


Universiti Malaysia Perlis
1
EKT 120 – Computer Programming Laboratory Module

1. OBJECTIVES:

1.1 To introduce the string data structure.


1.2 To be able to declare and initialize strings variables.
1.3 To be able to use string and string processing functions in programs.
1.4 To understand the two dimensional (2-D) arrays of type of characters which build up a list of
strings.

2. INTRODUCTION:

Array is a collection or a data structure of a fixed number of components where in all of the
components are of the same type.

The data type string is a programmer-defined and is not part of the C language. The C standard
library supplies it.

Strings can be treated as array of type char used to store names of people, places, or anything that
involves a combination of letters. Numbers can be stored as character, a string can be an array of
numbers, too.

To use the data type string, the program must include the header file string.
#include <string.h>

To declare a string, use the below command:

char string1[10];

The variable string1 will hold strings from 0 to 9 characters long.


If we initialize the above variable with “Welcome” by using the below command

char string1[10] = {“Welcome”};

The illustration of the above array is given as:

Index / Subscript
0 W
1 e
2 l
3 c
4 o
5 m
6 e
7 \0
8
9

Notice that string1[7] contains the character „\0‟, the null character marks the end of a string.

2
EKT 120 – Computer Programming Laboratory Module

2.1 Two Dimensional (2-D) Array of Character

char namelists[10][50];

The above data structure of array can store a list of names or strings; it can store 10 names or strings
with the size of up to 50 characters.

2.2 Sample Program

The program shows how to declare, initialize and display strings. [Note : The function “sizeof” is to get
the variable size in term of byte and the function “strlen” is to find the length of the string]

#include <stdio.h>
#include <string.h>

int main( )
{
//3 ways of declaring and initializing strings
char string1[10] = {“Welcome”};
char string2[] = {'W','e','l','c','o','m','e','\0'};
char string3[] = "Good Bye";

printf ("\nDisplay content of string1 : %s”,string1);


printf ("\nDisplay content of string2 : %s”,string2);
printf ("\nDisplay content of string3 : %s”,string3);

printf ("\n\nSize of string1 is %d",sizeof (string1)); //size of string1


printf ("\nSize of string2 is %d",sizeof (string2)); //size of string2
printf ("\nSize of string3 is %d",sizeof (string3)); //size of string3

printf ("\n\nLength of string1 is %d",strlen(string1)); //length of string1


printf ("\nLength of string2 is %d",strlen(string2)); //length of string2
printf (“\nLength of string3 is %d\n”,strlen(string3)); //length of string3

return 0;
}

Sample output of the program.

Display content of string1 : Welcome


Display content of string2 : Welcome
Display content of string3 : Good Bye

Size of string1 is 10
Size of string2 is 8
Size of string3 is 9

Length of string1 is 7
Length of string2 is 7
Length of string3 is 8

3
EKT 120 – Computer Programming Laboratory Module
2.3 The string library provides many functions for string processing. Find out what each of the string
function listed below does:

i. sizeof -operator displays the number of byte for all characters including the null
character („0‟) and the blank or space character in a string.

ii. strlen -function returns the number of characters in a string, but the terminating null
character is not included in the length of the string.
iii.
iv. strcpy - copies its second argument (a string) into its first argument (a character
array).

v. strcmp - compares its first string argument to its second string argument character-
by-character. The function returns 0 if the strings are equal, returns a negative value
if the first string is less than the second string and returns a positive value if the first
string is greater than the second string.

vi. strcat – appends its second argument (a string) to its first argument (a character array
containing a string).

vii. atoi - converts its argument; a string beginning with a series of digits that
represents an integer-to an int value.

viii. atof - converts its argument; a string beginning with a series of digits that
represents a floating-point number-to a double value.

ix. getchar - reads a single character from the standard input and returns the character
as an integer.

x. putchar – prints a character argument.

xi. gets - reads characters from the standard input until a newline character or the end-
of-file indicator is found.

xii. puts – takes a string as an argument and prints the string followed by a newline
character.

2.4 The program shows how to assign values into strings variables.

#include <stdio.h>
#include <string.h>

#define STRING_LENGTH 20

int main( )
{
char name[STRING_LENGTH];
float marks[3];
float total = 0;
int i;

printf("\nEnter student name : ");


scanf ("%s",name); /*to store the input value to variable name*/
/*note that there is no "&" sign at beginning of name, since
name is an array and holds the address value.*/
for (i=0;i<3;i++)
{
printf("Enter student test %d : ",i+1);
scanf("%f",&marks[i]);
total = total + marks[i];
}

/*Display information*/
printf ("\nStudent name : %s", name);
4
EKT 120 – Computer Programming Laboratory Module
for (i=0;i<3;i++)
printf ("\nTest %d mark : %5.2f",i+1, marks[i]);
printf("\n\nTotal marks for %s : %5.2f\n",name,total);
return 0; }

Sample output of the program.

Enter student name : John


Enter student test 1 : 70
Enter student test 2 : 80
Enter student test 3 : 65

Student name : John


Test 1 mark : 70.00
Test 2 mark : 80.00
Test 3 mark : 65.00

Total marks for John : 215.00

3. TASKS
3.1 Write a function fnCountChars ( ) with the prototype

int fnCountChars (const char *acStr, char cCh);

that returns how many times a certain character is found in a string. For example, if the input string is
“Under the sun” and the input character is „u‟, the returned value is 1.

5
EKT 120 – Computer Programming Laboratory Module
3.2 Write a function fnReverseString ( ) with the prototype

void fnReverseString ( const char *acS1, char *acS2);

String acS1 is the input and string acS2 is the output. They are the same except that letters in the
string are in reverse order. For example, if acS1 is “letter”, the function makes acS1 as “rettel”.

3.3 Write a program that reads four strings and prints only those strings beginning with the letter „b‟.

6
EKT 120 – Computer Programming Laboratory Module
3.4 Write a program to extract Web address starting with www. and ending with .my. The program
displays Web addresses contained in the input entered by the user.

Sample output 1:
Please input a string with URL address such as
"http://www.programming.com\
http://www.unimap.edu.my
www.unimap.edu.my

Sample output 2:
Please input a string with URL address such as
"http://www.programming.com\
<a href=http://www.kukum.edu.my> KUKUM </a>
www.kukum.edu.my

7
EKT 120 – Computer Programming Laboratory Module
3.5 Write a program that takes a list of students‟ names and marks and calculates the average marks.
You are required to declare two arrays called names and marks. Assume number of students are
5.
Declare array names: (use 2-D array)
char names[num_std][name_len]; //name length can be 20 characters long
Declare array marks:
float marks[num_std];
Sample output:

Enter student name : Jason


Enter student marks : 60
Enter student name : Ahmad
Enter student marks : 77
Enter student name : Chong
Enter student marks : 88
Enter student name : Kumar
Enter student marks : 70
Enter student name : Daniel
Enter student marks : 55

Jason 60.00
Ahmad 77.00
Chong 88.00
Kumar 70.00
Daniel 55.00

Average marks for 5 students : 70.00

8
EKT 120 – Computer Programming Laboratory Module
3.6 Write a program that resembles a phone book, which stores and displays the names, the
addresses (cities) and the telephone numbers for 10 people.

9
EKT 120 – Computer Programming Laboratory Module
3.7 The equation relating current, voltage and resistance in a circuit is I=V/R, where I = current, V =
voltage, and R = resistance. Write a program that accepts two of the three from the keyboard,
computes the third, and prints it to the screen. Enter the data in the form I=2.3. Allow input in any
order.

Sample output 1:

Please enter I, V, or R in the form of I=2.3


I=2.3
Please enter V or R in the form of R=4.0
R=4.5

Your input are


I=2.3
R=4.5

The output is
I= 2.30, R= 4.50, V=10.35

Sample output 2:

Please enter I, V, or R in the form of I=2.3


I=2.3
Please enter V or R in the form of R=4.0
R=4.5

Your input are


I=2.3
R=4.5

The output is
I= 2.30, R= 4.50, V=10.35

10

You might also like