C++ Strings

STRINGS


1. String is a group of characters

2. A group of character can be stored in a character array.

3. A string is an array of characters or character array.

4. Strings are used in Programming languages for storing and manipulating text, such as words, names, sentences.

5. End of the string is marked by the NULL (‘\0’) character.

6. String constants are enclosed in double quotes

Example: “Hello world”

A string is stored in memory by using ASCII codes of the characters that form the

string.

String Representation in Memory String Declaration:

An array of characters representing a string is defined as follows:
char array_name[size];

size of the array must be an integer value.

Eg. Char name[50]; Defines an array & reserves 50 bytes of memory for string a set of characters. The length of the string cannot exceeds49 since; one storage location must be reserved for string the end of the string marker.

Initialization at compile time:

Char array_name = {list of characters separated by comma};

For example:

Char month = {‘A’,’P’,’R’,’I’,’L’,’/0’}; Or Char month = “April”;

It has the same effect as the above statements.

The compiler takes care of storing the ASCII codes of the characters of the string in memory, and also stores the NULL character at the end.

Example: Read & Display a string

#include<iostream.h> void main()

{

char name[50];

cout<<”ENTER YOUR NAME<49-max>”; cin>>name;

cout<<”Your name is ”<<name;

}

Run:

Enter your name : Archanna Your name is Archanna

In main () the statement in >> name; reads characters & stores them into the

variable name cout << name outputs the contents of the string variable name.

String Manipulation

C++ has several built-in fuctions such as strlen(), strcat(), strcmp(), strcpy()..etc, for string manipulation.

To use these functions, the header file string.h must be included in the program using the statement.

#include<string.h>

String Length

The string function strlen() returns the length of a given string.

The length of the string excludes the end of string character(‘\0’).

Syntax: int var=strlen(string variable);

Example:

#include<iostream.h>

#include<string.h> void main()

{

char s1[20];

cout<<”Enter Your name”;
cin>>s1;

cout<<”Strlen(s1)”<<strlen(s1);

}

Run:

Enter Your name: Smrithi Strlen(s1):7

String Copy

The string function strcpy() copies the contents one string to another.

Syntax: strcpy(string1, string2); string1àDestination String string2 à Source String Source string is copied into destination string.

Example:

#include<iostream.h>

#include<string.h> void main() {

char s1[20],s2[20]; cout<<”Enter Your name”; cin>>s1;

strcpy(s2,s1);

cout<<”Strcpy(s2,s1): content of s2 is:”<< s2;

}

Run:

Enter Your name: Smrithi Strcpy(s2,s1): content of s2 is: Smrithi

String concatenation:

The string function strcat() concatenates two string resulting in a single string. Syntax: strcat(string1, string2); string1àDestination String string2 à Source String

The destination & Source strings are concatenated and the resultant string is

stored in the destination string.

Example:

#include<iostream.h>

#include<string.h>
void main()

{

char s1[20],s2[20];

cout<<”Enter Your first name”; cin>>s1; cout<<”Enter Your second name”; cin>>s2; strcat(s2,s1);

cout<<”Strcat(s2,s1): content of s2 is:”<< s2;

}

Run:

Enter Your First name: Roy Enter Your Second name: Joy Strcat(s2,s1): content of s2 is: RoyJoy

String Compare:

The string function strcmp() compares two strings, character by character.

It returns an integer, whose value is

< 0 if first string is alphabetically higher than second string.

= = 0 If both are identical

> 0 if first string is alphabetically lower than second string.

Syntax: strcmp(string1,string2);

ASCII codes of each character in the given strings are compared. Once it find

mismatch ASCII code it stop comparing and returns the difference between the ASCII value of mismatched string is returned.

Example:

#include<iostream.h>

#include<string.h> void main()

{

char s1[20],s2[20]; cout<<”Enter name1”; cin>>s1; cout<<”Enter name2”; cin>>s2; int i=strcmp(s2,s1);

if( i= = 0 )
cout<<”Name1 and name2 are same”; else if(i<0)

cout<<” Name1 is alphabetically higher than name2 ”; else

cout<<” Name1 is alphabetically lower than name2 ”;

}

Run1:

Enter name1: Roy Enter name2: Joy Name1 is alphabetically lower than name2 Run2:

Enter name1: Roy Enter name2: Roy Name1 and name2 are same

Run3:

Enter name1: Roy Enter name2: roy Name1 is alphabetically higher than name2

ARRAYS OF STRINGS

An array of strings is a two dimensional array of characters and is defined as follows:

Syntax: char arry_name[row size][col size];

row size-> No of strings can be stored, col size-> no of characters in each string. Example: char person[10][15]; à It defines an array of strings which can store name of 10 persons and each name cannot exceeds 14 characters. Last one character is used to represents the end of a string.

Example: Array of strings storing names

#include<iostream.h>

#include<string.h> void main()

{

char person[10][15];

cout<<”How many persons <max-10>”; cin>>n; for(int i=0;i<n;i++)
{

cout<<”enter person”<<i+1<<endl; cin>>person[i];

}

cout<<”Person names”; for( i=0;i<n;i++)

{

cout<<person[i]<<endl;

}

}

Run:

Enter Persons <max-100> 2

Enter person1: Roy Enter person2: Joy Person names

Roy Joy

أحدث أقدم