【问题标题】:How to check a string contain a certain value in C如何检查字符串是否包含C中的某个值
【发布时间】:2022-01-02 21:21:26
【问题描述】:
#include <stdio.h>
#include <string.h>   
#define CHAR_SIZE 35

//Function to remove white space
char *remove_white_spaces(char *str)
{
    int i = 0, j = 0;
    while (str[i])
    {
        if (str[i] != ' ')
            str[j++] = str[i];
        i++;
    }
    str[j] = '\0';
    return str;
}

void main()
{
    int i = 0; 
    char str[CHAR_SIZE];
    
    printf("\nKey in input: ");
    fgetchar();
    fgets(str , CHAR_SIZE, stdin);
    
    //Remove white space 
    remove_white_spaces(str);
    printf("%s",str);

    //for (i = 0; str[i] != '\0'; ++i);
    //printf("Length of the string: %d", i);

    if (str[i] == '0' || str[i] == '1' )
    {
        printf("CORRECT");
    }
    else
    {
        printf("Wrong Input");
    }

}

我想检查用户是否输入了正确的输入。例如,我输入了 0 01111110 10100000000000000000000。删除空格后,str 输入变为 001111110101000000000000000000000。从这个 str 中,我想检查用户是否只输入了 0 和 1。我得到的结果的输出是正确,如下所示1Output of result

但是,当用户键入另一个值(包括 0 和 1)时。我想得到的输出是错误的输入。但是我得到了正确的结果,如下所示2

Output of result

附加问题,我如何实现一个 if 语句,即 str 必须只有 32 个字符才能继续,否则它必须中断并且用户键只能键入 32 个字符。我可以在 while 循环而不是 if 语句中执行此操作,以便用户无需再次运行代码吗?

【问题讨论】:

  • values 包含什么?什么是“正确输入”?
  • i 初始化为什么?
  • Edit 并显示 minimal reproducible example 以及输入和预期与实际输出。

标签: arrays c string


【解决方案1】:

您可以使用 strtok 来提取字符。你的逻辑也有缺陷。它应该是if (str[i] == '0' || str[i] == '1' 来检查值是“0”还是“1”。这是您可以参考的示例实现:-

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define CHAR_SIZE 100

int main()
{
    char str[CHAR_SIZE];
    printf("\n Key in value: ");
    getchar();
    fgets(str, CHAR_SIZE, stdin);
    char *tok;
    tok = strtok(str, "\n");
    int i = 0;
    tok++; //skip the first character which is a space
    while (*tok != 0x00)
    {

        if (*tok <= 0x31 && *tok >= 0x30)
            tok++;

        else
        {
            printf("Wrong number input ==> %c \n", *tok);
            break;
        }
    }
}

【讨论】:

  • 我已经对 [if (str[i] == '0' || str[i] == '1')] 的 if 语句的代码进行了更改。但是,我遇到了一个问题,如果用户要键入 012345。我应该得到的输出是错误的,因为它包含其他值,例如 3、4、5。但是我得到的输出是正确的。我应该如何处理这个问题。
  • 如果你能分享一个代码 sn-p 会很有帮助
  • (*tok &lt;= 0x31 &amp;&amp; *tok &gt;= 0x30)?!?!不要使用幻数。
  • @AndrewHenle,我没有使用从我的代码中看到的幻数。在我的代码上需要一些帮助。
  • @clintonkavai ,我已经更新了代码,想寻求一些帮助
【解决方案2】:

初始化我: 把相当于C的

int i = 0; 

在你的编程语言中,在进入 while 循环之前应该可以完成这项工作。

【讨论】:

    【解决方案3】:

    首先,您正在检查 str[i] 是否应该等于 0 并且 等于 1 - 这没有任何意义,因为数组中的一个元素只能是一个值,0 1;所以,你应该测试if (str[i] == '0' || str[i] == '1')。 并且,在此之前,你应该初始化 i:int i = 0

    编辑你必须遍历字符串的元素

    int check = 0;
    while (str[i] != '\0')
    {
        if (str[i] == '0' || str[i] == '1')
             i++;
        else {
            check = 1;
            break;
        }
        
    }
    if (check == 0){
         print("CORRECT");
    }
    else {
        printf("WRONG INPUT");
    }
    

    【讨论】:

    • 你没有循环遍历 str while (str[i]) { if (str[i] == '0' || str[i] == '1' ) { printf("CORRECT"); } else { printf("Wrong Input"); } }的所有元素
    • 如何循环遍历所有元素?
    • 我根据你的代码编辑我的答案检查我的答案
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-31
    • 2012-11-11
    • 1970-01-01
    • 2015-12-25
    • 2016-08-09
    相关资源
    最近更新 更多