STRING INPUT/OUTPUT FUNCTIONS: gets, puts

The strings can be read from the keyboard and can be displayed onto the monitor using following unformatted functions:
1. Unformatted input function: gets()
2. Unformatted output function: puts()

 Program to illustrate the use of gets() and puts().

#include<stdio.h>
void main()
{
char name[10];
printf(“enter your name: \n”);
gets(name); //same as scanf(“%s”, name);
printf(“welcome: ”);
puts(name); //same as printf(“%s”, name);
}


 Program to count the number of characters in a string. – String Lenth

#include <stdio.h>
int main()
{
char str[100]; int i, length;
printf(“\n\tEnter a String to Compute its Length: ");
scanf(“%s", str);
for(i = 0; str[ i ] != ‘\0’; i++)
; /* NULL Statement * /
length = i;
printf(“\n\n\t The Length of String: %s is %d”, str, length);
return 0;
}
Example: Program to Print String in Reverse
#include <stdio.h>
int main()
{
char str[100]; int i, length;
printf(“\n\tEnter a String a Reverse: ");
scanf(“%s", str);
for(i = 0; str[ i ] != ‘\0’; i++) ; /* NULL Statement * /
length = i;
printf(“\n\n\t The Reverse of a String - %s - is : ” , str);
for( i = length – 1; i >= 0; i--)
printf(“%c”, str[ i ]);
return 0;
}
STRING INPUT/OUTPUT FUNCTIONS: gets, puts STRING INPUT/OUTPUT FUNCTIONS: gets, puts Reviewed by Unknown on 04:51 Rating: 5

No comments:

Powered by Blogger.