#include <stdio.h>
#include <conio.h>
int charset[26];
void resetcharset()
{
for (int i=0;26>i;i++)
charset[i]=0;
}
void showcount()
{
for (int i=0;26>i;i++)
if (charset[i]>0)
printf ("%c = %d\t",i+'A',charset[i]);
}
void getcount(char s[])
{
for (int pos=0;s[pos];pos++)
{
if ((s[pos]>='A') && (s[pos]<='Z'))
charset[s[pos]-'A']++;
else if ((s[pos]>='a') && (s[pos]<='z'))
charset[s[pos]-'a']++;
}
}
int main()
{
char s[500];
clrscr();
printf("Please enter a text :\n");
gets(s);
resetcharset();
getcount(s);
showcount();
printf ("\nPress any key to continue.\n");
getch();
return 0;
}
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
int getcount(char s[])
{
int state=0,words=0;
char chars[]=" !()*+,./:;<=>?[\\]^`{}~";
for (int pos=0;s[pos];pos++)
{
if (((s[pos]>='A') && (s[pos]<='Z'))
|| ((s[pos]>='a') && (s[pos]<='z'))
|| ((s[pos]>='0') && (s[pos]<='9')))
state=1;
else
for (int chpos=0;chars[chpos];chpos++)
if (chars[chpos]==s[pos])
{
if (state)
words++;
state=0;
break;
}
}
if (state)
words++;
return words;
}
int main()
{
char s[500];
clrscr();
cout << "Please enter a text :" << endl;
gets(s);
cout << endl << "Count of words = " << getcount(s);
cout << endl << "Press any key to continue." << endl;
getch();
return 0;
}
#include <stdio.h>
#include <conio.h>
#include <iostream.h>
#include <string.h>
struct word
{
char text[30];
int count;
} words[100];
int wordcount=0;
void showwords()
{
cout << endl << "Word List :" << endl;
for (int i=0;i<wordcount;i++)
cout << words[i].text << " (" << words[i].count << ")" << endl;
}
void addword(char word[],int length)
{
char s[30];
strncpy(s,word,length);
s[length]=0;
for (int i=0;i<wordcount;i++)
if (strcmpi(s,words[i].text)==0)
break;
if (i==wordcount)
{
strcpy(words[wordcount].text,s);
words[wordcount].count=1;
wordcount++;
}
else
words[i].count++;
}
int getcount(char s[])
{
int state=0,count=0,start;
char chars[]=" !()*+,./:;<=>?[\\]^`{}~";
for (int pos=0;s[pos];pos++)
{
if (((s[pos]>='A') && (s[pos]<='Z'))
|| ((s[pos]>='a') && (s[pos]<='z'))
|| ((s[pos]>='0') && (s[pos]<='9')))
{
if (state==0)
{
state=1;
start=pos;
}
}
else
for (int chpos=0;chars[chpos];chpos++)
if (chars[chpos]==s[pos])
{
if (state)
{
addword(&s[start],pos-start);
count++;
state=0;
}
break;
}
}
if (state)
{
addword(&s[start],pos-start);
count++;
}
return count;
}
int main()
{
char s[500];
clrscr();
cout << "Please enter a text :" << endl;
gets(s);
cout << endl << "Count of words = " << getcount(s);
showwords();
cout << endl << "Press any key to continue." << endl;
getch();
return 0;
}