C:如何发现在任何序列的字符串中都存在一定数量的字符?

C:如何发现在任何序列的字符串中都存在一定数量的字符?,c,string,loops,substring,C,String,Loops,Substring,因此,给定的任务是生成一个函数来检查任何字符串,如果 所有5个字母‘a’、‘b’、‘c’、‘d’和‘e’均包含在任何 (顺序)和 字符串'abcde'是给定字符串的子字符串。 如果1保留(但2未保留),则返回1。 如果1和2都成立,则返回2。 否则,返回0 示例: checkabcde(“someaxbxcxdxemm”) -> 1 checkabcde(“someOtherValue”) -> 0 checkabcde(“xyabcdeping”) -> 2 checkabc

因此,给定的任务是生成一个函数来检查任何字符串,如果

  • 所有5个字母‘a’、‘b’、‘c’、‘d’和‘e’均包含在任何 (顺序)和
  • 字符串'abcde'是给定字符串的子字符串。 如果1保留(但2未保留),则返回1。 如果1和2都成立,则返回2。 否则,返回0
  • 示例:

    checkabcde(“someaxbxcxdxemm”) -> 1
    checkabcde(“someOtherValue”) -> 0
    checkabcde(“xyabcdeping”) -> 2
    checkabcde(“someaxuxdxlxammabcde”) -> 2
    
    在我的方法中,我发现子字符串是“abcde”,但无法确定字符串是否包含任何序列中的“a”、“b”、“c”、“d”、“e”

    int checkabcde(char str[]) {
    
      char str2[] = {
        'a',
        'b',
        'c',
        'd',
        'e'
      };
    
      char str3[5]; //to be filled with elements from str2 when found inconsecutive order
    
      int i, z, x, f;
    
      z = 0; //position for str3
      f = 0; //flag for similarity comparison of str2 and str3
    
      for (i = 0; i < strlen(str); i++) {
        for (x = 0; x < strlen(str2); x++) {
          if (str[i] == str2[x]) {
    
            if ((str[i] == 'a') && (str[i + 1] == 'b') && (str[i + 2] == 'c') && (str[i + 3] == 'd') && (str[i + 4] == 'e')) {
              return 2;
            } else {
    
              if (str3[z] != str[z - 1]) {
                str3[z] = str2[x];
                z++;
              }
    
            }
    
          }
        }
      }
    
      for (i = 0; i < 5; i++) {
        for (x = 0; x < 5; x++) {
          if (str2[i] == str3[x]) {
            f++;
          }
        }
      }
    
      if (f == 5) {
        return 1;
      } else if (f1 == 0) {
        return 0;
      }
    }
    
    int checkabcde(char str[]){
    字符str2[]={
    “a”,
    “b”,
    "c",,
    “d”,
    “e”
    };
    char str3[5];//当发现顺序不连续时,用str2中的元素填充
    int i,z,x,f;
    z=0;//str3的位置
    f=0;//用于比较str2和str3的相似性的标志
    对于(i=0;i

    编辑:不允许使用指针

    除了查找子字符串外,还可以使用单独的结构检查单个字符

    一个由五个布尔组成的数组可以用来存储每个字符的当前值。e、 g:

    bool chars[5] = {false};
    
    for (int i = 0; i < strlen(str); i++) {
        char c = str[i];
        switch(c) {
            case 'a':
                chars[0] = true;
                break;
            case 'b':
                chars[1] = true;
                break;
            // include cases for each character you are looking for
        }
    }
    
    boolchars[5]={false};
    对于(int i=0;i
    如果
    chars
    数组中的每个条目在末尾都为true,则知道该字符串包含所有字符

    除了已经在进行的匹配子字符串的操作之外,还可以执行此操作


    您可能还想查看一个字符串是否包含某个子字符串的检查方法。

    尝试以下方法:

    int checkabcde(char str[]) {
    
        // Check if string contains substring "abcde"
        if (strstr(str, "abcde") != NULL) {
            return 2;
        }
    
        int charCounts[5] = {0, 0, 0, 0, 0};
        int length = strlen(str);
        int i = 0;
    
        // Keep counts of each occurrence of a,b,c,d,e
        for(; i < length; i++) {
            // If "abcde" contains the current character
            if (strchr("abcde", str[i]) != NULL) {
                charCounts[str[i] - 'a']++;
            }
        }
    
        i = 0;
    
        // Check if any of the counts for a,b,c,d,e are 0
        for (; i < 5; i++) {
            if (charCounts[i] == 0) {
                return 0;
            }
        }
    
        // Otherwise we must have found at least 1 of each a,b,c,d,e
        return 1;
    }
    
    int checkabcde(char str[]){
    //检查字符串是否包含子字符串“abcde”
    if(strstrstr(str,“abcde”)!=NULL){
    返回2;
    }
    int charCounts[5]={0,0,0,0,0};
    int长度=strlen(str);
    int i=0;
    //记录a、b、c、d、e的每次出现次数
    对于(;i
  • 对于1,您可以对
    s1
    中的所有字符使用
    f[256]
    设置
    f[ch]=1
    ,然后检查
    s2
    中的每个字符是否为
    f[ch]=1
  • 对于2,您可以使用
    strstr()
  • 以下
    code
    可以工作:

    #include <stdio.h>
    #include <string.h>
    
    int check(const char* s1, const char* s2) {
      int f[256];
      memset(f, 0, sizeof(f));
    
      // check 1
      for (int i = 0; s1[i] != '\0'; ++i)
        f[s1[i]] = 1;
      for (int i = 0; s2[i] != '\0'; ++i)
        if (f[s2[i]] == 0)
          return 0;
      // check 2
      return strstr(s1, s2) == NULL ? 1 : 2;
    }
    
    int main(void) {
      printf("%d\n", check("someaxbxcxdxemm",      "abcde"));
      printf("%d\n", check("someOtherValue",       "abcde"));
      printf("%d\n", check("xyabcdeping",          "abcde"));
      printf("%d\n", check("someaxuxdxlxammabcde", "abcde"));
      return 0;
    }
    
    #包括
    #包括
    整数检查(常量字符*s1,常量字符*s2){
    int f[256];
    memset(f,0,sizeof(f));
    //检查1
    对于(int i=0;s1[i]!='\0';++i)
    f[s1[i]]=1;
    对于(int i=0;s2[i]!='\0';++i)
    如果(f[s2[i]]==0)
    返回0;
    //支票2
    返回strstr(s1,s2)==NULL?1:2;
    }
    内部主(空){
    printf(“%d\n”,复选框(“someaxbxcxdxemm”,“abcde”);
    printf(“%d\n”,勾选(“someOtherValue”,“abcde”);
    printf(“%d\n”,复选框(“xyabcdeping”、“abcde”);
    printf(“%d\n”,勾选(“someaxuxdxlxammabcde”、“abcde”);
    返回0;
    }
    
    str
    中的每个索引处,
    sub
    循环查找子字符串匹配。如果找到,则返回2。
    found
    循环查看该字符str[index]是否为
    abcde
    之一。如果是,则
    检查
    数组中的匹配索引设置为空格。
    处理完
    str
    中的所有字符后,将
    abcde
    check
    进行比较。如果存在匹配项,则该匹配字符在
    str
    中丢失。返回0

    #include <stdio.h>
    #include <string.h>
    
    int checkabcde ( char str[]) {
        char abcde[] = "abcde";
        char check[] = "abcde";
        int index = 0;
        int match = 1;
        int length = strlen ( str);
        int span = strlen ( abcde);
    
        while ( str[index]) {
            if ( str[index] == abcde[0]) {
                match = 1;
                for ( int sub = 0; sub < span; ++sub) {
                    if ( index + sub > length || str[index + sub] != abcde[sub]) {
                        match = 0;
                        break;
                    }
                }
                if ( match) {
                    return 2;
                }
            }
            for ( int found = 0; index + found < length && found < span; ++found) {
                if ( str[index + found] == abcde[found]) {
                    check[found] = ' ';
                }
            }
            index++;
        }
        for ( int found = 0; found < span; ++found) {
            if ( check[found] == abcde[found]) {
                return 0;
            }
        }
        return 1;
    }
    
    int main( void) {
        char lines[][30] = {
            "someaxbxcxdxemm"
            , "someOtherValue"
            , "xyabcdeping"
            , "someaxuxdxlxammabcde"
            , "vu4ndljeibn2c9n@aiendjba"
            , "dbcaeddeaabcceabcde"
        };
    
        for ( int each = 0; each < 6; ++each) {
            printf ( "for [%s] result = %d\n", lines[each], checkabcde ( lines[each]));
        }
        return 0;
    }
    
    #包括
    #包括
    int checkabcde(字符str[]{
    字符abcde[]=“abcde”;
    字符检查[]=“abcde”;
    int指数=0;
    int匹配=1;
    int长度=strlen(str);
    int span=strlen(abcde);
    while(str[index]){
    if(str[index]==abcde[0]){
    匹配=1;
    对于(int-sub=0;sublength | | str[index+sub]!=abcde[sub]){
    匹配=0;
    打破
    }
    }
    如果(匹配){
    返回2;
    }
    }
    对于(int found=0;index+found
    迭代和计数字符?创建字母表长度的计数数组,并增加遇到的每个字符的计数。然后检查