Read and write operations on strings


Reading strings from terminal
The input function scanf() can be used with %s format specification to read in a string of characters.
            char   name[10];
            scanf(“%s”,name);

The problem with scanf() function is that it terminates its input on the first white space  bit finds. ( A white space includes blanks, tabs, carriage return, 
form feeds, new line).

Sample program that shows problem of scanf() in strings

#include<stdio.h>
#include<conio.h>
void main()
{
 char a[10],b[20];
 clrscr();
 printf(" Enter First strings: ");
 scanf("%s",a);
 printf(" Enter Second string: ");
 scanf("%s",b);
 printf(" The two strings are: ");
 printf("\n %s \n %s",a,b);
 getch();
}
 Output
  Enter First strings: computer
 Enter Second string: science and engineering
 The two strings are:
 computer
science

In the above example, the first does not have any white space so the total string will be  printed, But the second string has the white space between the string, 
so it prints only array  elements up to first space. So we can conclude that scanf() can be used to read only single  word.
Note that unlike previous scanf calls, in the case of character arrays, the ampersand (&) is not required before the variable name. The scanf() function
automatically terminates the string that is read with a null character and therefore the character array should be large enough to hold the input string plus the null character.

Reading a line of text using getchar() and gets()

getchar()
           
In many texts processing applications, we need to read in an entire line of text from the terminal. It is not possible to use scanf() function to read a 
line containing more than one word. This is because the scanf terminates reading as soon as a space is encountered in input.
            We can use getchar()  function repeatedly to read successive single characters from the input and place them  into a character array. Thus 
an entire line of text can be read and stored in an array. The reading is terminated when the new line character ‘\n’ entered and the null character is then 
inserted at the end of the string.

Sample Program to read a line of text from terminal using getchar()
#include<stdio.h>
#include<conio.h>
void main()
{
 char line[80],ch;
 int i=0;
 clrscr();
 printf(" \n Enter string: ");
 do
 {
  ch=getchar();
  line[i]=ch;
  i++;
 }
 while(ch!='\n');
 line[i]='\0';
 printf(" Entered string: %s",line);
 getch();
}         
           
Output

 Enter string: computer science and engineering
 Entered string: computer science and engineering

The above program reads string with white spaces and keeps all elements including white spaces into array called line.

gets()
            This is more convenient method for reading strings containing white spaces. This gets()function is available in stdio.h header file.
                        gets(str);
Sample Program to read a line of text from terminal using gets()
#include<stdio.h>
#include<conio.h>
void main()
{
 int line[80];
 clrscr();
 printf(" Enter string: ");
 gets(line);
 printf(" Entered string: %s",line);
 getch();
}
Output
Enter string: computer science and engineering
Entered string: computer science and engineering

Writing strings to screen

printf()                    
                        printf(“%s”, name);
can be used to display the entire contents of the array name.
                        printf(“%*.*s \n”, w, d, string);
            prints the first d characters of the string in the field width of w.
We can also specify the precision with which the array is displayed for instance the specification.
                                                %10.4s
This indicates that the first four characters are to be printed in the field width of 10 columns. However, if we include the minus sign in the specification
 (Eg: %-10.4s), the string will be printed left justified.
Rules on Strings

1.      When the field width is less than the length of the string , the entire string is printed.
2.      The integer value on the right side of the decimal point specifies the number of characters to be printed.
3.      When the number of characters to be printed is specified as zero, nothing is printed.
4.      The minus sign in the specification causes the string to be printed left justified.
printf(“% .*s\n”, w, d, str);
       prints the first ‘d’ characters of string in the field width of w.
Sample program to show the precision in strings left justified

#include<stdio.h>
#include<conio.h>
void main()
{
 char str[]="cprogram";
 int i,d;
 clrscr();
 for(i=0;i<8;i++)
 {
  d=i+1;
  printf("%-12.*s \n",d,str);
 }
 getch();
}
Output
c
cp
cpr
cpro
cprog
cprogr
cprogra
cprogram

putchar()
            Like getchar, C supports another character handling function putchar to output the values of character variable.            
         char ch=’A’;
            putchar(ch);   
This function requires one parameter. This statement is equal to
            printf(“%c”,ch);
Sample program which shows the use of putchar

#include<stdio.h>
#include<conio.h>
void main()
{
 char a[10]="computer";
 int i;
 clrscr();
 for(i=0;i<10;i++)
  putchar(a[i]);
 getch();
}
Output
computer

puts()
This is more convenient method for printing strings on screen. This puts() function is available in stdio.h header file.

                        puts(str);
Sample program which shows the use of puts
void main()
{
 char a[10];
 gets(a);
 printf(“\n”);
 puts(a);
}

Output
computer

computer



No comments:

Post a Comment