LabManual 08
LabManual 08
LAB MANUAL 08
CSC1102
Programming Language 1 [EEE]
Prepared By:
M. ARIFUR RAHMAN
1
TITLE
PREREQUISITE
OBJECTIVE
THEORY
STRING
The way a group of integers can be stored in an integer array, similarly a group of characters can
be stored in a character array. Character arrays are many a time also called strings. Character
arrays or strings are used by programming languages to manipulate text such as words and
sentences.
Each character in the array occupies one byte of memory and the last character is always ‘\0’.
What character is this? It looks like two characters, but it is actually only one character, with the
\ indicating that what follows it is something special. ‘\0’ is called null character. Note that ‘\0’
and ‘0’ are not same. ASCII value of ‘\0’ is 0, whereas ASCII value of ‘0’ is 48. The terminating null
2
(‘\0’) is important, because it is the only way the functions that work with a string can know
where the string ends. In fact, a string not terminated by a ‘\0’ is not really a string, but merely a
collection of characters. C concedes the fact that you would use strings very often and hence
provides a shortcut for initializing strings. For example, the string used above can also be
initialized as, char name[ ] = "HAESLER";
Note that, in this declaration ‘\0’ is not necessary. C inserts the null character automatically.
Even though there are many ways to refer to the elements of a character array, rarely is any one
of them used. This is because printf( ) function has got a simple way of doing it, as shown below.
Note that printf( ) doesn’t print the ‘\0’.
#include<stdio.h>
void main( )
{
char name[ ] = "Klinsman";
printf ( "%s", name );
}
The %s used in printf( ) is a format specification for printing out a string. The same specification
can be used to receive a string from the keyboard, as shown below.
#include<stdio.h>
void main( )
{
char name[25];
printf ( "Enter your name " );
scanf ( "%s", name );
printf ( "Hello %s!", name );
}
The function scanf( ) is not capable of receiving multi-word strings. Therefore names such as
‘Harry Potter’ would be unacceptable. The way to get around this limitation is by using the
function gets( ). The usage of functions gets( ) and its counterpart puts( ) is shown below.
#include<stdio.h>
3
#include<string.h>
void main( )
char name[25];
gets ( name );
puts ( "Hello" );
puts ( name );
Suppose we wish to store “Hello”. We may either store it in a string or we may ask the C compiler
to store it at some location in memory and assign the address of the string in a char pointer. This
is shown below:
#include<stdio.h>
#include<string.h>
void main( )
char *p = "Hello";
puts(str);
puts(p);
There is a subtle difference in usage of these two forms. For example, we cannot assign a string
to another, whereas, we can assign a char pointer to another char pointer. This is shown in the
following program.
4
#include<stdio.h>
#include<string.h>
void main( )
{
char str1[ ] = "Hello";
char str2[10];
char *s = "Good Morning";
char *q; //
str2 = str1; /* error */
q = s; /* works */
puts(q);
}
Also, once a string has been defined it cannot be initialized to another set of characters. Unlike
strings, such an operation is perfectly valid with char pointers.
#include<stdio.h>
#include<string.h>
void main( )
char *p = "Hello"; //
p = "Bye"; /* works */
puts(p);
With every C compiler a large set of useful string handling library functions are provided. Out of
them we shall discuss the functions strlen( ), strcpy( ), strcat( ), strcmp( ) and strrev(), since these
are the most commonly used functions. This will also illustrate how the library functions in
general handle strings. Let us study these functions one by one.
5
STRLEN()
This function counts the number of characters present in a string. Its usage is illustrated in the
following program.
#include<stdio.h>
#include<string.h>
void main()
char name[80];
printf("Enter a sentence:\n");
gets(name);
int len;
len=strlen(name);
STRCPY()
This function copies the contents of one string into another. The base addresses of the source
and target strings should be supplied to this function. Here is an example of strcpy( ) in action.
#include<stdio.h>
#include<string.h>
void main()
char source[]="C";
char target[20];
strcpy(target,source);
6
printf("target string %s",target);
STRCAT()
This function concatenates the source string at the end of the target string. Here is an example
of strcat( ) at work.
#include<stdio.h>
#include<string.h>
void main()
strcat(target,source);
STRCMP()
This is a function which compares two strings to find out whether they are same or different. The
two strings are compared character by character until there is a mismatch or end of one of the
strings is reached, whichever occurs first. If the two strings are identical, strcmp( ) returns a value
zero. If they’re not, it returns the numeric difference between the ASCII values of the first non-
matching pairs of characters. Here is a program which puts strcmp( ) in action.
#include<stdio.h>
#include<string.h>
void main()
7
int i,j;
i=strcmp(str1,"Let Us C");
j=strcmp(str1,str2);
printf("%d \n",i);
printf("%d",j);
STRREV()
This function reverses the order of the characters present in a string. Its usage is illustrated in the
following program.
#include<stdio.h>
#include<string.h>
char name[100];
int main()
char name[100];
gets(name);
strrev(name);
printf("reverse is:\n%s",name);
EXERCISE
8
IV. function copies the contents of one string into another.
V. The strcat() function the source string at the end of the target string.
LAB WORK
I. Write a program that asks user to input his/her full name and outputs the name in
reverse. Hint: Use STRREV()
II. Write a program that asks user to input his/her two favorite subjects and after
concatenating the 1st subject at the end of the 2nd subject gives output. Hint: Use
STRCAT()
III. Write a program that stores the name of a food in a string called source but outputs the
same name in another string called target . Hint: Use STRCPY()
IV. There are two resistors in a circuit. R1=four ohm and R2= four ohm. Write a program
that compares R1 and R2 to find out whether they are same or different. Hint: Use
STRCMP()
V. There are two resistors in a circuit. R1=four ohm and R2= five ohm. Write a program that
compares R1 and R2 to find out whether they are same or different. Hint: Use STRCMP()
VI. Write a program that asks user to input his/her full name and outputs the number of
characters that are present in the name. Hint: Use STRLEN()
ASSIGNMENT
I. There are two resistors in a circuit. R1=four ohm and R2= four ohm. Write a program
that compares R1 and R2 to find out whether they are same or different. Hint: Cannot
use STRCMP()
II. Write a program that asks user to input his/her full name and outputs the name in
reverse. Hint: Cannot use STRREV()