Lets play with Characters and Strings!

Let us have a look upon some functions for:

Single character-

input- getchar(variable name) or variable name= getchar()
output- putchar(variable name)

 Multiple characters (Strings)-
Note: A String is nothing but an array of characters.

input- gets(variable name)
output- puts("text here...") or puts(variable name)

Formatted I/O functions:

1. scanf(): This function is used to input formatted data from the  user or in other words we can say that this function can be used to input any type of data.

Syntax- scanf("Format specifier", &Variable name);

DATA TYPE                      FORMAT SPECIFIER
char                                    %c
string                                  %s
int                                      %d
float                                   %f
double                               %lf

2. printf(): This function is used to display formatted output or in other words we can say that this function can be used to display any type of data in a formatted manner.

Syntax- printf(".........Format specifier......control string.....", variable name);

Escape Sequences:- \n, \t, \b, \a can also be used.

More About Data Types:

DATA TYPE                          USED TO STORE                  SIZE                       RANGE

Char (%c)                               Single character                     1 Byte                 -128 to 127
Int (%d)                                  Integers                                  2 Bytes                -32768 to 32767
Float (%f)                               Single Precision                     4 Bytes                3.4E-38 to 3.4E38
                                               Decimal numbers   
Double (%lf)                          Double Precision                    8 Bytes                1.7E-308 to 1.7E+308
                                               Decimal numbers




Examples:

WAP to get the name of user entered and display the same.

#include<stdio.h>
main()
{
char nm[10];
gets("Enter your name:\n");
gets(nm);
puts("Name: ");
puts(nm);
getch();
}

 WAP to get the name, enrollment number, address, gender, course name, marital status and fees from the user.

#include<stdio.h>
main()
{
char name[10], add[20], gn[5], course[10], ms;
int en;
float fee;
printf("Enter your Enrollment number\n");
scanf("%d", &en);
fflush(stdin);                                  \\To clear the input stream
printf("Enter your name:\n");
gets(name);
printf("Enter your address:\n");
fflush(stdin);
scanf("%s", &add);                         \\Another way to enter any string
printf("Enter your gender:\n");
fflush(stdin);
gets(gn);
printf("Enter your course name:\n");
fflush(stdin);
gets(course);
printf("Enter your marital status:\n");
fflush(stdin);
ms=getchar();
printf("Enter your fees:\n");
scanf("%f", &fee);
printf("\n\n Enrollment number: %d", en);
printf("\n\n Name: %s", name);
printf("\n\n Address: %s", add);
printf("\n\n Gender: %s", gn);
printf("\n\n Course Name: %s", course);
printf("\n\n Marital Status: %c", ms);
printf("\n\n Fee: %f", fee);
getch();
}

Something more to Practice-----

1. WAP to input length and breadth of a rectangle and display the area of the rectangle.
2. WAP to input principal, amount, rate and time. Calculate the Simple Interest and the Compound Interest.
3. WAP to input the radius of the circle. Display the circumference and the Area of the circle.
4. WAP to input two numbers and swap them using the third variable.
5. WAP to input three numbers and swap them with fourth variable.
6. WAP to input two numbers and swap them without using third variable.
7. WAP to input three numbers nad swap them without using fourth variable.

0 comments: