100 likes | 218 Views
CPCS202- LAB9. String functions+ string I.Mona Alshehri. String Functions:. Header file:#include <string.h> 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
E N D
CPCS202- LAB9 String functions+ string I.Mona Alshehri
String Functions: Header file:#include <string.h> 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 <stdio.h>#include <string.h> #include <conio.h> #include <iostream.h> void main(void) { char s[30]= "Borland International"; cout<<strlen(s); //print 21 getch(); }
String Functions: Header file:#include <string.h> 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 <stdio.h>#include <string.h> #include <conio.h> #include <iostream.h> void main(void) { char string[10]; char str1[13] = "abcdefghi"; strcpy(string, str1); cout<<string; //print abcdefghi getch(); }
String Functions: Header file:#include <string.h> 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<iostream.h> #include<conio.h> #include<string.h> void main() { cout<<strcat("GOOD","MORNING"); getch(); } Program Output GOODMORNING
String Functions: Header file:#include <string.h> 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.
String Functions: Example : #include<iostream.h> #include<conio.h> #include<string.h> void main() { char msg1[ ]="Hello"; char msg2[ ]="HELLO"; cout<<strcmp(msg1,msg2); cout<<strcmp(msg2,msg1); getch(); } Program Output 32 -32
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
Solution: #include <iostream.h> #include <string.h> #include <stdio.h> #include <conio.h> 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(); }
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 3 0 4 3 5 1 6 0
#include <iostream.h> #include <string.h> #include <stdio.h> #include <conio.h> 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(); } Solution: