Loading....
Coupon Accepted Successfully!

 

Introduction

A string is nothing but a sequence of characters. Strings can contain small and capital letters, numbers, and symbols. A string is to be treated as an array of characters with terminating \0. Each element of a string occupies a byte in the memory. Every string is terminated by a null character. The last character of such a string is a null (‘\0’) character, and the compiler identifies the null character during the execution of the program. The programmer need not take care of the null character. In a few cases, the null character is to be specified explicitly. The null character is represented by ‘\0,’ which is the last character of a string. The null (\0) character is a byte with all bits at logic zero. Hence, ASCII and Hex values are zero. The string is stored in the memory in the following manner:
char country[6] =“INDIA”;//Declaration and initialization of an array country with 6 ­characters
Here, text “INDIA” is assigned to an array country [6]. The text is enclosed within double quotation marks.

I

N

D

I

A

‘\0’

73

78

68

73

65

00

As shown above, each character occupies a single byte in the memory. At the end of the string, a null character is inserted by the compiler. The first row shows the elements of the string, and the second row shows their corresponding ASCII values. The complier takes care of ­storing the ASCII numbers of the characters in the memory. In addition, the ASCII value of the null character is also stored in the memory.
The programmer can use the string in a program for storing and controlling the text. The text comprises words, sentences, names, and so on. The various operations with strings such as copying, comparing, concatenation, or replacing require a lot of effort in ‘C’ programming. These strings are called a C-style string. In ‘C,’ the string functions are declared in the string.h header file. Thus, using this header file, string manipulation is done. C++ also supports c style ­functions. Table 18.1 describes these functions.
130697.jpgTable 18.1 String library functions

Functions

Description

strlen()

Determines the length of a string

strcpy()

Copies a string from the source to the destination

strncpy()

Copies the characters of a string to another string up to the specified length

strcmp()

Compares the characters of two strings (Function discriminates between small & capital letters.)

stricmp()

Compares two strings (Function does not discriminate between small & capital letters.)

strncmp()

Compares the characters of two strings up to the specified length

strnicmp()

Compares the characters of two strings up to the specified length. Ignores case.

strlwr()

Converts uppercase characters of a string into lowercase

strupr()

Converts lowercase characters of a string into uppercase

strdup()

Duplicates a string

strchr()

Determines the first occurrence of a given character in a string

strrchr()

Determines the last occurrence of a given character in a string

strstr()

Determines the first occurrence of a given string in another string

strcat()

Appends the source string to the destination string

strncat()

Appends the source string to the destination string up to a specified length

strrev()

Reverses all the characters of a string

strset()

Sets all the characters of a string with a given argument or symbol

strnset()

Sets a specified number of characters of a string with a given argument or symbol

strspn()

Finds up to what length two strings are identical

strpbrk()

Searches for the first occurrence of a character in a given string and then, displays the string starting from that character

The following program explains the use of the above functions:

18.1 Write a program to declare string (character array). Read string through the ­keyboard and count the length of the string using string library function.

#include<iostream.h>

#include<constream.h>

#include<string.h>

int main()

{

char name[15];

clrscr();

cout<<“\n Enter Your name:”;

cin>>name;

cout<<“\n Length of name is:”<<strlen(name);

return 0;

}

OUTPUT

Enter Your name : Suraj

Length of name is : 5

Explanation: In the above program, the string is declared using the statement char name [15]; using the library function strlen(), the total number of charcters entered is displayed.

18.2 Write a program to display reverse of entered string.

#include<iostream.h>

#include<constream.h>

#include<string.h>

int main()

{

char name[15];

clrscr();

cout<<“\nEnter Your name:”;

cin>>name;

cout<<“\n Reverse string is:”<<strrev(name);

return 0;

}

OUTPUT

Enter Your name : Akash

Reverse string is : hsakA

Explanation: In the above program, a character array name[] is declared. The string is entered through the keyboard. The strrev() function displays a reverse string.

18.3 Write a program to initialize a string using different formats.

#include<iostream.h>

#include<constream.h>

#include<string.h>

int main()

{

clrscr();

char text[]=“Welcome”; // Using double quote

char text1[]={‘W’,‘e’,‘l’,‘c’,‘o’,‘m’,‘e’,‘\0’}; // using single quote

cout<<“\n First string:”<<text;

cout<<“\n Second string:”<<text1;

return 0;

}

OUTPUT

First string : Welcome

Second string : Welcome

Explanation: In this program, two methods of initialization of arrays are used. Here, both declaration and initialization are done in the same statement.

char text[]=“Welcome”;

In this statement, the array text[] is declared, and it is initialized with the string “Welcome.” It is an easy way to initialize the character array. It is not necessary to include the null character. Here also, the size of the array is not mentioned. When declaration and initialization are done in the same statement, the compiler determines the size of the array. Hence, it is optional to mention the size of the array in the subscript[] brackets.

char text1[]={‘W’,‘e’,‘l’,‘c’,‘o’,‘m’,‘e’,‘\0’}; // using single // quotes

A character array (string) can also be initialized as shown in the above statement. However, in this method, each character should be included in single quotation marks and should be separated by commas. The string should be terminated by the null character. Hence, the second method is difficult as compared with the first. Here, the programmer has to specify the null character at the end of the string.





Test Your Skills Now!
Take a Quiz now
Reviewer Name