在处理 C 字符串时,如何将用户的第一个输入替换为当前输入
how do I replace the user first input with current input when dealing with c-string
我在尝试用其他用户输入替换用户输入时遇到问题。
例如,如果用户输入"我爱狗",然后我们问他们是否要输入其他字符串,他们输入"我吃了很多冰淇淋"。
如何将用户的第一个输入替换为当前输入?
// Function Prototype
int countVowels(char * str);
int countCons(char * str);
int main()
{
const int SIZE = 81; // Max size of the array
//const int V_SIZE = 6; // Size of the vowel array
char newSentence[SIZE];
char userString[SIZE];
//char vowels[V_SIZE] = {'a', 'e', 'i', 'o', 'u'};
char choice; // To hold the menu choice
char *strPtr = userString; // Declare and initialize the pointer
char *sentPtr = newSentence;
// Get the string from the user
cout << "Please enter a string. (Maximum of 80 characters) :nn";
cin.getline(userString, SIZE);
do{
// Display the menu and get a choice
cout << "nnA. Count the number of vowels in the string n";
cout << "B. Count the number of consonants in the string n";
cout << "C. Enter another string n";
cout << "D. Exit the program nn";
cout << "Please make your selection: ";
cin >> choice;
// Menu selections
if (tolower(choice) == 'a')
{
countVowels(userString);
cout << "This string contains " << countVowels(userString);
cout << " vowels. n";
}
else if (tolower(choice) == 'b')
{
countCons(userString);
cout << "This string contains " << countCons(userString);
cout << " consonants. n";
}
else if (tolower(choice) == 'c')
{
cout << "Please enter other string: ";
cin.getline(newSentence, SIZE);
userString.replace();
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
}
else
{
system("PAUSE");
return 0;
}
} while (choice != 'D');
}
我在菜单选择 C 时遇到问题。如何将 userString 替换为 newSentence?
我希望不需要两个数组声明来实现您的目的。 而不是cin.getline(newSentence, SIZE); 使用 cin.getline(userString, SIZE); 它将通过覆盖来保存新字符串。
else if (tolower(choice) == 'c')
{
cout << "Please enter other string: ";
cin.getline(userString, SIZE);
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), 'n');
}
如果你看别的东西,定义它。
C 中有一个内置函数,称为 strcpy 用另一个字符串覆盖一个字符串。
strcpy(userString, newSentence);
左侧的字符串现在将成为 newSentence 字符串。
相关文章:
- 为什么它只打印双链接列表的第一个值,而我的程序却崩溃了
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 如何仅读取文本文件中的第一个值
- 在C++中,如何在第一个"system()"结束后执行第二个"system()"?
- 如何设置默认参数以防用户不输入另一个参数
- 查找不在标准中的第一个值::设置<int>最小-最大值
- C++:忽略第一个 cin.ignore 之后的输入
- 在C++中打印多个矢量的第一个值
- C++去除前x个元素的有效方法,在不改变向量大小的情况下将第x+1个元素推到第一个
- C++第一个cout将不会打印
- 我们可以在第一个else-if条件结束后使用另一个else-if条件吗
- 为什么在我的程序中输入另一个输入会给我不同的结果
- OpenGL:第二个VBO破坏了第一个VBO
- 为什么第一个Dynamic_cast没有投射到基类?
- C++ 验证同一行上两个输入的第一个
- (C++)生成数组中的第一个p*n完美平方数(从键盘输入p和n)
- 输入后,我的程序似乎跳过了第一个IF语句,直接转到ELSE
- 当一长串字符传递给第一个 cin 时,为什么不暂停第二个 cin 上的输入?
- 当我超过第一个 getline() 的输入中的数组大小时,第二个 getline 或其他输入函数不起作用
- 为什么无论输入如何,我的第一个 "if" 语句都会执行?