Write a program to count words not having alphabet 'a' at second position in a string ( any coding language )
Answers
C Program to Count Number of Words in a string:
Explanation:
- #include <stdio.h>
- #include <string.h>
- void main()
- {
- char s[200];
- int count = 0, i;
- printf("Enter the string:\n");
- scanf("%[^\n]s", s);
- for (i = 0;s[i] != '\0';i++)
- {
- if (s[i] == ' ' && s[i+1] != ' ')
- count++;
- }
- printf("Number of words in given string are: %d\n", count + 1);
- }
Answer:
A C-program to count words not having alphabet 'a' at second position in a string :
#include <stdio.h>
#include <string.h>
int main() {
char sentence[200];
int i;
int counter = 0, word=1;
printf("Enter sentence: ");
fflush(stdin);
fgets(sentence, 500, stdin);
int len = strlen(sentence);
printf( "%s",sentence);
for(i=0;i<len;i++)
{
if(sentence[i]!=' ' && sentence[i+1]==' ')
word=word+1;
}
for (i = 0; sentence[i] !='\0'; i++)
{
if (i==0 && sentence[i+1]=='a')
{
counter = counter +1;
}
if (sentence[i] == ' ' && sentence[i+2] == 'a')
{
counter = counter +1;
}
}
printf("No.of words in the sentence that is having alphabet 'a' at second position is : %d\n", counter);
printf("No.of words in the sentence is : %d\n", word);
printf("No.of words in the sentence that is not having alphabet 'a' at second position is : %d\n", word-counter);
return 0;
}