Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 2

Count Words

#include<iostream>
using namespace std;
int is_digit(char ch)
{
if(ch>='0'&&ch<='9')
{
return 1;
}
else
{
return 0;
}
}
int is_alpha(char ch)
{
if(ch>='a'&&ch<='z')
{
return 1;
}
if(ch>='A'&&ch<='Z')
{
return 2;
}
else
{
return 0;
}
}
int is_special(char ch)
{
if(is_alpha(ch)||is_digit(ch))
{
return 0;
}
else
return 1;

}
int count_words(char *arr)
{
int count=0;
int i;

for(i=0;arr[i]!='\0';i++)
{
if(is_special(arr[i])&&!is_special(arr[i+1]))
{
count++;
}
}
if(is_special(arr[0]))
{
count--;
}
return count+1;
}
void main()
{
char arr[100];
gets(arr);
count_words(arr);
int res= count_words(arr);
cout<<res;

You might also like