Lab 10 Strings
Lab 10 Strings
LAB 10
STRINGS
1. OBJECTIVES:
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>
char string1[10];
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
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.
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";
return 0;
}
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.
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;
/*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; }
3. TASKS
3.1 Write a function fnCountChars ( ) with the prototype
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
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:
Jason 60.00
Ahmad 77.00
Chong 88.00
Kumar 70.00
Daniel 55.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:
The output is
I= 2.30, R= 4.50, V=10.35
Sample output 2:
The output is
I= 2.30, R= 4.50, V=10.35
10