Character array
Single dimension character array
char n[20];
The variable “n” is a character array , it can store max 19 characters, 1 location is reserved for ‘\0’(NULL character).
Note:
* ‘\0’ (NULL character) is not included in the length of the string.
* ‘\0’ (NULL character) is included in the size of the string.
Example:
N=”hello”
Size =6
Length=5
Question:1
C program to take input for a string and display it in the given format? And display length of string?
Example:
n=”hello”
o/p
h
e
l
l
o
Sol:
/* Using for loop */
#include<stdio.h>
int main()
{
  char n[20];
  int i=0;
  printf("Enter any string ");
  gets(n);
  for(i=0;i<=n[i]!='\0';i++)
  {
      printf("%c\n",n[i]);
  }
  printf("Len = %d\n",i);
  return(0);
}
/* Using while loop */
#include<stdio.h>
int main()
{
  char n[20];
  int i=0;
  printf("Enter any string ");
  gets(n);
  while(n[i]!='\0')
  {
      printf("%c\n",n[i]);
      i++;
  }
  printf("Len = %d\n",i);
  return(0);
}
/* Output */ Enter any string hello hi h e l l o h i Len = 8
Question:2
C program to take input for a string and display it and also print the following:
* total length i.e. total characters
* total alphabets
Sol:
/* Using for loop */
#include<stdio.h>
int main()
{
  char n[20];
  int i=0,a=0;
  printf("Enter any string ");
  gets(n);
  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++;
  }
  printf("Len = %d\n",i);
  printf("Total alphabets = %d\n",a);
  return(0);
}
/* Using while loop */
#include<stdio.h>
int main()
{
  char n[20];
  int i=0,a=0;
  printf("Enter any string ");
  gets(n);
  while(n[i]!='\0')
  {
      printf("%c\n",n[i]);
      if((n[i]>='A' && n[i]<='Z') || (n[i]>='a' && n[i]<='z'))
         a++;
      i++;
  }
  printf("Len = %d\n",i);
  printf("Total alphabets = %d\n",a);
  return(0);
}
/* Output */ Enter any string he23llo h e 2 3 l l o Len = 7 Total alphabets = 5
Question:3
C program to take input for a string and display it and also print the following:
* total length i.e. total characters
* total alphabets
* total upper case alphabet
* total lower case alphabets
* total digits
* total spaces
* total special characters
Sol:
/* Using for loop */
#include<stdio.h>
int main()
{
  char n[20];
  int i=0,a=0,ua=0,la=0,d=0,sp=0,spl=0;
  printf("Enter any string ");
  gets(n);
  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);
  return(0);
}
/* Using while loop */
#include<stdio.h>
int main()
{
  char n[20];
  int i=0,a=0,ua=0,la=0,d=0,sp=0,spl=0;
  printf("Enter any string ");
  gets(n);
  while(n[i]!='\0')
  {
      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++;
        i++;
  }
  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);
  return(0);
}
/* Output */ Enter any string He23 $%45llo H e 2 3 $ % 4 5 l l o Len = 12 Total alphabets = 5 upper alphabets = 1 lower alphabets = 4 digits = 4 spaces = 1 special characters = 2




