C Strings Questions and Answers
C Strings are one of the most essential topics in C programming, frequently appearing in placement papers and technical interviews. This section on C Strings questions with answers helps you master how strings are stored, manipulated, and used in C programs. Whether you’re preparing for TCS, Infosys, Wipro, or Cognizant, understanding string functions like strcpy(), strlen(), strcmp(), and strcat() is crucial. In competitive exams and programming interviews, questions often test your grasp of character arrays, pointers to strings, and memory allocation issues related to them. Practicing C programming MCQs and string manipulation examples enhances your coding accuracy and logic. Use these curated C programming questions and answers to strengthen your understanding and prepare for company-specific tests or online coding assessments effectively.
C Strings
1. Which of the following function sets first n characters of a string to a given character?
- strinit()
- strnset()
- strset()
- strcset()
3. How will you print \n on the screen?
- printf("\n");
- echo "\\n";
- printf('\n');
- printf("\\n");
4. The library function used to find the last occurrence of a character in a string is
- strnstr()
- laststr()
- strrchr()
- strstr()
5. Which of the following function is used to find the first occurrence of a given string in another string?
- strchr()
- strrchr()
- strstr()
- strnset()
6. Which of the following function is more appropriate for reading in a multi-word string?
- printf();
- scanf();
- gets();
- puts();
7. Which of the following function is correct that finds the length of a string?
- int xstrlen(char *s) { int length=0; while(*s!='\0') { length++; s++; } return (length); }
- int xstrlen(char s) { int length=0; while(*s!='\0') length++; s++; return (length); }
- int xstrlen(char *s) { int length=0; while(*s!='\0') length++; return (length); }
- int xstrlen(char *s) { int length=0; while(*s!='\0') s++; return (length); }
9. Given a declaration and initialization statement as shown below, char a [200] = {'0';} What is the output of the following printf statment ?
- 48 0 0
- 44 0 0
- 46 0
- 49 0 49 0
10. Given a declaration and initilization as shown below, char a [6] = {'1', '2', '3', }; What is the output of the following printf statement ? printf ("%c %c %c %c %c", a[0], a[3], a[4], a[2], a[4], a[1]);
- 1 2 3 4 5 6 7
- 1 2 3 4 5
- 1 3 2
- 1 2 3 0 0 0