Friday, November 21, 2014

C using pointers



C Program to Count number of words, digits,vowels using pointers

#include
#include
#include
#include
#define low 1
#define high 0
void main()
{
   int nob, now, nod, nov, nos, pos = high;
   char *str;
   nob = now = nod = nov = nos = 0;
   clrscr();
   printf("Enter any string : ");
   gets(str);
   while (*str != '\0')
   {
      if (*str == ' ')
      {
         pos = high;
         ++nob;
      }
       else if (pos == high)
      {
        
         pos = low;
         ++now;
      }
       if (isdigit(*str))
         ++nod;
      if (isalpha(*str))
         switch (*str)
        {
            case 'a':
            case 'e':
            case 'i':
            case 'o':
            case 'u':
            case 'A':
            case 'E':
            case 'I':
            case 'O':
            case 'U':
               ++nov;
               break;
         }

 (*str))
         ++nos;
      str++;
   }
   printf("\nNumber of words  %d", now);
   printf("\nNumber of spaces %d", nob);
   printf("\nNumber of vowels %d", nov);
   printf("\nNumber of digits %d", nod);
   printf("\nNumber of special characters %d", nos);
   getch();
}

Output

Enter any string : pritesh a taral from c4learn.com
Number of words 5
Number of spaces 4
Number of vowels 9
Number of digits 1
Number of special characters 5

C Program to Sort the list of Strings

#include
#include
#include
int main()
{
   char *str[5], *temp;
   int i, j, n;
   printf("\nHow many names do you want to have?");
   scanf("%d", &n);
    for (i = 0; i < n; i++)
   {
      printf("\nEnter the name %d: ", i);
      flushall();
      gets(str[i]);
    }
    for (i = 0; i < n; i++)
     {
      for (j = 0; j < n - 1; j++)
        {
         if (strcmp(str[j], str[j + 1]) > 0) {
            strcpy(temp, str[j]);
            strcpy(str[j], str[j + 1]);
            strcpy(str[j + 1], temp);
         }
      }
   }
    flushall();
    printf("\nSorted List : ");
   for (i = 0; i < n; i++)
      puts(str[i]);
    return (0);
}

Output


How many names do you want to have? 4
Enter the name 0: pri
Enter the name 1: prt
Enter the name 2: prq
Enter the name 3: pra
Sorted List : pra pri prq prt

No comments:

Post a Comment