Home Interview Questions and AnswersTechnical Interview Questions and AnswersC Programming C Language String Interview Questions and Answers For Freshers Part-5

string9. How can you tell whether two strings are the same?

The standard C library provides several functions to compare two strings to see whether they are the same. One of these functions, strcmp(), is used here to show how this task is accomplished:
[C]
#include <stdio.h>

#include <string.h>

void main(void);

void main(void)

{

char* str_1 = “abc”;

char* str_2 = “abc”;

char* str_3 = “ABC”;

if (strcmp(str_1, str_2) == 0)

printf(“str_1 is equal to str_2.\n”);

else

printf(“str_1 is not equal to str_2.\n”);

if (strcmp(str_1, str_3) == 0)

printf(“str_1 is equal to str_3.\n”);

else

printf(“str_1 is not equal to str_3.\n”);

}
[/C]
This program produces the following output:

str_1 is equal to str_2.

str_1 is not equal to str_3.

Notice that the strcmp() function is passed two arguments that correspond to the two strings you want to compare. It performs a case-sensitive lexicographic comparison of the two strings and returns one of the following values:
Return Value           Meaning
<0     –     The first string is less than the second string.
0     –     The two strings are equal.
>0     –     The first string is greater than the second string.

In the preceding example code, strcmp() returns 0 when comparing str_1 (which is “abc”) and str_2 (which is “abc”). However, when comparing str_1 (which is “abc”) with str_3 (which is “ABC”), strcmp() returns a value greater than 0, because the string “ABC” is greater than (in ASCII order) the string “abc”.

Many variations of the strcmp() function perform the same basic function (comparing two strings), but with slight differences. The following table lists some of the functions available that are similar to strcmp():
Function Name           Description
strcmp()     –     Case-sensitive comparison of two strings
strcmpi()     –     Case-insensitive comparison of two strings
stricmp()     –     Same as strcmpi()
strncmp()     –     Case-sensitive comparison of a portion of two strings
strnicmp()     –     Case-insensitive comparison of a portion of two strings

10. How do you print only part of a string?

The following program shows how to print only part of a string using the printf() function:
[C]
#include <stdio.h>

#include <string.h>

void main(void);

void main(void)

{

char* source_str = “THIS IS THE SOURCE STRING”;

/* Use printf() to print the first 11 characters of source_str. */

printf(“First 11 characters: ‘%11.11s’\n”, source_str);

/* Use printf() to print only the

last 13 characters of source_str. */

printf(“Last 13 characters: ‘%13.13s’\n”,

source_str + (strlen(source_str) – 13));

}
[/C]
This example program produces the following output:

First 11 characters: ‘THIS IS THE’

Last 13 characters: ‘SOURCE STRING’

The first call to printf() uses the argument “%11.11s” to force the printf() function to make the output exactly 11 characters long. Because the source string is longer than 11 characters, it is truncated, and only the first 11 characters are printed. The second call to printf() is a bit more tricky. The total length of thesource_str string is calculated (using the strlen() function). Then, 13 (the number of characters you want to print) is subtracted from the total length of source_str.

You may also like

Leave a Comment