Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/c/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C 一个字符一个字符地重复_C_Char_Compare - Fatal编程技术网

C 一个字符一个字符地重复

C 一个字符一个字符地重复,c,char,compare,C,Char,Compare,我试图找出两个紧跟其后的字符是否是同一个字符。也就是说,如果我有这个输入“老女人”,我想在第二个D“复制品”旁边打印。这是我的代码,但我不知道如何才能做到这一点编码。这是我的密码: void main() { char ch,ch2; ch = getchar(); // getting the line of text int i = 1; ch2 = ch; while (ch != '\n') // the loop will close if n

我试图找出两个紧跟其后的字符是否是同一个字符。也就是说,如果我有这个输入“老女人”,我想在第二个D“复制品”旁边打印。这是我的代码,但我不知道如何才能做到这一点编码。这是我的密码:

void main()
{
    char ch,ch2;
    ch = getchar(); // getting the line of text
    int i = 1;
    ch2 = ch;

    while (ch != '\n') // the loop will close if nothing entered
    {
        if (ch == ch2[&i]) {
            printf("%c-duplicate", ch);
        }
        i++;
        if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { // checking for uppaercase vowel
            printf("%c-upper case vowel", ch);
        }
        else if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { // checking for lowecase vowel
            printf("%c-lower case vowel", ch);
        }
        else if (ispunct(ch)) {          // checking for punctuation
            printf("%c-punctuation", ch);
        }
        else
            putchar(ch);

            printf("\n");
            ch = getchar();
        }
        printf("\n");
    }
}
我将字符设置为另一个变量,然后用第一个if语句检查它们。程序应该逐字符运行

  • 根据其他建议:更改ch==ch2[&i]这在这里没有意义
  • 因为在循环之前设置了ch=ch2,所以 if(ch==ch2) (修复后)将始终在第一次计算时为true
  • 你的else非常混乱,如果你有不止一行的代码,你必须放在括号里
  • 请记住,当您输入您的输入时,您实际上是在提交两个字符,例如(“e”和“\n”),因为您在键入字符后按enter键,这很重要
  • 再努力一点,意思是写一条错误信息,写下你尝试解决问题的结果。这对我们和你都有帮助。看起来有点像是你写的这篇文章,只是想马上修复一下。编程变得越来越难,如果你不能解决问题(带着建议),那么伤害会更大,但这不是必须的
  • 要快速获得肮脏的概念证明,请添加另一个ch=getchar();紧跟在你的另一个下面。请注意,下面的代码应该运行,但还没有完全执行您想要的操作,您需要进行一些进一步的调试

    编辑2016年10月19日

    修复了你们指出的char2问题

    将ch2=ch移动到获取新字符的行上方

    #include <stdio.h>
    #include <ctype.h>
    
    int main(){
        char ch,ch2;
        ch = getchar(); // getting the line of text
        int i = 1;
        ch2 = 0;
    
        while (ch != '\n') // the loop will close if nothing entered
        {
            if (ch == ch2) {
                printf("%c-duplicate", ch);
            }
            i++;
            if (ch == 'A' || ch == 'E' || ch == 'I' || ch == 'O' || ch == 'U') { // checking for uppaercase vowel
                printf("%c-upper case vowel", ch);
            }
            else if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u') { // checking for lowecase vowel
                printf("%c-lower case vowel", ch);
            }
            else if (ispunct(ch)) {          // checking for punctuation
                printf("%c-punctuation", ch);
            }
    
            printf("\n");
            ch2 = ch;
            ch = getchar();
            ch = getchar();
    
        }
    
    
        printf("\n");
        return 0;
    }
    
    #包括
    #包括
    int main(){
    炭ch,ch2;
    ch=getchar();//获取文本行
    int i=1;
    ch2=0;
    while(ch!='\n')//如果没有输入任何内容,循环将关闭
    {
    if(ch==ch2){
    printf(“%c-重复”,ch);
    }
    i++;
    如果(ch='A'| | ch='E'| | ch='I'| | ch='O'| | ch='U'){//检查上置元音
    printf(“%c-大写元音”,ch);
    }
    如果(ch='a'| | ch='e'| | ch='i'| | ch='o'| | ch='u'){//检查小写元音
    printf(“%c-小写元音”,ch);
    }
    else if(ispunct(ch)){//检查标点符号
    printf(“%c-标点符号”,ch);
    }
    printf(“\n”);
    ch2=ch;
    ch=getchar();
    ch=getchar();
    }
    printf(“\n”);
    返回0;
    }
    
    下面是一个例子,我相信这正是您想要的
    c
    是正在读取的当前字符(注意:它是类型
    int
    ),而
    c1
    是先前读取的字符(初始化为
    -1
    ,以确保它与第一次测试不匹配)

    虽然您可以比较
    A
    E
    …,但字符串库提供了
    strchr
    ,可以轻松测试较大字符串中是否包含单个字符

    与其为每个
    replicate
    元音
    等调用
    printf
    ,不如使用
    sprintf
    构建一个包含适用于任何一个字符的所有条件的字符串。这样,您只需在每次迭代结束时调用一次
    printf
    (取决于您是打印全部还是仅打印符合条件的内容)
    s
    用作保存每个字符匹配信息的缓冲区,
    offset
    只是先前写入
    s
    的字符数。(您应该检查以确保您没有超过
    s
    中可用的字符数(但此处不需要)

    不清楚您是要打印输入字符串中的每个字符,还是只打印到目前为止已匹配的字符。下面的代码仅输出与其中一个条件匹配的字符(如果未给出参数)。如果希望查看所有字符,请将任何值作为第一个参数传递给程序

    #include <stdio.h>
    #include <string.h>
    #include <ctype.h>
    
    #define MAXL 32
    
    int main (int argc, char **argv) {
    
        char *uvowel = "AEIOU", *lvowel = "aeiou";
        int c, c1 = -1;
    
        while ((c = getchar()) != '\n' && c != EOF) {
            char s[MAXL] = "";              /* description buffer */
            int offset = 0;                 /* offset       */
            if (c == c1)                    /* new == old   */
                offset = sprintf (s, " duplicate");
            if (strchr (uvowel, c))         /* upper-case?  */
                sprintf (s + offset, " upper-case vowel");
            else if (strchr (lvowel, c))    /* lower-case?  */
                sprintf (s + offset, " lower-case vowel");
            else if (ispunct (c))           /* punctuation? */
                sprintf (s + offset, " punctuation");
            else if (argc > 1)              /* if printing all */
                sprintf (s + offset, " other");
            if (*s)                         /* print c and s   */
                printf (" %c - %s\n", c, s);
            c1 = c;                         /* save last char read */
        }
    
        return 0;
    }
    
    将任何值作为第一个参数传递以打印所有字符:

    $ echo "The oldd woman" | ./bin/classdup 1
     T -  other
     h -  other
     e -  lower-case vowel
       -  other
     o -  lower-case vowel
     l -  other
     d -  other
     d -  duplicate other
       -  other
     w -  other
     o -  lower-case vowel
     m -  other
     a -  lower-case vowel
     n -  other
    
    重复元音

    $ echo "womaan" | ./bin/classdup
     o -  lower-case vowel
     a -  lower-case vowel
     a -  duplicate lower-case vowel
    
    仔细检查一下,如果你有任何问题,让我知道。有很多方法可以做到这一点,这似乎是一个接近你的意图


    (注意:您需要通过
    -Wno unused parameter
    编译器选项来消除关于
    argv
    未使用的警告,或者只是在代码中的某个地方做一个存根测试,例如
    if(argv[1]){}

    值得回答,以帮助理解我认为的变量和指针

    要尝试并回答…尽可能简单…注意#1:主要问题/问题是ch和ch2被声明为单字符变量。它们可以是“a”或“b”或“\n”或0x20或任何其他单字符。它们不是字符数组或指针。您有一个注释,其中您读取了一个字符“ch=getchar()//获取文本行',该注释是不正确的(尽管您在示例中有很好的注释来显示您的想法),无论如何,这个'ch=getchar()'只获取一个字符。稍后,您将ch2视为一个数组

    char ch,ch2;
    . . . then later:
    if (ch == ch2[&i]) {   // ouch, does that even compile? yes. oh dear. how do we explain this one?! 
    
    哎哟!这是错误的,因为它将ch2视为数组/指针。 现在循环的工作方式是将ch2设置为第一次读取字符,并且它永远不会改变

    它可能可以编译,但它会发出警告吗?事实上,公平地说,我没有收到警告。gcc 4.8.2非常乐意将ch2作为一个字符并执行(ch==ch2[&I])。现在,ch2[&I]可能是语法上有效的代码,它可以编译。它甚至可以运行。但这意味着什么?它在语义上有效吗?让我们稍后再考虑这个奇怪的事情

    请注意,您可以使用c编译,但它可能充满指针错误,并且可能会崩溃/挂起。因此…请小心:-)

    尝试进行如下更改:

    ch = getchar(); // does not get the line of text, just gets one char
    . . . 
    ch2 = 0; // for the first iteration of loop
    if (ch == ch2) { // change from ch2[&i] - wow :-) very confused!
    . . . 
        ch2 = ch; // set ch2 to the last char read before reading new
        ch = getchar(); // read one new char
    
    这使得代码只需使用2个字符即可工作。ch和ch2。你不知道
    ch = getchar(); // does not get the line of text, just gets one char
    . . . 
    ch2 = 0; // for the first iteration of loop
    if (ch == ch2) { // change from ch2[&i] - wow :-) very confused!
    . . . 
        ch2 = ch; // set ch2 to the last char read before reading new
        ch = getchar(); // read one new char
    
    char s[100]; // string of 100 chars, accessing index below 0 and above 99 is bad
    i=0;
    s[i]='H';    // assign char 'H' to first letter of string (s[0])
    i++;         // increment i, i is now 2.
    s[i]='i';
    i++;
    s[i]=0; // end string
    printf("s[0]=%c s[1]=%c s[2]=%02x string:%s",s[0],s[1],s[2],s);
    
    printf("%p %p %p\n", &ch, &ch2, &i);
    
    // ch2[i] will not compile for me, as ch2 is not an array. syntax error
    
    // ch2[&i] DOES compile for me. But . . what is the value ? 
    // What does it mean. I do not know! Uncharted territory.
    printf("ch2[&i]:%p:%02x\n",&i,ch2[&i]);
    printf("ch2[&ch]:%p:%02x\n",&ch,ch2[&ch]);
    printf("ch2[&ch2]:%p:%02x\n",&ch2,ch2[&ch2]);
    
    ch2[&i]:0xbfa0c54c:bfa0de71
    ch2[&ch]:0xbfa0c54a:08
    ch2[&ch2]:0xbfa0c54b:00
    
    i[array] === <type int>[<type int *>]. 
    ch2[&i] === <type char>[<type int *>].