Working with Strings & Standard Functions 245
Table 8.2 String formats with different precision
Sr. No. Statement Output
1.
printf ("%sn", text);
PRABHAKAR
2.
printf ("%. 5s ", text);
PRABHA
3. printf ("%.8s ",te xt); PRABHAKA
4. printf ("%. 15sn", text); PRABHAKAR
5. Printf (u%-10.4sn",text);
PRAB
6.
Printf (m i s " , text) ;
PRABHAKAR
Where, the te x t is ' PRABHAKAR'
1) The 1st statement displays the output ' PRABHAKAR'. The entire string can be displayed with
the first statement.
2) We can also specify the precision with character string, which is to be displayed. The precision
is (number of characters to be displayed-) provided after decimal point. For instance in the 2nd
statement in the above table the first five characters are displayed. Here the integer value 5 on
the right side of the decimal point, which specifies the five characters to be displayed.
3) In 3rd statement first eight characters are displayed.
4) While statement number four -displays the entire strings.
5) The 5th statement with -sign (Ex. %-10.4s ), displays the string with left justified.
6) When the field length is less than the length of the string the entire string is printed. When it is
greater than the length of the string, blank spaces are initially printed followed by the string.
This effect can be seen in the 6th statement.
7) When number of characters to be displayed is specified as zero after decimal point nothing
will be displayed.
Few examples are illustrated below giving the effects of various formats of strings.
8.4 Write a program to display the string 'PRABHAKAR' using various format specifications.
mainO
{
char t e x t [1 5 ]-"PRABHAKAR';
c lr s c r O ;
printf ("% s n",tex t);
printf ("% .5sn",text);
printf ( % .8s ", text);
printf ("% .15sn ",text);
Printf C%-10.4s”,text);
Printf ("% lls",te x t);
)
OUTPUT:
PRABHAKAR
PRABH
246 Programming and Data Structures
PRBHAKA
PRABHAKAR
PRAB
PRABHAKAR
8.5 Use the while loop and print out the elements of the character array.
# include <stdio.h>
# include <conio.h>
main()
{
char t e x t [ ] ="HAVE A NICE DAY";
int i=0j
c lr s c r ()>
while (i<=15)
{
printf ("% c ",tex t[i]);
j++;
}
OUTPUT: HAVE A NICE DAY
Explanation In this program while loop is used for printing the characters up to length of 15.There
after while loop terminates when variable ' i reaches to 15.
8.6 Given below is an example in which the string elements are displayed. Use the while
loop and print out the elements of the character array. Take help of NULL ('0') character.
# include <std io.h>
# include <conio.h>
void m ain()
{
char t e x t [ ] ="HAVE A NICE DAY";
int issO;
c lrs c r () ;
while (tex t[i]!= ' 0 ')
I
printf ("% c ",tex t[i]);
i++;
}
I
OUTPUT: HAVE A NICE DAY
Explanation It is not needed to give the argument as given in the previous program. One can
display the character string without knowing the length of the string. As we know that every character
array always ends with NULL ( ' 0' ) character. By using NULL character in while loop we can
write a program to display the string.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.227.4