C复制字符,逐个字符 [英] C duplicate character,character by character

查看:216
本文介绍了C复制字符,逐个字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到两个相邻的字符是否是相同的字符。即如果我有这个输入老女人我想打印第二个D副本旁边。这里是我的代码,但我不知道如何我可以做这个编码。这是我的代码:

I am trying to find if two characters following by one another are the same character. ie if i have this input "The oldd woman" I want to print next to second D "duplicate". Here is my code but I can't find out how can I do this coding. Here is my code:

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语句。

I am setting the character to another variable and then checking them with the first if statement. The program should run character by character.

推荐答案

我认为应该尝试并帮助理解变量和指针。

Worth answering to try and help understand variables and pointers I think.

尝试和回答。 。 。尽可能简单。 。 。 注意#1:主要问题/问题是ch和ch2声明为单个char变量。它们可以是'a'或'b'或'\\\
'或0x20或任何其他单个字符。它们不是字符数组或指针。你有一个注释,你读一个char'ch = getchar()//获取文本行',该注释是不正确的(虽然你有很好的意见,显示你在你的例子中思考),反正, ch = getchar()'只是得到一个单一的char。稍后将ch2作为数组。

To try and answer . . . as simply as possible . . . NOTE #1: the main problem/issue is that ch and ch2 are declared as single char variables. They can be 'a' or 'b' or '\n' or 0x20 or any other single char. They are NOT char arrays or pointers. You have a comment where you read one char 'ch = getchar() // getting the line of text', that comment is incorrect (although you do have good comments in showing what you are thinking in your example), anyway, this 'ch = getchar()' just gets a single char. Later you treat ch2 as an array.

char ch,ch2;
. . . then later:
if (ch == ch2[&i]) {   // ouch, does that even compile? yes. oh dear. how do we explain this one?! 

ouch!这是错误的,因为它将ch2视为数组/指针。
你的循环现在工作的方式是ch2设置一次到第一个char读。它永远不会改变。

ouch! This is wrong because it treats ch2 as an array/pointer. The way your loop is working now is ch2 is set once to the very first char read. And it never changes.

它可以编译好,但它会给出警告吗?其实,公平对待你。我没有得到警告。 gcc 4.8.2完全满意ch2是一个char和做(ch == ch2 [& i])。现在,ch2 [& i]可能是语法上有效的代码,它将编译ok。它甚至会运行确定。但是这是什么意思?是语义有效吗?让我们忘记这个奇怪的事情,直到后来。

It may compile okay BUT does it give a warning? Actually, in fairness to you. I do not get a warning. gcc 4.8.2 is perfectly happy with ch2 being a char and doing (ch == ch2[&i]). Now, ch2[&i] may be syntactically valid code, it will compile ok. It will even run ok. BUT what does it mean? Is it semantically valid? Let's forget about this weird thing until later.

注意,你可以有c编译精细,但它可以充满指针错误,可以崩溃/挂起。所以。 。 。请小心: - )。

Note that you can have c compiling fine BUT it can be quite full of pointer errors and can crash/hang. So . . . be careful :-).

尝试进行如下更改:

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。你不使用i。您不使用数组或字符串或字符指针。

This makes the code work just using 2 chars. ch and ch2. You do not use i. You do not use an array or string or char pointer.

注释#1.1:ch2 [& i]编译并运行。但它是错误,OHHHH SOOOOOO错误。奇怪。数组访问如何在c中工作? c [& i]的语法是正确(可能取决于编译器)。但请不要使用此语法!这是什么意思?它在语义上是可疑的。看起来可能意图是使用字符数组与i一起。快速示例显示正确分配和读取字符数组:

NOTE #1.1: ch2[&i] compiles and runs. BUT IT IS WRONG, OHHHH SOOOOOO WRONG. And weird. How does array access work in c? The syntax of c[&i] is "correct" (maybe depends on compiler). BUT please do not use this syntax! What does it mean? It is semantically dubious. Looks like perhaps intent was to use array of chars together with i. Quick example showing assigning and reading from array of chars correctly:

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);

