• Courses
  • Tutorials
  • Jobs
  • Practice
  • Contests

C String

Question 11

Output of following program C
#include <stdio.h>
int fun(char *p)
{
    if (p == NULL || *p == \'\\0\') return 0;
    int current = 1, i = 1;
    while (*(p+current))
    {
        if (p[current] != p[current-1])
        {
            p[i] = p[current];
            i++;
        }
        current++;
    }
    *(p+i)=\'\\0\';
    return i;
}

int main()
{
    char str[] = \"geeksskeeg\";
    fun(str);
    puts(str);
    return 0;
}
  • gekskeg
  • geeksskeeg
  • geeks
  • Garbage Values

Question 12

C
int main() {
    char p[]="geeksquiz";
    char t;
    int i,j;
    for(i=0,j=strlen(p);i!=j;i++,j--)
    {
        t=p[i];
        p[i]=p[j-i];
        p[j-i]=t;
    }
    printf("%s",p);

    return 0;
}

Output?

  • ziuqskeeg

  • Nothing is printed on the screen

  • geeksquiz

  • gggggggg

Question 13

Output of following C program? Assume that all necessary header files are included 

C
int main()
{
    char *s1 = (char *)malloc(50);
    char *s2 = (char *)malloc(50);
    strcpy(s1, \"Geeks\");
    strcpy(s2, \"Quiz\");
    strcat(s1, s2);
    printf(\"%s\", s1);
    return 0;
}
  • GeeksQuiz

  • Geeks

  • Geeks Quiz

  • Quiz

Question 14

The output of following C program is C
#include <stdio.h>
char str1[100];

char *fun(char str[])
{
    static int i = 0;
    if (*str)
    {
        fun(str+1);
        str1[i] = *str;
        i++;
    }
    return str1;
}

int main()
{
    char str[] = \"GATE CS 2015 Mock Test\";
    printf(\"%s\", fun(str));
    return 0;
}
  • GATE CS 2015 Mock Test
  • tseT kcoM 5102 SC ETAG
  • Nothing is printed on screen
  • Segmentation Fault

Question 15

Consider the following function written in the C programming language. The output of the above function on input “ABCD EFGH” is C
void foo (char *a)
{
  if (*a && *a != ` `)
  {
     foo(a+1);
     putchar(*a);
  }
}
  • ABCD EFGH
  • ABCD
  • HGFE DCBA
  • DCBA

Question 16

Consider the following C program. C
#include <stdio.h>
struct Ournode {
  char x, y, z;
};

int main() {
  struct Ournode p = {\'1\', \'0\', \'a\' + 2};
  struct Ournode *q = &p;
  printf(\"%c, %c\", *((char *)q + 1), *((char *)q + 2));
  return 0;
}
The output of this program is:
  • 0, c
  • 0, a+2
  • \'0\', \'a+2\'
  • \'0\', \'c\'

Question 17

Assume that a character takes 1 byte. Output of following program? 
 

C
#include<stdio.h>
int main()
{
    char str[20] = \"GeeksQuiz\";
    printf (\"%d\", sizeof(str));
    return 0;
}
  • 9
     

  • 10
     

  • 20
     

  • Garbage Value
     

Question 18

Predict the output of following program, assume that a character takes 1 byte and pointer takes 4 bytes.
 

C
#include <stdio.h>
int main()
{
    char *str1 = \"GeeksQuiz\";
    char str2[] = \"GeeksQuiz\";

    printf(\"sizeof(str1) = %d, sizeof(str2) = %d\",
           sizeof(str1), sizeof(str2));

    return 0;
}
  • sizeof(str1) = 10, sizeof(str2) = 10 

     

  • sizeof(str1) = 4, sizeof(str2) = 10
     

  • sizeof(str1) = 4, sizeof(str2) = 4
     

  • sizeof(str1) = 10, sizeof(str2) = 4
     

Question 19

A language with string manipulation facilities uses the following operations. 

head(s)- returns the first character of the string s 
tail(s)- returns all but the first character of the string s 
concat(sl, s2)- concatenates string s1 with s2. 

The output of concat(head(s), head(tail(tail(s)))), where s is acbc is
 

  • ab
     

  • ba
     

  • ac
     

  • as
     

Question 20

In below program, what would you put in place of “?” to print “Quiz”?
 

C
#include <stdio.h>
int main() 
{ 
  char arr[] = \"GeeksQuiz\"; 
  printf(\"%s\", ?); 
  return 0; 
}
  • arr
     

  • (arr+5)
     

  • (arr+4)
     

  • Not possible
     

There are 21 questions to complete.

Last Updated :
Take a part in the ongoing discussion