C++ Programs on Sentences
Find 7 C++ programs below on the following topics
- Count letters
- Count words
- Punctuation marks
- Palindrome string
- Vowels in sentence
1. Count the number of letters in the sentence
#include <iostream>
using namespace std;
struct sentence{
char s[500];
};
struct sentence obj;
void countLetters();
int main()
{
cout << "Enter any sentence" << endl;
cin.getline(obj.s,500);
countLetters();
return 0;
}
void countLetters()
{
int i;
int letters = 0; // letters
for(i = 0; i<500; i++)
{
if( (obj.s[i]>='a' && obj.s[i]<='z') || (obj.s[i]<='A' && obj.s[i]>='Z'))
{
 letters++;
}
}
cout<<"\n No of letters in the sentence are : "<<letters;
}
 
2. Count the number of words in the sentence
#include <iostream>
#include
using namespace std;
struct sentence{
char s[500];
};
struct sentence obj;
void numberofWords();
int main()
{
cout << "Enter any sentence" << endl;
fgets( obj.s, 500, stdin );
numberofWords();
return 0;
}
void numberofWords()
{
int len = strlen(obj.s);
cout<<"\n lenth is " <<len << ",";
int i;
int words = 0;
i = 0;
while(i < len)
{
if (obj.s[i++] == ' ')
{
words++;
}
}
cout<<"words are : "<<words;
}
  
3. Count the punctuation marks in any sentence
#include
using namespace std;
int main(){
    int countpunct = 0;
    char word;
    
    cout<<"\nEnter the Sentence: ";
    while ((word = getchar()) != EOF && word != '\n'){
        if (word == '.' || word == '?' ||  word == '!' ||  
        word == '(' ||  word == ')' ||  word == '*' ||  word == '&'){
            countpunct++;
        }
    }
    cout<<"\nThe number of punctuation marsks is : "<<countpunct;
}
  
4. Program to find if string is Palindrome
#include <iostream>
#include
using namespace std;
struct sentence{
 char string[50];
};
struct sentence obj;
void function();
int main(){
cout << "Enter any  string: "; cin >> obj.string;
function();
return 0;
}
   
void function()
{
int i, length;
int flag = 0;
length = strlen(obj.string);
for(i=0;i < length ;i++){
if(obj.string[i] != obj.string[length-i-1]){
    flag = 1;
    break;
   }
}
if (flag) {
cout << obj.string << " is not a palindrome" << endl; 
}    
else {
cout << obj.string << " is a palindrome" << endl; 
}
}
  
5. Program to count letters in each word of sentence
#include <iostream>
#define MAX_WORDS	10
using namespace std;
struct st{
char text[100]; // to store string
int cnt[MAX_WORDS]; //to store length of the words   
};
struct st obj;
void printwords();
int main()
{
//reading string
cout<<"Enter any string: ";
fgets( obj.text, 100, stdin );
printwords();
return 0;
}
void printwords()
{
int len=0,i=0,j=0;
while(1)
{
	if(obj.text[i]==' ' || obj.text[i]=='\0')
	{
	//check NULL
	if(obj.text[i]=='\0')
	{
		if(len>0)
		{
			obj.cnt[j++]=len;
			len=0;
		}
		break; //terminate the loop
	}
	obj.cnt[j++]=len;
	len=0;
	}
	else
	{
		len++;
	}		
	i++;
}
cout<<"\n Here is Words length : \n";
for(i=0;i<<j;i++)
{
	cout<<" "<<obj.cnt[i];
}
}
 
6. Count the vowels in the sentence
#include
#include
using namespace std;
int vowelCount ( char *);
int main(){
char word[80];
cout << "Enter a line: ";
cin.getline (word, 80);
cout << "\n Vowel count: " << vowelCount(word) << endl;
return 0;
}
// returns the number of vowels in a char array
int vowelCount ( char * pCh){
int vowels = 0;
while(*pCh){
if(strspn(pCh, "aeiou"))
vowels++;
pCh++;
}
return vowels;
}
   
7. Is there a palindrome word the sentence
#include
using namespace std;
struct sentence{
  char str[50];  
};
struct sentence obj;
int countPalindrome(char str[50], int st, int ed);
int main()
{
int pal = 0, len = 0, i, start = 0, end;
cout<<"\n\n\t Enter any sentence : ";
cin.getline (obj.str, 50);
while(obj.str[len]!='\0')
len++;
len--;
for(i=0;i<=len;i++)
{
if((obj.str[i] == ' ' && obj.str[i+1] != ' ') || i == len)
{
if(i == len)
end = i;
else
end = i - 1;
if( countPalindrome (obj.str, start, end ) )
pal++;
start = end + 2;
}
}
cout<<"\n\n\t the total number of palindrome are :" <<pal;
return 0;
}
int countPalindrome(char str[50], int st, int ed)
{
int i, pal=0;
for(i=0; i<=(ed-st)/2; i++)
{
	if(str[st+i] == str[ed-i])
	pal = 1;
	else
	{
	pal = 0;
	break;
}
}
return pal;
}  
