Arrays and Strings
Arrays and Strings
Program#2: Write a program that inputs a string. Copies it to another string and then prints both
sting.
Program#3: Write program that inputs a string and displays number of words and number of
Program#4: Write a program that inputs a string from the user and count the number of vowels
in the string.
Strcpy() Function:
The strcpy() is used to copy contents of string variable or string constants to anther string variable.
Syntax:
strcpy(str1, str2);
Example:
char str1[10]=“Pakistan”, str2[10], str3[10];
strcpy(str2, str1);
strcpy(str3, “skardu”);
Strcat() function:
The strcat() function is used for concatenation or joining to two strings.
Syntax:
strcat(str1, str2);
Example:
char str1[10], str2[10];
strcpy(str1, “Degree”)
strcpy(str2, “College”);
strcat(str1,str2);
cout<<after cancatening of the two strings”<<str2;
rlen() function:
The strlen() is used to return the length of a string.
ntax:
strlen(string);
ample:
har str[10]= “physics”;
out<<the string length/size is”<<strlen(str);
cmp() function:
The strcmp() function compares two strings and returns an integer value based on the result of
omparison. This comparison is based on ASCII codes of characters. The returned value may the following:
It returns zero if both strings are equal
It returns 1 if first string is greater than second string
It return -1 if first string is less than second string.
ntax:
trcmp(str1, str2);
ample:
har str1[10], str[10];
n.get(str1);
n.get(str2);
trcmp(str1, str2);