"The best way to cheer yourself up is to try to cheer somebody else up." Mark Twain

Thursday, February 25, 2010

Write a program that reads an array of strings and count the number of strings of each length that occurs, as well as the total number of strings. The program should then print the mean (average) entirely of (lowercase and lowercase) letters and is terminated by '.'.


#include<iostream.h>
#include<conio.h>
#include <string.h>
#include<stdio.h>


void main()
{
clrscr();


char str[255] = " Hi \n Enter Strings \n Thanks ";


cout<< "\n Enter a String (~ to terminate ): ";
cin.getline(str, 255, '~');


/* Count no. of strings */
int no_f_strings = 0;
for(int i = 0; str[i] != '\0' ; i++)
   if(str[i] == '\n')
      no_f_strings ++;


/*Count total number of strings */
int tot_strings = 0;
for( i = 0 ; str[i] != '\0' ; i++)
{

/*space or new-line or fullstop means new word starts */


if( str[i] == ' ' || str [i] == '\n' || str[i] == '.')
  {tot_strings++;
  while(str[i] == ' ')
   i++;}
 if(str[i] == '\0')
   i--;
}
tot_strings++; //for the last word in the string array


cout<< "\n The  number of strings of each length : " << no_f_strings;


cout<<"\n The total number of strings (words) : " << tot_strings;


int len = strlen(str);


int ucas = 0, lcas = 0, fullst = 0;


for( i=0; str[i] != '\0' ; i++)
{
if(str[i]>='A' && str[i]<='Z')
  ucas++;


if(str[i]>='a' && str[i]<='z')
 lcas++;


if(str[i]=='.')
fullst++;


}


float avg_ucas = (float) (ucase / len);
float avg_lcas = (float) (lcas/len);


cout<< "\n The Mean  of lowercase letters in strings of array  = " <<  avg_lcas;

cout<< "\n The Mean  of uppercase letters in strings of array = " << avg_ucas;


cout<< "\n The total number of  terminating fullstop= " << fullst;



getch();
}

No comments:

Post a Comment

C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.
Now Playing: Ballade Pour Adeline

About Me

My photo
I m an IT lecturer of a college. I love social-work. I want to do something beneficial for society before dying , that can promote our society, to some extent.