strings
TRANSCRIPT
STRINGS
Prepared By:-
Imad Ali
Mcs Morning(2013-15)
Roll no:- 608
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.
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];
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”;
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.
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;
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);
Cin.get()The cin.get() is used to input a single
character.
syntax:-
Cin.get(ch);
E.G;
Cin.get(c);
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];
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’};
Or By assigning complete string
Example:-
Char str[3][10]={“Ali”,
“Abdullah”,
“Usman”};
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
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.
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…………
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);
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();}
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:-
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.
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);
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();
}
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);
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);
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;
}
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);
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;
}
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;
}
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);
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);
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
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
Stroll() Function
• The stroll() function is used to compare two strings using the collating sequence specified by setlocale function.
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");
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);