C 一个字符一个字符地重复
我试图找出两个紧跟其后的字符是否是同一个字符。也就是说,如果我有这个输入“老女人”,我想在第二个D“复制品”旁边打印。这是我的代码,但我不知道如何才能做到这一点编码。这是我的密码: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
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语句检查它们。程序应该逐字符运行
#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 *>].