O'Reilly logo

Express Learning - Computer Fundamentals and Programming by ITL ESL, Ashok Kamthane

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Strings and Standard
Functions
1. What is a string? How it is declared?
Ans: A string is an array of characters. String can be declared with character array. It can also be
declared with string pointer. To manipulate text such as words and sentences, normally strings are used.
Every string is terminated with ‘\0’ character or NULL. The NULL is a byte with all bits at logic zero.
The value of the string is its base address, i.e., address of the first character. When a string is created, a
few compilers place the string in the memory where it cannot be modified.
A string can be defined as follows:
char name[]={'I','N','D','I','A','\0'};
2. Which format specifier or control string is used in printf() to print a string?
Ans: The %s format specifier or control stringis used in printf()to print the string.
3. Write a short program to read the string and display it using format specifier %s.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
char a[20];
clrscr();
printf("\nEnter the String:");
scanf(" %s",a);
printf("Entered string is: %s",a);
getch();
}
8
M08_ITL-ESL4791_02_SE_C08.indd 157 12/22/2012 5:03:30 PM
II-158 Programming Concepts
OUTPUT:
Enter the string: Amit
Entered string is: Amit
4. What precautions should be taken while entering the string using scanf()?
Ans: Following are the precautions to be taken while entering the string using scanf().
1. The length of the string should not exceed the length of the array.
Example: If the length of the array is 4, then the string length more than 4 is not acceptable.
2. The multi-word string is not acceptable by scanf().
Example: The multi-word string such as Amit Kamthane is not acceptable by scanf(). The
string after spacewill not be read with scanf(). Refer to the programming example Q 6.
5. Is it possible to accept a multi-word string by scanf()? What is the solution to display 
a multi-word string?
Ans: The multi-word string is not acceptable by scanf(). This is one of the big disadvantages of
scanf() function. The solution to accept the multi-word string is by using function gets().
6. Prove using short program:How multi-word string is not accepted by function scanf()?
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
char a[20];
clrscr();
printf("\nEnter the Multi-word String:");
scanf(" %s",a);
printf("Entered string is: %s",a);
getch();
}
OUTPUT:
Enter the String: Amit Kamthane
Entered string is: Amit
Explanation: In the above program, user enters the multi-word string Amit  Kamthane. However,
when we try to print the string using printf(), it displays onlyAmit and the rest of the part is ignored.
Therefore, by the above example, we can say that the function scanf() is incapable of accepting
the multi-word string.
M08_ITL-ESL4791_02_SE_C08.indd 158 12/22/2012 5:03:30 PM
Strings and Standard Functions II-159
7. Write a short program to read the multi-word string using the function gets()?
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
char a[20];
clrscr();
printf("\nEnter the Multi-word String:");
gets(a);
printf("Entered string is: %s",a);
getch();
}
OUTPUT:
Enter the Multi-word String:Amit Kamthane
Entered string is:Amit Kamthane
Note: The multi-word string is acceptable by the function gets().
8. Which  format specifier is used in scanf()function other than %s to read the multi-
word string?
Ans: The format specifier “ %[^\n]s” is used to read the multi-word string.
9. Write a program to read the multi-word string using format specifier %[^\n]s.
Ans:
#include <stdio.h>
#include <conio.h>
void main()
{
char a[20];
clrscr();
printf("\nEnter the Multi-word String:");
scanf(" %[^\n]s",a);
printf("Entered string is: %s",a);
getch();
}
OUTPUT:
Enter the multi-word String: Amit Kamthane
Entered string is: Amit Kamthane
M08_ITL-ESL4791_02_SE_C08.indd 159 12/22/2012 5:03:30 PM

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required