Sunday, November 23, 2008

String Questions in C Programming with Solution

Q.1
What will be output 
 void main()
{
char a[5];
a[0]='q';
a[1]='u';
a[2]='e';
clrscr();
printf("%s",a);
getch();
}
Output: garbage
Explanation: %s is used for string but a is not a string it is only array of character since its last character is not null character.
If you will write a[3]=’\0’; then output will be que.
Q.2 Write a scanf statement which can store one line of string which includes white space.
Ans: 
void main()
{
char a[30];
clrscr();
scanf("%[^\n]",a);
printf("%s",a);
getch();
}
Q.3 
Write a c program in which a scanf function can store a paragraph at time ?
Ans:
void main()
{
char a[30];
clrscr();
scanf("%[^\t]",a);
printf("%s",a);
getch();
}

Note: Paragraph will end when you will press tab key.
Q.4
In the following program
void main()
{
char a[30];

}

How much array a is reserving memory space ?
Ans:
It is not reserving any memory space because array a has only declared it has not initialized .Any not initialized variable doesn’t reserve any memory space.

No comments:

Blog List