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
Arrays and Strings - C Programming MCQ Questions and Answers
Learn competitive and Technical Aptitude C programming mcq questions and answers on Arrays and Strings with easy and logical explanations. Page-6 section-1
Declaration: char *strrchr(const char *s, int c);
It scans a string s in the reverse direction, looking for a specific character c.
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.
gets(); collects a string of characters terminated by a new line from the standard input stream stdin
#include<stdio.h>
#include<string.h>
void main()
{
char str1[20] = "Hello", str2[20] = " World";
printf("%s", strcpy(str2, strcat(str1, str2)));
} >> printf("%sn", strcpy(str2, strcat(str1, str2)));
=> strcat(str1, str2) it append the string str2 to str1. The result will be stored in str1. Therefore str1 contains "Hello World".
=> strcpy(str2, "Hello World") it copies the "Hello World" to the variable str2.
Hence it prints "Hello World".
#include<stdio.h>
void main()
{
printf(5+"Good Morningn");
} printf(5+"Good Morning\n"); It skips the 5 characters and prints the given string.
Hence the output is "Morning".
#include<stdio.h>
#include<string.h>
void main()
{
char str[] = "Exam\0Veda";
printf("%s", str);
} A string is a collection of characters terminated by '\0'.
>> char str[] = "Exam\0\Veda\0"; The variable str is declared as an array of characters and initialized with value "Exam".
>> printf("%s", str); It prints the value of the str.
The output of the program is "Exam".
Array index always starts with 0.
strcmp(s1, s2);
returns int as follows:
== 0, when s1 == s2
< 0, when s1 < s2
> 0, when s1 > s2
What is the value of a[4]?
In the fourth location of an array it contains 0 since default initialization will take place.