1.

Explain, in brief the purpose of the following string handling functions: (i) strcat (ii) strcmp (iii) strcpyUse suitable examples

Answer»

(i) strcat() Function concatenates two strings together and has the following form: 

strcat(string1,string2);

When this function is executed, string2 is appended to string1 by removing the null character at the end of string1.

C permits nesting of strcat functions as strcat(strcat(string1,string2),string3);

(ii) strcmp( ) is the string comparison function defined in string.h header file. It has the following form:. 

int strcmp ( const char *s1, const char *s2 );

strcmp will accept two strings. It will return an integer. This integer will either be:

Negative if s1 is less than s2. 

Zero if s1 and s2 are equal. 

Positive if s1 is greater than s2. 

Strcmp performs a case sensitive comparison; if the strings are the same except for a difference in case, then they're countered as being different. Strcmp also passes the address of the character array to the function to allow it to be accessed.

(iii) strcpy ( ) function is just like a string-assignment operator which take the following form:

char *strcpy ( char *dest, const char *src );

strcpy is short for string copy, which means it copies the entire contents of src into dest. The contents of dest after strcpy will be exactly the same as src such that strcmp ( dest, src ) will return 0.src may be a character array variable or a string constant. 



Discussion

No Comment Found

Related InterviewSolutions