getchar(): It reads a character from the keyboard. It is
executed when a key is pressed and the pressed key is echoed to screen.
getch(): getch()
function returns just after the keypress and the pressed key is not echoed to
screen. It can be used in an interactive environment.
getche(): getch() function returns just after the keypress and the
pressed key is echoed to the screen. It can be used in an interactive
environment.
putchar(): At
current cursor position, putchar() prints a character to screen
Formatted I/O in C :
The formatted
Input / Output functions can read and write values of all data types, provided
the appropriate conversion symbol is used to identify the datatype.scanf is used for receiving formatted Input andprintf is used for displaying formatted output.
Syntax for scanf :
scanf(“control_string 1, - - , control_string n”,&variable1, - - -
- ,variable n);
|
Syntax for printf :
printf(“control_string 1, - - , control_string n”,variable1, - - - - ,
variable n);
|
Sample Program :
printf_scanf.c
#include<stdio.h>
#include<conio.h> void main() { int no; char ch; char s[20]; printf("Enter a character : "); scanf("%c",&ch); printf("Enter a No : "); scanf("%d",&no); printf("Enter a Word or String : "); scanf("%s",&s); printf("\n Character : %c",ch); printf("\n No : %d",no); printf("\n String : %s",s); getch(); } |
Unformatted I/O in C
Unformatted I/O functions works only with character datatype (char).The unformatted Input functions used in C are getch(), getche(), getchar(), gets().
Syntax for getch () in C :
variable_name = getch();
|
getch() accepts only single character from keyboard. The character
entered through getch() is not displayed in the screen (monitor).
Syntax for getche() in C :
variable_name = getche();
|
Like getch(), getche() also accepts only single character, but unlike
getch(), getche() displays the entered character in the screen.
Syntax for getchar() in C :
variable_name = getchar();
|
getchar() accepts one character type data from the keyboard.
Syntax for gets() in C :
gets(variable_name);
|
gets() accepts any line of string including spaces from the
standard Input device (keyboard). gets() stops reading character from keyboard
only when the enter key is pressed.
The unformatted output statements in C are putch, putchar and puts.
Syntax for putch in C :
putch(variable_name);
|
putch displays any alphanumeric characters to the standard output
device. It displays only one character at a time.
Syntax for putchar in C :
putchar(variable_name);
|
putchar displays one character at a time to the Monitor.
Syntax for puts in C :
puts(variable_name);
|
puts displays a single / paragraph of text to the standard output
device.
Sample program :
gets_puts.c
#include<stdio.h>
#include<conio.h> void main() { char a[20]; gets(a); puts(a); getch(); } |
String functions / Operations in C
1) strlen() in C :
This function is
used to determine the length of a given string.
Syntax :
variable=strlen(“string”);
|
The string length will be returned to the variable.
Sample Program
str_length.c
#include<stdio.h>
#include<conio.h> #include<string.h> void main() { char a[30]; int l; printf("Enter a string : "); scanf("%s",&a); l=strlen(a); printf("\n Length of the Given String is : %d",l); getch(); } |
2) strcpy() in C :
This function copies the string from one variable to another variable.
Syntax :
strcpy(source_variable, destination_variable);
|
Sample Program
str_cpy.c
#include<stdio.h>
#include<conio.h> #include<string.h> void main() { char a[30],b[30]; printf("Enter a string : "); scanf("%s",&a); strcpy(b,a); printf("Value of a : %s",a); printf("\nValue of b : %s",b); getch(); } |
3) strcmp() in C :
This function is used to compare two strings. They are case sensitive.
Syntax :
strcmp(string1,string2);
|
If both the strings are equal return value will be 0, else non_zero
value will be returned.
Sample program :
str_cmp.c
#include<stdio.h>
#include<conio.h> #include<string.h> void main() { char a[30],b[30]; int n; printf("Enter string one : "); scanf("%s",&a); printf("Enter string two : "); scanf("%s",&b); n=strcmp(a,b); if(n==0) { printf("Both the Strings are Equal"); } else { printf("Strings are not Equal"); } getch(); } |
4) strlwr() in C :
This function converts uppercase characters to lowercase.
Syntax :
strlwr(“string”);
|
Sample program :
str_lwr.c
#include<stdio.h>
#include<conio.h> #include<string.h> void main() { char a[30]; printf("Enter a string in uppercase : "); scanf("%s",&a); printf("RESULT : %s",strlwr(a)); getch(); } |
5) strupr() in C :
This function converts lowercase characters to uppercase.
Syntax :
strupr(“string”);
|
Sample program :
str_upr.c
#include<stdio.h>
#include<conio.h> #include<string.h> void main() { char a[30]; printf("Enter a string in lowercase : "); scanf("%s",&a); printf("RESULT : %s",strupr(a)); getch(); } |
6) strrev() in C :
This function is sued to reverse the characters in a given string.
Syntax :
strrev(string);
|
Sample program :
str_rev.c
#include<stdio.h>
#include<conio.h> #include<string.h> void main() { char a[30]; printf("Enter string : "); scanf("%s",&a); strrev(a); printf("Reversed String : %s",a); getch(); } |
No comments:
Post a Comment