Program Discussion :: Strings
56 / 60
Write a program to enter a string then count the no. of vowels in the string.
Answer:
#include
using namespace std;
int main()
{
char line[150];
int vowels, consonants;
vowels = consonants = 0;
cout ='a'&& line[i]='A'&& line[i]
Asked In ::
Language:
Monika Jain
20 Jun, 2017 10:39 PM
<!DOCTYPE html>
<html>
<body>
<?php
function No_vowels() {
$arr ="what is your name"; /* input string */
echo "Input string is :" . $arr."<br/>";
$arrc=array(); $no=0; /* initialising an empty array and its initial index
$arrlength=strlen($arr);
for($x=0; $x< $arrlength; $x++) /* loop for reading the input array */
{
/* comparing each character with vowels */
if( ($arr[$x]=="a")||($arr[$x]=="e")||($arr[$x]=="i")||($arr[$x]=="o")||($arr[$x]=="u"))
{
$no++ ; /* for repeating the loop and skipping the below statement */
}
}
/* printing string without vowels */
echo "No of vowels are : ".$no ;
}
No_vowels();
?>
</body>
</html>
Language:
Lekha
7 Jul, 2017 9:30 AM
#include <stdio.h>
int main()
{
char line[150];
int i, vowels, consonants;
vowels = consonants = 0;
printf("Enter a line of string: ");
scanf("%[^\n]", line);
for(i=0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
}
printf("Vowels: %d",vowels);
printf("\nConsonants: %d",consonants);
return 0;
}
Language:
Naman
7 Jul, 2017 9:30 AM
#include <iostream>
using namespace std;
int main()
{
char line[150];
int vowels, consonants;
vowels = consonants = 0;
cout << "Enter a line of string: ";
cin.getline(line, 150);
for(int i = 0; line[i]!='\0'; ++i)
{
if(line[i]=='a' || line[i]=='e' || line[i]=='i' ||
line[i]=='o' || line[i]=='u' || line[i]=='A' ||
line[i]=='E' || line[i]=='I' || line[i]=='O' ||
line[i]=='U')
{
++vowels;
}
else if((line[i]>='a'&& line[i]<='z') || (line[i]>='A'&& line[i]<='Z'))
{
++consonants;
}
}
cout << "Vowels: " << vowels << endl;
cout << "Consonants: " << consonants << endl;
return 0;
}
Language:
Madhu
25 Aug, 2017 12:42 AM
import java.util.Scanner;
import java.io.*;
class Ideone
{
public static void main(String args[])throws IOException
{
String str,inc="";
int i;
Scanner sc=new Scanner(System.in);
str =sc.nextLine();
for(i=0;i<str.length();i++)
{
if(str.charAt(i)==' ')
{
inc=inc+str.charAt(i);
}
else
{
inc=inc+(char)(1+str.charAt(i));
}
}
System.out.println(inc);
}
}
Language:
Siddhartha Paul
19 Jun, 2019 2:16 PM
import java.util.*;
public class CountVowels
{
public static void main(String[] args)
{
int vowel=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the string: ");
String str=sc.nextLine();
int len=str.length();
for(int i=0;i<len;i++)
{
char ch=str.charAt(i);
if((ch=='A')||(ch=='E')||(ch=='I')||(ch=='O')||(ch=='U')||(ch=='a')||(ch=='e')||(ch=='i')||(ch=='o')||(ch=='u'))
vowel++;
else
continue;
}
System.out.println("The no. of vowels: "+vowel);
}
}