Program to illustrate the initialization of string.

#include<stdio.h>
void main()
{
char s[4]={'V','T','U'}; //or char s[4]="VTU";
printf("Value in array s[0] : %c \n", s[0]);
printf("Value in array s[1] : %c \n", s[1]);
printf("Value in array s[2] : %c \n", s[2]);
}
Output:
Value in array s[0] : V
Value in array s[1] : T
Value in array s[2] : U

Reading & Printing Strings

The strings can be read from the keyboard and can be displayed onto the monitor using following formatted functions:
1. Formatted input function: scanf()
2. Formatted output function: printf()

 Program to illustrate the use of scanf() and printf().

#include <stdio.h>
int main()
{
char str[50];
int i, n; clrscr();
printf(“\n\tEnter a string(gets):“);
gets(str);
printf(“\n\t String Entered is(gets) : %s“, str);
printf(“\n\tEnter number of character in the string:“);
scanf(“%d”, &n);
printf(“\n\tEnter %d characters of string one by one :”);
for( i = 0; i < n ; i++ )
scanf(“%c”, &str[ i ] );
str[ i ] = ‘\0’ ;
printf(“\n\t String Entered is: %s“, str);
printf(“\n\tEnter a string(scanf): “); scanf(“%s”, str);
printf(“\n\t String Entered is(scanf) : %s“, str);
getchar(); return 0;
}
Example: Program to illustrate the use of scanf() and printf().
#include<stdio.h>
void main()
{ char name[10];
printf(“enter your name: \n”); scanf(“%s”, name);
printf(“welcome: ”); printf(“%s”, name);
}
Program to illustrate the initialization of string. Program to illustrate the initialization of string. Reviewed by Unknown on 04:52 Rating: 5

No comments:

Powered by Blogger.