C Language: Arrays 27

Question:7
C program to input for a string, pass it to a function, and display total vowels in the string.
Sol:

#include<stdio.h>
void show(char n[])
{
     int i=0,v=0;
     while(n[i]!='\0')
     {
          printf("%c\n",n[i]);
          if(n[i]=='A' || n[i]=='a' || n[i]=='E' || n[i]=='e' || n[i]=='O' || n[i]=='o'
		   || n[i]=='I' || n[i]=='i' || n[i]=='U' || n[i]=='u')
          v++;
          
          i++;
     }
     printf("Length of string %d\n",i);
     printf("Total vowels in string %d\n",v);
}
int main()
{
     char n[30];
     printf("Enter any string ");
     gets(n);
     show(n);
}
/* Output */
Enter any string Hello How Are You
H
e
l
l
o

H
o
w

A
r
e

Y
o
u
Length of string 17
Total vowels in string 7

Question:8
C program to input for a string, pass it to a function, and display the following.
* Total length of string
* total upper case alphabets
* total lower case alphabets
* total digits
* total spaces
* total special characters
Sol:

#include<stdio.h>
void show(char n[])
{
	int i=0,a=0,ua=0,la=0,d=0,sp=0,spl=0;
	for(i=0;n[i]!='\0';i++)
  {
      printf("%c\n",n[i]);
      if((n[i]>='A' && n[i]<='Z') || (n[i]>='a' && n[i]<='z'))
      {
         a++;
         if(n[i]>='A' && n[i]<='Z')
            ua++;
         else
            la++;
      }
      else
        if(n[i]>='0' && n[i]<='9')
        d++;
      else
        if(n[i]==' ')
            sp++;
        else
            spl++;
  }
  printf("Len = %d\n",i);
  printf("Total alphabets = %d upper alphabets = %d lower alphabets = %d\n",a,ua,la);
  printf("digits = %d  spaces = %d special characters = %d\n",d,sp,spl);
	
}
int main()
{
  char n[20];
  printf("Enter any string ");
  gets(n);
  show(n);
  return(0);
}
/* Output */
Enter any string Hello How7 Are@ You
H
e
l
l
o

H
o
w
7

A
r
e
@

Y
o
u
Len = 19
Total alphabets = 14 upper alphabets = 4 lower alphabets = 10
digits = 1  spaces = 3 special characters = 1

C Language Programming Tutorial

C Language Tutorial Home     Introduction to C Language     Tokens     If Condition      goto statement and Labelname     Switch Statements     For loop     While Loop     Do while loop     break and continue     Functions     Recursion     Inbuild Functions     Storage Classes     Preprocessor     Arrays     Pointers     Structures and Unions     File Handling     Projects