strings

33
STRINGS Prepared By:- Imad Ali Mcs Morning(2013-15) Roll no:- 608

Upload: imad-ali

Post on 25-May-2015

162 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Strings

STRINGS

Prepared By:-

Imad Ali

Mcs Morning(2013-15)

Roll no:- 608

Page 2: Strings

String

Q. What is string?A collection of characters written with in

double quotations is called string or constant.

A string is stored as an array of character. The array is terminated by a special character known as null character. It is denoted by escape sequence ‘\0 ‘ and used to indicate the end of string.

Page 3: Strings

String Declaration

C++ stores string as an array of characters. An array is a group of contiguous memory location that can store same type of data.

Syntax:-

Char array_name[length];

E.G;

Char book[30];

Page 4: Strings

String Initialization

The process of assigning the values to string at the time of declaration.

Syntax:-

Char array_name[length]=value;

E.G;

Char book[20]=“String Handling”;

Page 5: Strings

String Input

A String value can be input from the user using different functions. C++ provides the following functions for string input:

1.Cin: General Purpose keyboard input.

2.Cin.getline():Input a string of characters from keyboard

3.cin.get():Input a single character from keyboard.

Page 6: Strings

Cin Function

Cin Function is used to get the input from the user.

Syntax:- cin>>str;E.G; int name[50]; cout<<“Enter As string:”; cin>>name;

Page 7: Strings

Cin .getline

The getline function of cin object to input any string value including blank spaces. The user can input any type of string value.

The main difference between cin and cin.getline is that cin function ignores white spaces and cin.getline includes them in the text.

Syntax:- cin.getline(str,len);E.G; cin.getline(name,50);

Page 8: Strings

Cin.get()The cin.get() is used to input a single

character.

syntax:-

Cin.get(ch);

E.G;

Cin.get(c);

Page 9: Strings

Array of StringsAn array of string is a two dimensional array

of characters. Each row of array represents one string. Each character in an array of strings is stored in separate index of a two dimensional array.

Syntax:-Char str[rows][cols];E.G;Char names[3][20];

Page 10: Strings

Initializing array of string

An array string can be initialized in different ways. It can be initialized by assigning individual character to each index in the array. It can also be initialized by assigning complete string to each row in array.

E.g; char str[3][5]={‘a’,’b’,’c’,’d’,’e’, ‘f’,’g’,’h’,’I’,’j’, ‘k’,’l’,’m’,’n’,’o’};

Page 11: Strings

Or By assigning complete string

Example:-

Char str[3][10]={“Ali”,

“Abdullah”,

“Usman”};

Page 12: Strings

Input/output with array string

Each row of two dimensional array of characters represents a complete string.

The values can be input by referencing the complete row of two dimensional array of characters as follows:

Char str[3][5];

Cin>>str[0]; // represents the first row in the array

Page 13: Strings

Output with array string

The values can be displayed by referencing the complete row of two dimensional array of characters as follows:

Char str[3][5];

Cout<<str[0]; //represents the first row in

the array.

Cout<<str[1]; //represents the second

row in array.

Page 14: Strings

String Functions

The header file ‘string.h’ contains built-in functions that are used to process string values. Some important functions defined in the header file are as follows:

Memchr();

Memcmp();

Memcpy();

Memmove(); etc…………

Page 15: Strings

Memchr() Function

The memchr() function is used to search a byte with a particular value in buffer. The search continues for count bytes or until the value is found. It returns a pointer to the first location of the value in the buffer. The function is NULL value if the value is not found.

Syntax:- memchr(buffer,ch,size);

E.G;void * result //here result is a pointer

Result=Memchr(test,search,12);

Page 16: Strings

Example:-

#include<iostream>#include<conio.h>#include<string.h>#include<stdio.h>using namespace std;int main(){char test[]="Test String";char ch;void * ch2;cout<<"Enter a Character to search:";cin>>ch;cout<<"the test string is:";cout<<test;ch2=memchr(test,ch,12);if(ch2!=NULL)cout<<"The character found in string";elsecout<<"The character not found in string";getch();}

Page 17: Strings

Memcmp() Function

It is used to compare each successive byte referenced by first pointer with the corresponding bytes referenced by second pointer. The comparison continues both pointers do not have same value or number of specified bytes have been compared . The function returns an integer value as follows:-

Page 18: Strings

Memcmp() Function

Return value DescriptionLess than 0 if first string takes less bytes than second

