在C++中防止字符数组中的双空格输入。

huangapple go评论69阅读模式
英文:

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&lt;iostream&gt;
using namespace std;
bool isValidData( char data[60] );
int main()
{
	char data[60];
	cin.getline(data,60);
	bool name = isValidData(data);
	cout&lt;&lt;name;
}
bool isValidData( char data[60] ) 
{
    int i=0;
    while(data[i]!=&#39;
#include&lt;iostream&gt;
using namespace std;
bool isValidData( char data[60] );
int main()
{
char data[60];
cin.getline(data,60);
bool name = isValidData(data);
cout&lt;&lt;name;
}
bool isValidData( char data[60] ) 
{
int i=0;
while(data[i]!=&#39;\0&#39;) {
if ( data[i]==&#39; &#39; &amp;&amp; data[i]==&#39;.&#39; &amp;&amp; data[i]==&#39;!&#39;){
if ( data[i+1]==&#39; &#39; &amp;&amp; data[i+1]==&#39;.&#39; &amp;&amp; data[i+1]==&#39;!&#39;)
return false;
}
i++;
}
return true;
}
&#39;) { if ( data[i]==&#39; &#39; &amp;&amp; data[i]==&#39;.&#39; &amp;&amp; data[i]==&#39;!&#39;){ if ( data[i+1]==&#39; &#39; &amp;&amp; data[i+1]==&#39;.&#39; &amp;&amp; data[i+1]==&#39;!&#39;) 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]!=&#39;
bool isValidData( char data[60] ) 
{
int i=0;
while(data[i]!=&#39;\0&#39; &amp;&amp; data[i+1]!=&#39;\0&#39;) {
if ((data[i]==&#39; &#39; || data[i]==&#39;.&#39; || data[i]==&#39;!&#39;) &amp;&amp; data[i+1]==data[i]) {
return false;
}
i++;
}
return true;
}
&#39; &amp;&amp; data[i+1]!=&#39;
bool isValidData( char data[60] ) 
{
int i=0;
while(data[i]!=&#39;\0&#39; &amp;&amp; data[i+1]!=&#39;\0&#39;) {
if ((data[i]==&#39; &#39; || data[i]==&#39;.&#39; || data[i]==&#39;!&#39;) &amp;&amp; data[i+1]==data[i]) {
return false;
}
i++;
}
return true;
}
&#39;) { if ((data[i]==&#39; &#39; || data[i]==&#39;.&#39; || data[i]==&#39;!&#39;) &amp;&amp; data[i+1]==data[i]) { return false; } i++; } return true; }

huangapple
  • 本文由 发表于 2020年1月3日 21:25:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/59579405.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定