Mention various operations that can be performed on strings using built-in functions. Explain any two functions.

6. B) Mention various operations that can be performed on strings using built-in functions.
Explain any two functions.

Answer:-

FunctionWork of Function
strlen()computes string’s length
strcpy()copies a string to another
strcat()concatenates(joins) two strings
strcmp()compares two strings
strlwr()converts string to lowercase
strupr()converts string to uppercase

1.strcpy( ): It is possible to assign a value to a string variable using strcpy( ). It allows us to copy a string from one location to another. The string to be copied can be literal or string variable.

The general form of call to strcpy is strcpy(dest,source);
Strcpy() function has two parameters.

The first is the dest, a string variable whose value is going to be changed.

The second is the source of the string literal or variable which is going to be copied to the destination.


Case 1:

Ex: char str1[ ] = “america” ;
char str2[]= “india” ;
strcpy ( str1, str2 ) ;
strcpy(america,india);
america = 7character
india = 5 character
so first 5 character of america is replaced .
Output is inidaca


Case 2: Ex: char str1[10]=”india”
char str2[]= “america” ;
strcpy ( str1, str2 ) ;
strcpy(india,america);
india = 5 character
america = 7character
since 7 is greater than 5, all characters of india are replaced by america.
Output is America

2. strcmp(): It is used to compare the contents of the two strings.
Syntax:
int strcmp(string 1, string 2);
Example: char mystr_a[10] = “Hello”;
char mystr_b[10] = “Goodbye”;
– mystr_a == mystr_b; /* NOT allowed! The correct way is if (strcmp(mystr_a, mystr_b )) */
printf (“Strings are NOT the same.”);
else
printf( “Strings are the same.”); Here it will check the ASCII value of H and G i.e, 72 and 71
and return the diference 1.

Leave a Reply

Your email address will not be published. Required fields are marked *