logo

CuriousTab

CuriousTab

Discussion


Home C Programming Strings Comments

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


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

  • Correct Answer
  • strstr() 

    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 = "CuriousTab", *str2 = "ia", *ptr;
    
       ptr = strstr(str1, str2);
       printf("The substring is: %s\n", ptr);
       return 0;
    }
    

    Output: The substring is: iaCURIOUSTAB


    Strings problems


    Search Results


    • 1. What would be the equivalent pointer expression for referring the array element a[i][j][k][l]

    • Options
    • A. ((((a+i)+j)+k)+l)
    • B. *(*(*(*(a+i)+j)+k)+l)
    • C. (((a+i)+j)+k+l)
    • D. ((a+i)+j+k+l)
    • Discuss
    • 2. How many bytes are occupied by near, far and huge pointers (DOS)?

    • Options
    • A. near=2 far=4 huge=4
    • B. near=4 far=8 huge=8
    • C. near=2 far=4 huge=8
    • D. near=4 far=4 huge=8
    • Discuss
    • 3. A pointer is

    • Options
    • A. A keyword used to create variables
    • B. A variable that stores address of an instruction
    • C. A variable that stores address of other variable
    • D. All of the above
    • Discuss
    • 4. Can you combine the following two statements into one?
      char *p;
      p = (char*) malloc(100);
      

    • Options
    • A. char p = *malloc(100);
    • B. char *p = (char) malloc(100);
    • C. char *p = (char*)malloc(100);
    • D. char *p = (char *)(malloc*)(100);
    • Discuss
    • 5. If a variable is a pointer to a structure, then which of the following operator is used to access data members of the structure through the pointer variable?

    • Options
    • A. .
    • B. &
    • C. *
    • D. ->
    • Discuss
    • 6. How will you print \n on the screen?

    • Options
    • A. printf("\n");
    • B. echo "\\n";
    • C. printf('\n');
    • D. printf("\\n");
    • Discuss
    • 7. The library function used to find the last occurrence of a character in a string is

    • Options
    • A. strnstr()
    • B. laststr()
    • C. strrchr()
    • D. strstr()
    • Discuss
    • 8. Which of the following function sets first n characters of a string to a given character?

    • Options
    • A. strinit()
    • B. strnset()
    • C. strset()
    • D. strcset()
    • Discuss
    • 9. If the two strings are identical, then strcmp() function returns

    • Options
    • A. -1
    • B. 1
    • C. 0
    • D. Yes
    • Discuss
    • 10. Which of the following function is correct that finds the length of a string?

    • Options
    • 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);
      }
      
    • Discuss


    Comments

    There are no comments.

Enter a new Comment