Presentation is loading. Please wait.

Presentation is loading. Please wait.

String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of.

Similar presentations


Presentation on theme: "String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of."— Presentation transcript:

1 String functions+ string I.Mona Alshehri

2 String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of a string. Return Value strlen returns the number of characters in s, not counting the null-terminating character. Example: #include #include #include void main(void) { char s[30]= "Borland International"; cout<<strlen(s); //print 21 getch(); }

3 String Functions: Header file:#include Function: strcpy(char s1[n],char s2[m]) Description Copies one string into another. Copies string s2to s1 stopping after the terminating null character has been moved. Return Value strcpy returns s1. Example: #include #include #include void main(void) { char string[10]; char str1[13] = "abcdefghi"; strcpy(string, str1); cout<<string; //print abcdefghi getch(); }

4 String Functions: Header file:#include Function: strcat(char dest[n], char src[m]); Description Appends one string to another. strcat appends a copy of src to the end of dest. The length of the resulting string is strlen(dest) + strlen(src). Return Value strcat returns a pointer to the concatenated strings. #include void main() { cout<<strcat("GOOD","MORNING"); getch(); } GOODMORNING Program Output

5 String Functions: Header file:#include strcmp() function It subtracts the ascii code of the first letter of the first string from the first letter of the second string, if the result is zero, it means that the letters are equal, and then it goes on to the second letters, whenever the result is non-zero, it means that the letters are not equal, so the comparison will stop. Example: Strcmp(COMPUTER,COMpUTER); 67-67=0 (Ascii code of C = 67) 79-79=0 (Ascii code of O = 79) 77-77=0 (Ascii code of M = 77) 80-112=-32 (Ascii code of P = 80 & Ascii code of p=112) STOP, The words are not equal.

6 String Functions: Example : #include void main() { char msg1[ ]="Hello"; char msg2[ ]="HELLO"; cout<<strcmp(msg1,msg2); cout<<strcmp(msg2,msg1); getch(); } Program Output 32 -32

7 Example: Write a program that reads a line of text (string), and prints in table the number of capital letters and small letter in the input text? Examples: Enter Name: Salam it is a nice Home work Capital letter= 2 Small letter= 20

8 Solution: #include void main() { char word[20]; int capital=0,smal=0; cout<<"Enter the name:"; gets(word); for(int i=0;word[i]!=‘\0’;i++) if(word[i]>=' A‘ && word[i]<=‘Z’) capital++; if(word[i]>=‘a’ && word[i]<=‘z’) smal++; cout<<“Capital letter=“<<capital<<“\nSmall letter =“<<smal; getch(); }

9 Example: Write a program that reads a line of text (string), and prints in table the number of 3, 4, 5, and 6 letter words appearing in the text ? Examples: Text: Salam it is a nice home work Table : word length Number of words 30 43 51 60

10 Solution: #include void main() { char word[20]; int j=0,j3=0,j4=0,j5=0,j6=0; cout<<"Enter the sentance:"; gets(word); int len=strlen(word); for(int i=0;i<=len;i++) if(word[i]==' '|| i==len) switch(j) { case 3:{j3++;j=0;continue;} case 4:{j4++;j=0;continue;} case 5:{j5++;j=0;continue;} case 6:{j6++;j=0;continue;} } else j++; cout<<"Word lenth "<<"NO. of Word\n"; cout<<"3 "<<j3<<"\n"; cout<<"4 "<<j4<<"\n"; cout<<"5 "<<j5<<"\n"; cout<<"6 "<<j6<<"\n"; getch(); }


Download ppt "String functions+ string I.Mona Alshehri. String Functions: Header file:#include Function: Int strlen(char s[n]) Description Calculates the length of."

Similar presentations


Ads by Google