Modal Box

String in C

In C programming, a string is an array of characters terminated with a null character \0.
In C , array of character is declared as following :
  char arrayname[size];
Single character may be taken as input by scanf function as ,
  scanf(“%c”,&ch);   where ch is the character variable.

If multiple characters has to enter as input , then instead of scanf in loop, gets function is used.

Syntax: gets(arrayname);
it automatically stores null at the last position of array.

Similarly, to print the string (char array), puts function may be used.
eg: puts(arrayname);

Program to take user's name as input and print it back.

#include <stdio.h>
#include <conio.h>
void main()
{
   char name[20];
   printf("enter your name\n");
   gets(name);   //function to accept the user entered string.
   printf("name entered by you :");   //n is used to display the value in new line.
   puts(name);
}

Program to find length of the string and calculate the no of vowels in it.

#include <stdio.h>
#include <conio.h>
void main()
{
   char str[50];
int i=0,count=0; printf("enter the string\n"); gets(str);   //function to accept the user entered string. while(str[i]!=NULL)
{
if(str[i]=='a' || str[i]=='e' ||str[i]=='i' || str[i]=='o' ||str[i]=='u')
{
count++;
}
i++;
}
printf("length=%d , No of Vowels=%d",i,count);
}

Video/ C Introduction

Watch video in full size
Wordpress Social Share Plugin powered by Ultimatelysocial