Greater than 0 if first string takes greater bytes than second.

Equal to 0 if first string takes bytes equal to second.

Page 19: Strings

Memcpy();

The memcpy() function is used to copy the number of specified character from first buffer to second buffer and return first buffer. Faster than memmove() function.

Syntax; memcpy(buffer2,buffer1,size);

E.G;

Char test1[]=“sampling string”;

Char test2[15];

Memcpy(test2,test1,15);

Page 20: Strings

Example:-#include<iostream>#include<conio.h>#include<stdio.h>#include<string.h>using namespace std;int main(){

char test1[]="This is it!!!";char test2[15];cout<<"The value of test 1 is;";puts(test1);cout<<"copying test1 to test2...\n";memcpy(test2,test1,15);cout<<"The value of test2 is:";puts(test2);getch();

}

Page 21: Strings

Memmove()

The memmove() function is also used to copy the number of specified characters from first buffer and copy them into second buffer and returns the first buffer.

It is slower than memcpy(). Cause it handles the overlapping moves.

Syntax:- memmove(buffer2,buffer1,size);

E.G:- memmove(test2,test1,15);

Page 22: Strings

Memset() Function

The memset() function is also used to set the first count characters referenced by buffer to the specified value. It returns the buffer.

Syntax:- memset(buffer,char,number);

Page 23: Strings

Example:-

#include<iostream>#include<conio.h>#include<string.h>#include<stdio.h>using namespace std;int main(){

char buffer[15]="A sam string";cout<<"\nBuffer before memset";puts(buffer);memset(buffer,'x',8);cout<<"\n Buufer after memset";puts(buffer);getch();return 0;

}

Page 24: Strings

Strcat() Function

The strcat() function is used to append a copy string to the end of second string.

It also terminates the resulting string with null character.The string in which the value is copied should have enough space to hold the result.

Syntax:- strcat(str1,str2);

E,G:- strcat(test1,test2);

Page 25: Strings

Example:-

#include<iostream>#include<conio.h>#include<string.h>#include<stdio.h>using namespace std;int main(){

char ch[15]="A sam string";char ch1[30]="@nd sample";strcat(ch1,ch);cout<<"After concatination:";puts(ch1);getch();return 0;

}

Page 26: Strings

Strncat() Function

The strncat() is used to append the specified number of values of one string to the end of second string. The string in which the values are copied have enough space to hold values.

Syntax:-Strncat(str1,str2,n);

#include<iostream>#include<conio.h>#include<string.h>#include<stdio.h>using namespace std;int main(){

char ch[15]="A sam string";

char ch1[30]="@nd sample";

strncat(ch1,ch,6);cout<<"After

concatination:";puts(ch1);getch();return 0;

}

Page 27: Strings

Strchr() Function

The strchr() fucntion is used to find the first occurrence of a character in string returns pointer to this character. it returns null value if character is not found.

Syntax:-strchr(str,chr);

Page 28: Strings

Strrchr() Function

The strrchr() function is used to find the last occurrence of a character in string and returns a pointer to this character. It returns null if value is not found

Syntax:-

strrchr(str,ch);

Page 29: Strings

Strcmp() and Stricmp() Function

The strcmp() function is used to compare the function character by character. The comparison is case sensitive.

The stricmp is also does the same function but the comparison in case sensitive.

It returns integer value as follows:Less than 0 S1<S2Greater than 0 S1>S2Equal to 0 S1=S2

Page 30: Strings

Strncmp() Function

The strncmp() function is used to compare the specified number of character in two strings. The comparison is case sensitive.

It returns integer value as follows:

1.Less than 0

2.Greater than 0

3.Equal to 0

Page 31: Strings

Stroll() Function

• The stroll() function is used to compare two strings using the collating sequence specified by setlocale function.

Page 32: Strings

Others Functions

strspn

Returns the number of matches found in the string.

Count = strspn(string, 'char');

strpbrk

Returns a character pointer to the first occurrence of characters in a string. It will return the pointer of which ever character comes first.

CharPointer = strpbrk(string, "chars");

strstr

Returns a character pointer to the first occurrence of a string in a string.

CharPointer = strstr(string, "string");

Page 33: Strings

Other Functions

_strlwr

Converts all characters to lower case.

_strlwr(string);

_strupr

Converts all characters to upper case.

_strupr(string);

strrev

Reverses the string.

strrev(string);