原文:
给定一个带有大写、小写和特殊字符的字符串。输入字符串以空格或点结束。问题是在单独的一行中计算字符串中每个字符的字数、元音和频率。 例:****
**input :** how good god is.
**output :**
number of words = 4
number of vowels = 5
number of upper case characters = 6
character = frequency = 3
character = . frequency = 1
character = d frequency = 1
character = g frequency = 2
character = h frequency = 1
character = i frequency = 1
character = o frequency = 1
character = d frequency = 1
character = o frequency = 3
character = s frequency = 1
character = w frequency = 1
*方法:*我们使用来存储字符及其频率。treemap 用于按排序顺序获取输出。 下面是上述方法的 java 实现:
c
// c program to print number of words,
// vowels and frequency of each character
#include
using namespace std;
void words(string str)
{
int wcount = 0, ucount = 0, vcount = 0;
for (int i = 0; i < str.length(); i )
{
char c = str[i];
switch (c)
{
case ' ':
case '.':
wcount ; // more delimiters can be given
}
switch (c)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vcount ;
}
if (c >= 65 and c <= 90) ucount ;
}
cout << "number of words = "
<< wcount << endl;
cout << "number of vowels = "
<< vcount << endl;
cout << "number of upper case characters = "
<< ucount << endl;
}
// function to calculate the frequency
// of each character in the string
void frequency(string str)
{
// creates an empty treemap
map hmap;
// traverse through the given array
for (int i = 0; i < str.length(); i )
hmap[str[i]] ;
// print result
for (auto i : hmap)
{
cout << "character = " << i.first;
cout << " frequency = "
<< i.second << endl;
}
}
// driver code
int main(int argc, char const *argv[])
{
string str = "geeks for geeks.";
words(str);
frequency(str);
return 0;
}
// this code is contributed by
// sanjeev2552
java 语言(一种计算机语言,尤用于创建网站)
// java program to print number of words,
// vowels and frequency of each character
import java.util.*;
import java.lang.*;
import java.io.*;
public class stringfun
{
string str = "geeks for geeks.";
void words()
{
int wcount = 0, ucount = 0, vcount = 0;
for (int i = 0; i < str.length(); i )
{
char c = str.charat(i);
switch (c)
{
case ' ':
case '.':
wcount ; // more delimiters can be given
}
switch (c)
{
// program for calculating number of vowels
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
vcount ;
}
if (c >= 65 && c <= 90)
{
ucount ;
}
}
system.out.println("number of words = " wcount);
system.out.println("number of vowels = " vcount);
system.out.println("number of upper case characters = "
ucount);
}
// function to calculate the frequency
// of each character in the string
void frequency()
{
// creates an empty treemap
treemap hmap =
new treemap();
// traverse through the given array
for (int i = 0; i < str.length(); i )
{
integer c = hmap.get(str.charat(i));
// if this is first occurrence of element
if (hmap.get(str.charat(i)) == null)
hmap.put(str.charat(i), 1);
// if elements already exists in hash map
else
hmap.put(str.charat(i), c);
}
// print result
for (map.entry m:hmap.entryset())
system.out.println("character = " m.getkey()
" frequency = " m.getvalue());
}
// driver program to run and test above program
public static void main(string args[]) throws ioexception
{
stringfun obj = new stringfun();
obj.words();
obj.frequency();
}
}
python 3
# python3 program to print number of words,
# vowels and frequency of each character
# a method to count the number of
# uppercase character, vowels and number of words
def words(str):
wcount = vcount = ucount = i = 0
while i < len(str):
ch = str[i]
# condition checking for word count
if (ch == " " or ch == "."):
wcount = 1
# condition checking for vowels
# in lower case
if(ch == "a" or ch == "e" or
ch == "i" or ch == 'o' or ch == "u"):
vcount = 1
# condition checking for vowels in uppercase
if (ch == "a" or ch == "e" or
ch == "i" or ch == 'o' or ch == "u"):
vcount = 1
# condition checking for upper case characters
if (ord(ch) >= 65 and ord(ch) <= 90):
ucount = 1
i = 1
print("number of words = ", wcount)
print("number of vowels = ", vcount)
print("number of upper case characters = ",
ucount)
# a method to print the frequency
# of each character.
def frequency(str):
i = 1
# checking each and every
# ascii code character
while i < 127:
ch1 = chr(i)
c = 0
j = 0
while j < len(str):
ch2 = str[j]
if(ch1 == ch2):
c = 1
j = 1
# condition to print the frequency
if c > 0:
print("character:", ch1
" frequency:", c)
i = 1
# driver code
# sample string to check the code
s = "geeks for geeks."
# function calling
words(s)
frequency(s)
# this code is contributed by animesh_gupta
*输出:*
number of words = 3
number of vowels = 5
number of upper case characters = 2
character = frequency = 2
character = . frequency = 1
character = g frequency = 2
character = e frequency = 4
character = f frequency = 1
character = k frequency = 2
character = o frequency = 1
character = r frequency = 1
character = s frequency = 2
时间复杂度:o(n),其中 n 是字符串中的字符数。 辅助空间:o(1)。
麻将胡了pg电子网站的版权属于:月萌api www.moonapi.com,转载请注明出处