C Programming - Strings

Why should I learn to solve C Programming questions and answers section on "Strings"?

Learn and practise solving C Programming questions and answers section on "Strings" to enhance your skills so that you can clear interviews, competitive examinations, and various entrance tests (CAT, GATE, GRE, MAT, bank exams, railway exams, etc.) with full confidence.

Where can I get the C Programming questions and answers section on "Strings"?

IndiaBIX provides you with numerous C Programming questions and answers based on "Strings" along with fully solved examples and detailed explanations that will be easy to understand.

Where can I get the C Programming section on "Strings" MCQ-type interview questions and answers (objective type, multiple choice)?

Here you can find multiple-choice C Programming questions and answers based on "Strings" for your placement interviews and competitive exams. Objective-type and true-or-false-type questions are given too.

How do I download the C Programming questions and answers section on "Strings" in PDF format?

You can download the C Programming quiz questions and answers section on "Strings" as PDF files or eBooks.

How do I solve C Programming quiz problems based on "Strings"?

You can easily solve C Programming quiz problems based on "Strings" by practising the given exercises, including shortcuts and tricks.

Exercise : Strings - General Questions
1.
Which of the following function sets first n characters of a string to a given character?
strinit()
strnset()
strset()
strcset()
Answer: Option
Explanation:

Declaration:

char *strnset(char *s, int ch, size_t n); Sets the first n characters of s to ch

#include <stdio.h>
#include <string.h>

int main(void)
{
   char *string = "abcdefghijklmnopqrstuvwxyz";
   char letter = 'x';

   printf("string before strnset: %s\n", string);
   strnset(string, letter, 13);
   printf("string after  strnset: %s\n", string);

   return 0;
}

Output:

string before strnset: abcdefghijklmnopqrstuvwxyz

string after strnset: xxxxxxxxxxxxxnopqrstuvwxyz


2.
If the two strings are identical, then strcmp() function returns
-1
1
0
Yes
Answer: Option
Explanation:

Declaration: strcmp(const char *s1, const char*s2);

The strcmp return an int value that is

if s1 < s2 returns a value < 0

if s1 == s2 returns 0

if s1 > s2 returns a value > 0


3.
How will you print \n on the screen?
printf("\n");
echo "\\n";
printf('\n');
printf("\\n");
Answer: Option
Explanation:

The statement printf("\\n"); prints '\n' on the screen.


4.
The library function used to find the last occurrence of a character in a string is
strnstr()
laststr()
strrchr()
strstr()
Answer: Option
Explanation:

Declaration: char *strrchr(const char *s, int c);

It scans a string s in the reverse direction, looking for a specific character c.

Example:

#include <string.h>
#include <stdio.h>

int main(void)
{
   char text[] = "I learn through IndiaBIX.com";
   char *ptr, c = 'i';

   ptr = strrchr(text, c);
   if (ptr)
      printf("The position of '%c' is: %d\n", c, ptr-text);
   else
      printf("The character was not found\n");
   return 0;
}

Output:

The position of 'i' is: 19


5.
Which of the following function is used to find the first occurrence of a given string in another string?
strchr()
strrchr()
strstr()
strnset()
Answer: Option
Explanation:

The function strstr() Finds the first occurrence of a substring in another string

Declaration: char *strstr(const char *s1, const char *s2);

Return Value:
On success, strstr returns a pointer to the element in s1 where s2 begins (points to s2 in s1).
On error (if s2 does not occur in s1), strstr returns null.

Example:

#include <stdio.h>
#include <string.h>

int main(void)
{
   char *str1 = "IndiaBIX", *str2 = "ia", *ptr;

   ptr = strstr(str1, str2);
   printf("The substring is: %s\n", ptr);
   return 0;
}

Output: The substring is: iaBIX