HANDLING OF CHARACTER STRINGS
HANDLING OF CHARACTER STRINGS
INTRODUCTION:
A string is
an array of characters. Any group of characters defined between double
quotation marks is a constant string.
Example:
“Man is
obviously made to think.”
For example,
printf(“\“Well
Done!\” ”);
will output the
string
“Well Done!”
while the
statement
printf(“WellDone!”);
will output the
string
WellDone!
Character
strings often used to build meaningful and readable programs.
The common
operations performed on character strings are:
Reading and Writing strings
Combining strings together.
Copying one string to another.
Comparing strings for equality.
Extracting a portion of a
string.
DECLARING AND INITIALIZING STRING
VARIABLES:
A string
variable is any valid C variable name and is always declared as an array. The
general form of declaration of a string variable is
char
string_name[size];
The size
determines the number of characters in the string-name.
Some examples
are:
char city[10];
char name[30];
Reading a Line
of Text:
In many text
processing applications, we need to read in an entire time of text from the terminal. It is not possible
to use scanf function to read a line
containing more than one word. This is because the scanf terminates
reading
as soon as a
space is encountered in input. We have learned to read a single character from
the terminal, using the
function getchar.
WRITING STRINGS
TO SCREEN
We have used
the printf function with %s format to print strings to the screen. The format %s can be used to display
an array of characters that is terminated by the null character. For example,
the statement
printf(“%s”,
name);
can be used to
display the entire contents of the array name.
The
following features of the %s specifications.
1. When the
field width is less than the length of the string, the entire string is
printed.
2. The integer
value on the right side of the decimal point specifies the number of characters
to be printed.
3. When the
number of characters to be printed is specified as zero, nothing is printed.
4. The minus
sign in the specification causes the string to be printed left-justified.
printf(“%*.*s\n”, w,
d, string);
OPERATIONS ON CHARACTERS
C allows
us to manipulate characters the same way we do with numbers. Whenever a
character constant or character variable is used in an expression, it is
automatically converted into an integer value by the system. The integer value
depends on the local character set of the system. To write a character in its
integer representation, we may write it as an integer. For example, if the
machine uses the ASCII representation, then,
x = ‘a’;
printf(“%d\n”,
x);
will display
the number 97 on the screen.
In ASCII character
set, the decimal numbers 65 to 90 represent upper-case alphabets (A-Z) and 97
to 122 represent lower-case alphabets (a-z).
The values from
91 to 96 are excluded using an if statement in the for loop.
The C labrary
supports a function that converts a string of digits into their integer values.
The function
takes the form
x =
atoi(string);
STRING - HANDLING FUNCTIONS
The C library
supports a large number of string-handling functions that
can be used to
carry out many of the string manipulations. Following are the
most commonly
used string-handling functions.
Function Action
strcat() concatenates two strings
strcmp() compares two strings
strcpy() copies one string over another
strlen() finds the length of a string.
strrev() reversing the string.
strlwr() converts upper case to lower
case.
strupr() converts lower case to upper
case.
strcat() Function
The strcat
function joins two strings together. It takes the following
form: strcat(string1, string2);
C permits
nesting of strcat functions. For example, the statement
strcat (strcat (string1,
string2), string3);
#include<string.h>
#include<stdio.h>
void
main()
{
char destination[25]="This is
",c[8] = " Turbo ", turbo[5]=" C++";
clrscr();
strcat(destination,c);
strcat(destination,turbo);
printf("%s\n",destination);
getch();
}
strcmp() Function
The strcmp
function compares two strings identified b the arguments and has a value 0 if
they are equal. If they are not, it has the numeric difference between the
first non matching characters in the strings. It takes the form:
strcmp(string1,
string2);
string1 and
string2 may be string variables or string constants. Examples are:
strcmp(“Rom”, “Ram”);
#include<string.h>
#include<stdio.h>
void
main()
{
char buf1[10],buf2[10];
int ptr;
clrscr();
printf("enter two strings :");
scanf("%s %s",buf1,buf2);
ptr=strcmp(buf2, buf1);
printf("diff =%d\n",ptr);
if(ptr==0)
printf("two strings are equal\n
");
else if (ptr > 0)
printf("\nbuffer 2 is greater than
buffer 1\n");
else
printf("buffer 2 is less than
buffer 1\n");
getch();
}
strcopy() Function
The strcpy function
works almost like a string-assignment operator. It takes the form
strcpy(string1,
string2);
For example,
the statement
strcpy(city,
“DELHI”);
#include<stdio.h>
#include<string.h>
void
main()
{
char string[10];
char str1[10]="abcdefghi";
clrscr();
strcpy(string,str1);
printf("%s\n", string);
getch();
}
strlen() Function
This function
counts and returns the number of characters in a string.
n =
strlen(string);
where n is an integer variable which receives
the value of the length of the string.
#include<stdio.h>
#include<conio.h>
#include<string.h>
void
main()
{
int l=0;
char string[25]="Borland
International";
clrscr();
l=strlen(string);
printf("%d\n",l);
getch();
}
/* A program is
to find the reverse of a string */
#include<stdio.h>
#include<string.h>
#include<conio.h>
void
main()
{
char forward[10] = "string";
clrscr();
printf("Before strrev():
%s\n",forward);
strrev(forward);
printf("After strrev(): %s\n",forward);
getch();
}
/* A program is
to convert upper case to lower case */
#include
<stdio.h>
#include
<string.h>
void
main()
{
char string[25] =
"BorlandInternational";
clrscr();
printf("string prior to strlwr:
%s\n", string);
strlwr(string);
printf("string after strlwr: %s\n", string);
getch();
}
/* A program is
to find the converts lower case to upper
case */
#include
<stdio.h>
#include
<string.h>
void
main()
{
char string[25] =
"BorlandInternational";
clrscr();
printf("string prior to strupr:
%s\n", string);
strupr(string);
printf("string after strupr: %s\n", string);
getch();
}
Comments
Post a Comment