注意#1.2:ch2 [& i]

NOTE #1.2: ch2[&i] compiles and runs. How and why does it compile?

& i表示内存中变量i的指针

&i means the pointer to the variable i in memory

printf中的%p会显示指针值

%p in printf will show pointer value

因此,请尝试将其添加到代码示例中:

So try adding this to code sample:

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]);

我得到类似这样的东西每次运行的指针改变:

I'm getting something like this each run the pointers change:

ch2[&i]:0xbfa0c54c:bfa0de71
ch2[&ch]:0xbfa0c54a:08
ch2[&ch2]:0xbfa0c54b:00

发现的说明

通常我们声明一个数组例如'int array [100];'其中100是数组的大小。 array [0]是第一个元素,数组[99]是最后一个。我们使用整数对数组进行索引。现在,所有数组都是指针。 SO *数组与数组[0]相同。 *(array + 1)与array [1]相同。

Usually we declare an array e.g. 'int array[100];' where 100 is the size of array. array[0] is first element and array[99] is the last. We index the array using integer. Now, all arrays are pointers. SO *array is the same as array[0]. *(array+1) is the same as array[1].

到目前为止很好。

现在*(1 +数组) ]。
我们可以说int i = 7;并使用array [i]或*(array + 7)或*(7 + array)OR i [array]来显示数组的第7个元素。 i [array]给任何程序员应该看起来非常VERY WROOOONG(不是句法错误,但哲学/语义/道德上错了!)

Now *(1+array) is also the same as array[1]. We can say int i=7; And use array[i] or *(array+7) or *(7+array) OR i[array] to show the 7th element of the array. i[array] to any programmer should look very VERY WROOOONG (not syntactically wrong BUT philosophically/semantically/morally wrong!)

精细。冷静。 Jeez。现在用'char ch2;'ch2是一个单一的字符。它不是数组。 ch2 [& i]工作(作为在编译和有时/大多数运行确定!!!),因为最后一个(WROOOONG)i [array]符号是有效的。看看TYPES很有趣:

Okay. Fine. Calm down. Jeez. Now with 'char ch2;' ch2 is a single char. It is NOT an array. ch2[&i] works(works as in compiles and sometimes/mostly runs ok!!!) because the last(WROOOONG) i[array] notation is valid. Looking at TYPES is interesting:

i[array] === <type int>[<type int *>]. 
ch2[&i] === <type char>[<type int *>]. 

C可以将char和int强制转换为int和int可以添加到指针或用作指针。因此,最后结论:语法ch2 [& i]评估为偏移量为& i(指向整数i的指针)PLUS值的整数。没有好的理由使用这种语法!这很危险。你最终访问的内存位置可能有效,也可能不是有效的,而且你的指针是指向单个变量的指针,在引用任何其他值时无效。

C happily and merrily casts char to int and int can be added to pointer or used as pointer. SO FINALLY IN CONCLUSION: the syntax ch2[&i] evaluates to an integer at offset of: &i(pointer to integer i) PLUS value of char ch2. There is no good reason to use this syntax! It's DANGEROUS. You end up accessing a memory location which may or may not be valid and as your pointer is pointer to single variable the location in not valid in reference to any other values.

请参阅:在c'c [& i];'编译,其中c和i是变量(不是数组/指针)。但是语义上是否有效?

注意#2:观看括号{}。而主关闭}和缩进在示例中不匹配。程序函数可以与此错误。 putchar(ch)作为最后一个的一部分运行。

NOTE #2: watch the bracketing {}. while and main closing } and indentation do not match in example. The program functions okay with this error. The putchar(ch) runs as part of the last else. The commands after that run at end of while loop.

注意#3 main应该返回int not void main可选不需要任何东西'或'(int argc,char ** argv)'。

NOTE #3 main should return int not void main optionally takes nothing '()' or '(int argc, char**argv)'.

这篇关于C复制字符,逐个字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