String


    Find the output of following c program
   
    1.      #include<stdio.h>
int main()
{
int i=0;
char name[]={'s','s',' ','c','o','d','e','\0'};
while(name[i])
{
printf("%c",name[i]);
i++;
}
return 0;
}

    2.   #include<stdio.h>
int main()
{
char *str;
printf("Enter a string\n");
gets(str); 
printf("String is %s",str);
return 0;
}

    3.   What will be the output if you entered more than ten character.
     #include<stdio.h>
int main()
{
char str[10];
printf("Enter a string\n");
gets(str); 
printf("String is %s",str);
return 0;
}

    4.   #include<stdio.h>
#include<string.h>
int main()
{
char *str1="Good", *str2="Morning";
strcat(str1,str2) ;
printf("%s\n",str1);
return 0;
}

    5.   #include<stdio.h>
#include<string.h>
int main()
{
char str[]="Good";
strcat(str,"morning") ;
printf("%s\n",str);
return 0;
}

    6.   #include<stdio.h>
#include<string.h>
int main()
{
char str[]="sscoading";
str=str+5;
printf("%s\n",str);
return 0;
}

    7.   #include<stdio.h>
#include<string.h>
int main()
{
char str1[5]="Good ",str2[5]="day";
char *ptr1=str1,*ptr2=str2;
strcat(ptr1,ptr2) ;
printf("%s\n",ptr1);
return 0;
}

    8.   #include<stdio.h>
int main()
{
printf("sscoading"+2);
return 0;
}

    9.   #include<stdio.h>
void func(char *str)
{
 printf("%s\n",str);
}
int main()
{
char str[]="dopractice";
func(str+2) ;
return 0;
}

   10.     #include<stdio.h>
int main()
{
char str[] ={98,101,97,117,116,121}; 
printf("%s\n",str);
return 0;
}

   11.      #include<stdio.h>
int main()
{
char str[] ="beautyful";    
char *p=str+3;
printf("%c\t",*p);
printf("%s\n",p);
return 0;
}

   12.      #include<stdio.h>
int main()
{
printf("%c\t", "Destination"[2]); 
printf("%c",*("Destination"+2));
return 0;
}

   13.      #include<stdio.h>
int main()
{
 printf("%s",*("coading"+2));
 return 0;
}

   14.      #include<stdio.h>
#include<string.h>
int main()
{
 printf("%s\t","Destination"+2);  
 printf("Destination"+(strlen("arati")));
 return 0;
}

   15.      #include<stdio.h>
int main()
{
char str[]="Knowledge";
char *p=str;
p++; p=p+2; p[3]='t';
printf("%s\t%s",str,p);
return 0;
}

   16.      #include<stdio.h>
#include<string.h>
int main()
{
char *p[]={"Orange","Yellow","Sky" "Blue","Black"};   
printf("%s %s\n",p[1],p[2]);       
return 0;
}

   17.      #include<stdio.h>
#include<string.h>
int main()
{
char arr[10];
strcpy(arr, "Give""up"); 
printf("%s",arr);
return 0;
}

   18.      #include<stdio.h>
int main()
{
char *p[]={'G','o','o','d'};
char arr[10];
p=arr;
printf("%c\n",*p);
return 0;
}

   19.      #include<stdio.h>
int main()
{
char *p[3];
*p[1]='a';                 
printf("%c\n",*p[1]);
return 0;
}

   20.      #include<stdio.h>
#include<string.h>
int main()
{
char str1[15]="Good ";  
char str2[]="Evening";
strcpy(str1+strlen(str1),str2);
printf("%s\n",str1);
return 0;
}

   21.      #include<stdio.h>
int main()
{
char name[15]="bestplaceforc";
int i=0;
while(name[i])
{
 printf("%c",name[i]);
 i=i+3;
}
return 0;
}

   22.      #include<stdio.h>
int main()
{
char str[5][10]; 
int i;
for(i=0;i<5;i++)
scanf("%s",str[i]);
for(i=0;i<5;i++)
printf("%s",str[i]);
return 0;
}

   23.      #include<stdio.h>
int main()
{
char *str[10];
int i;
for(i=0;i<3;i++)
scanf("%s",str[i]);
for(i=0;i<3;i++) 
printf("%s",str[i]);
return 0;
}

   24.      #include<stdio.h>
int main()
{
char *str="sscoading";        
int i=0;
while(str[++i]);
printf("%d\n",i);
return 0;
}

   25.      #include<stdio.h>
void func(char *p)
{
if(*p !='6') 
{
printf("%c",*p);
func(++p);
}
}
int main()
{
char *str="12345678";
func(str);
return 0;
}

   26.      #include<stdio.h>
int main()
{
char *ptr;
ptr="practice makes perfect";  
printf(ptr);
return 0;
}

ANSWER

          1.       ss code
2.       error (segmentation fault)
3.       It will produce as usual output if you entered little bit more character, if the entered character is more, then it leads to run time error.
Hint- you may get a warning “the `gets' function is dangerous and should not be used.”
4.       runtime error
Hint- str1 points is not writable, since string constant "Good" is stored in read-only memory by some compilers.
5.       Goodmorning
6.       compilation error
7.       Good day
8.       coading
9.       practice
10.   beauty
11.   u utiful
12.   s        s
13.   run time error
14.   stination        nation
15.   Knowletge        wletge
16.   Yellow SkyBlue
17.   Giveup
18.   compilation error
19.   Run time error
20.   good Eveving
21.   btafc
22.   it will take five string and produce the same output.
Hint-if string length is larger it leads to error
23.   Runtime error
24.   9
25.   12345
26.   practice makes perfect


No comments:

Post a Comment

For any doubt or suggestion you can write here