IndiaBIX
IndiaBIX
Start typing & press "Enter" or "ESC" to close
  • Home
  • Jobs
  • Results
  • Current Affairs
  • GK
  • Online Test
  • HR Interview
  • BLOG

Strings

  • Home
  • Computer Science & Engineering
  • C Programming Questions and Answers
  • Strings
1. 

Which of the following function is correct that finds the length of a string?

A. int xstrlen(char *s) { int length=0; while(*s!='\0') { length++; s++; } return (length); }
B. int xstrlen(char s) { int length=0; while(*s!='\0') length++; s++; return (length); }
C. int xstrlen(char *s) { int length=0; while(*s!='\0') length++; return (length); }
D. int xstrlen(char *s) { int length=0; while(*s!='\0') s++; return (length); }

Answer: Option A

Explanation:

Option A is the correct function to find the length of given string.

Example:

#include<stdio.h>

int xstrlen(char *s)
{
    int length=0;
    while(*s!='\0')
    { length++; s++; }
    return (length);
}

int main()
{
    char d[] = "IndiaBIX";
    printf("Length = %d\n", xstrlen(d));
    return 0;
}

Output: Length = 8

View Answer Discuss Workspace Report

2. 

Which of the following function is more appropriate for reading in a multi-word string?

A. printf();
B. scanf();
C. gets();
D. puts();

Answer: Option C

Explanation:

gets(); collects a string of characters terminated by a new line from the standard input stream stdin

#include <stdio.h>

int main(void)
{
   char string[80];

   printf("Enter a string:");
   gets(string);
   printf("The string input was: %s\n", string);
   return 0;
}

Output:

Enter a string: IndiaBIX

The string input was: IndiaBIX

View Answer Discuss Workspace Report

3. 

Which of the following function is used to find the first occurrence of a given string in another string?

A. strchr()
B. strrchr()
C. strstr()
D. strnset()

Answer: Option C

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

View Answer Discuss Workspace Report

4. 

The library function used to find the last occurrence of a character in a string is

A. strnstr()
B. laststr()
C. strrchr()
D. strstr()

Answer: Option C

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

View Answer Discuss Workspace Report

5. 

How will you print \n on the screen?

A. printf("\n");
B. echo "\\n";
C. printf('\n');
D. printf("\\n");

Answer: Option D

Explanation:

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

View Answer Discuss Workspace Report

6. 

If the two strings are identical, then strcmp() function returns

A. -1
B. 1
C. 0
D. Yes

Answer: Option C

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

View Answer Discuss Workspace Report

7. 

Which of the following function sets first n characters of a string to a given character?

A. strinit()
B. strnset()
C. strset()
D. strcset()

Answer: Option B

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

View Answer Discuss Workspace Report

Questions & Answers

Aptitude Chemical Engineering Civil Engineering Computer Science & Engineering Current Affairs Data Interpretation Electrical & Electronics Engineering Electronics & Communication Engineering General Knowledge Logical Reasoning Mechanical Engineering Non Verbal Reasoning Verbal Ability Verbal Reasoning

Interviews

HR Interview

Jobs

Sarkari Jobs

Results

Rojgar ResultSarkari Result

Admission

Admission 2023

Admit Card

Admit Card 2023

Answer Key

Answer Key 2023
copyright
Privacy Policy
© 2025 IndiaBIX. All Rights Reserved.

Report