英文:
prevent input for double spaces in character arrayin C++
问题
I have translated the code-related portion for you:
我必须避免在我的字符数组中出现双空格、双感叹号和双句号。我必须使用字符数组,例如:有效数据:"It is raining.!" 无效数据:"It is raining!!.(这只是一个示例)
我尝试了以下方法,但未获得所需的结果。请帮助我。
#include <iostream>
using namespace std;
bool isValidData(char data[60]);
int main()
{
char data[60];
cin.getline(data, 60);
bool name = isValidData(data);
cout << name;
}
bool isValidData(char data[60])
{
int i = 0;
while (data[i] != '我必须避免在我的字符数组中出现双空格、双感叹号和双句号。我必须使用字符数组,例如:有效数据:"It is raining.!" 无效数据:"It is raining!!.(这只是一个示例)
我尝试了以下方法,但未获得所需的结果。请帮助我。
#include <iostream>
using namespace std;
bool isValidData(char data[60]);
int main()
{
char data[60];
cin.getline(data, 60);
bool name = isValidData(data);
cout << name;
}
bool isValidData(char data[60])
{
int i = 0;
while (data[i] != '\0') {
if (data[i] == ' ' && data[i] == '.' && data[i] == '!') {
if (data[i + 1] == ' ' && data[i + 1] == '.' && data[i + 1] == '!')
return false;
}
i++;
}
return true;
}
') {
if (data[i] == ' ' && data[i] == '.' && data[i] == '!') {
if (data[i + 1] == ' ' && data[i + 1] == '.' && data[i + 1] == '!')
return false;
}
i++;
}
return true;
}
英文:
I have to avoid double spaces, double ! and double full stops in my character array. I must have to use character array btw.
e.g Valid data: "It is raining.!" Invalid Data: "It is raining!!." (it is only example)
I tried the following way but am not getting desired result. Plz help me.
#include<iostream>
using namespace std;
bool isValidData( char data[60] );
int main()
{
char data[60];
cin.getline(data,60);
bool name = isValidData(data);
cout<<name;
}
bool isValidData( char data[60] )
{
int i=0;
while(data[i]!='#include<iostream>
using namespace std;
bool isValidData( char data[60] );
int main()
{
char data[60];
cin.getline(data,60);
bool name = isValidData(data);
cout<<name;
}
bool isValidData( char data[60] )
{
int i=0;
while(data[i]!='\0') {
if ( data[i]==' ' && data[i]=='.' && data[i]=='!'){
if ( data[i+1]==' ' && data[i+1]=='.' && data[i+1]=='!')
return false;
}
i++;
}
return true;
}
') {
if ( data[i]==' ' && data[i]=='.' && data[i]=='!'){
if ( data[i+1]==' ' && data[i+1]=='.' && data[i+1]=='!')
return false;
}
i++;
}
return true;
}
答案1
得分: 0
你的代码失败是因为没有字符可以同时等于'`'、'!'和空格字符' '。
即使你修复了这个问题,你仍然会将'!, '标记为无效。
直接测试属性而不是这样:
bool isValidData(char data[60])
{
int i = 0;
while (data[i] != 'bool isValidData(char data[60])
{
int i = 0;
while (data[i] != '\0' && data[i + 1] != '\0') {
if ((data[i] == ' ' || data[i] == '.' || data[i] == '!') && data[i + 1] == data[i]) {
return false;
}
i++;
}
return true;
}
' && data[i + 1] != 'bool isValidData(char data[60])
{
int i = 0;
while (data[i] != '\0' && data[i + 1] != '\0') {
if ((data[i] == ' ' || data[i] == '.' || data[i] == '!') && data[i + 1] == data[i]) {
return false;
}
i++;
}
return true;
}
') {
if ((data[i] == ' ' || data[i] == '.' || data[i] == '!') && data[i + 1] == data[i]) {
return false;
}
i++;
}
return true;
}
英文:
Your code fails because no character can simultaneously be equal to ,
, !
, and
.
Even if you fix that, you will still flag ,!
as invalid.
Test the property directly instead:
bool isValidData( char data[60] )
{
int i=0;
while(data[i]!='bool isValidData( char data[60] )
{
int i=0;
while(data[i]!='\0' && data[i+1]!='\0') {
if ((data[i]==' ' || data[i]=='.' || data[i]=='!') && data[i+1]==data[i]) {
return false;
}
i++;
}
return true;
}
' && data[i+1]!='bool isValidData( char data[60] )
{
int i=0;
while(data[i]!='\0' && data[i+1]!='\0') {
if ((data[i]==' ' || data[i]=='.' || data[i]=='!') && data[i+1]==data[i]) {
return false;
}
i++;
}
return true;
}
') {
if ((data[i]==' ' || data[i]=='.' || data[i]=='!') && data[i+1]==data[i]) {
return false;
}
i++;
}
return true;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论