c++ - 字符串对抗问题
c++ - string confrontation issue
我必须用 C++ 创建一个小程序,期望输入字符串并以字母形式输出其内容(有多少,等等)。
我这样做是通过创建一个包含 26 个块的整数数组,并用一个 for 循环来处理我的字符串和字符串 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 以及小写字母。如果找到与索引 i 的对应关系,则第 i 个数组块的值递增 1,并中断 for 循环。
但是,我有一个小问题:如果插入了字母表字符串中不存在的字符(如空格或撇号),它会被计为 i=0(字母 A)的对应关系。但是,如果我将该字符也包含在我的字母字符串中,并使用我的 for 循环遍历它,则不会出现该问题。
这是我的.cc文件(函数实现)
#include "08.Count.h"
#include <string>
#include <iostream>
using namespace std;
int chartoint (char a) {
//Converts an alphabetic character to its corresponding position in the alphabet (as an integer)
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alphabet2 = "abcdefghijklmnopqrstuvwxyz";
for (int i=0; i<26; i++)
if ((a==alphabet[i])||(a==alphabet2[i])) {
//check if the char corresponds to the upper-case OR lower-case i-th letter of the alphabet
return i;
//when we find a correspondence, we output the point at where we've found it
break;
//waste of time continuing cycling if the correspondence is already found
}
}
void list (string word) {
//Outputs the alphabetic distribution of the input string
int array[26];
//array to store the correspondence data for each alphabet letter
for (int i=0; i<26; i++)
array[i]=0; //Initialize
for (int i=0; word[i]!='[=10=]'; i++)
array[chartoint(word[i])]++;
//chartoint(word[i]) outputs the position of the alphabet the i-th character of the string word corresponds. We increment the value of that position in the array by one
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i=0; i<26; i++)
if (array[i]!=0)
cout << array[i] << " " << alphabet [i] << ", ";
//we output the distribution in letters, but only for the letters that are found at least one time
}
这是我的主程序
#include <iostream>
#include <string>
#include "08.Count.h"
using namespace std;
int main()
{
string word;
cout << "Insert phrase" << endl;
getline(cin, word);
//To avoid the input string being terminated by a whitespace
cout << "The phrase " << word << " contains:" << endl;
list (word);
cout << endl;
}
在您的 chartoint
函数中,只有一个 return
内部循环。当字符不是字母表时,没有任何措施可以处理。 So in that case the return value is undefined
(对你来说它返回为 0)
所以把函数chartoint
改成
int chartoint (char a) {
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alphabet2 = "abcdefghijklmnopqrstuvwxyz";
for (int i=0; i<26; i++)
if ((a==alphabet[i])||(a==alphabet2[i])) {
return i;
}
return -1; // <------ indicates a is not an alphabet
}
将函数列表更改为
void list (string word) {
int array[26],index;
for (int i=0; i<26; i++)
array[i]=0; //Initialize
for (int i=0; word[i]!='[=11=]'; i++)
{
index = chartoint(word[i]);
if(index!=-1) // <--- Check returned index
array[index]++;
}
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i=0; i<26; i++)
if (array[i]!=0)
cout << array[i] << " " << alphabet [i] << ", ";
}
因此,如果返回的 index
不是 -1,则将相应的字符数加 1。否则忽略它,因为它不是字母表。
我必须用 C++ 创建一个小程序,期望输入字符串并以字母形式输出其内容(有多少,等等)。
我这样做是通过创建一个包含 26 个块的整数数组,并用一个 for 循环来处理我的字符串和字符串 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" 以及小写字母。如果找到与索引 i 的对应关系,则第 i 个数组块的值递增 1,并中断 for 循环。
但是,我有一个小问题:如果插入了字母表字符串中不存在的字符(如空格或撇号),它会被计为 i=0(字母 A)的对应关系。但是,如果我将该字符也包含在我的字母字符串中,并使用我的 for 循环遍历它,则不会出现该问题。
这是我的.cc文件(函数实现)
#include "08.Count.h"
#include <string>
#include <iostream>
using namespace std;
int chartoint (char a) {
//Converts an alphabetic character to its corresponding position in the alphabet (as an integer)
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alphabet2 = "abcdefghijklmnopqrstuvwxyz";
for (int i=0; i<26; i++)
if ((a==alphabet[i])||(a==alphabet2[i])) {
//check if the char corresponds to the upper-case OR lower-case i-th letter of the alphabet
return i;
//when we find a correspondence, we output the point at where we've found it
break;
//waste of time continuing cycling if the correspondence is already found
}
}
void list (string word) {
//Outputs the alphabetic distribution of the input string
int array[26];
//array to store the correspondence data for each alphabet letter
for (int i=0; i<26; i++)
array[i]=0; //Initialize
for (int i=0; word[i]!='[=10=]'; i++)
array[chartoint(word[i])]++;
//chartoint(word[i]) outputs the position of the alphabet the i-th character of the string word corresponds. We increment the value of that position in the array by one
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i=0; i<26; i++)
if (array[i]!=0)
cout << array[i] << " " << alphabet [i] << ", ";
//we output the distribution in letters, but only for the letters that are found at least one time
}
这是我的主程序
#include <iostream>
#include <string>
#include "08.Count.h"
using namespace std;
int main()
{
string word;
cout << "Insert phrase" << endl;
getline(cin, word);
//To avoid the input string being terminated by a whitespace
cout << "The phrase " << word << " contains:" << endl;
list (word);
cout << endl;
}
在您的 chartoint
函数中,只有一个 return
内部循环。当字符不是字母表时,没有任何措施可以处理。 So in that case the return value is undefined
(对你来说它返回为 0)
所以把函数chartoint
改成
int chartoint (char a) {
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
string alphabet2 = "abcdefghijklmnopqrstuvwxyz";
for (int i=0; i<26; i++)
if ((a==alphabet[i])||(a==alphabet2[i])) {
return i;
}
return -1; // <------ indicates a is not an alphabet
}
将函数列表更改为
void list (string word) {
int array[26],index;
for (int i=0; i<26; i++)
array[i]=0; //Initialize
for (int i=0; word[i]!='[=11=]'; i++)
{
index = chartoint(word[i]);
if(index!=-1) // <--- Check returned index
array[index]++;
}
string alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
for (int i=0; i<26; i++)
if (array[i]!=0)
cout << array[i] << " " << alphabet [i] << ", ";
}
因此,如果返回的 index
不是 -1,则将相应的字符数加 1。否则忽略它,因为它不是字母表。