在C中的挑战循环:能不能做到另一种方式? [英] Loops in C challenge: can it be done another way?
问题描述
你好所有的C专家(请不要拍,我不是C程序员了,但时不时我有持久性有机污染物在我脑海里的问题)
Hi all C experts (please don't shoot, I'm no C programmer anymore but from time to time I have a question that pops in my mind)
我在读另一个问题(<一href="http://stackoverflow.com/questions/4331347/how-to-print-an-entered-string-backwards-in-c-using-only-a-for-loop">How只使用一个for循环)向后C到打印输入的字符串。
I was reading another question (How to print an entered string backwards in C using only a for loop).
在简单,最合乎逻辑的答案是
The "simplest" and most logical answer is
for (x = end; x >= 0; --x) {
printf("%c", word[x]);
}
不过,我想知道,如果没有一种方法来达到同样的目标,但保持接近原始循环poseted:
But I was wondering if there wasn't a way to achieve the same goal but staying closer to the original loop poseted:
for (x = word[end]; x >= word[0]; x--) {
printf("%c", x);
}
我不知道够不够C到做出来,但无法通过我们
I don't know enough C to work it out, but couldn't we play with the arrays pointers to loop through
char * wordp;
for(wordp = &word[end]; /*something*/; wordp--){\
printf("%c", &wordp);
}
PS:我真的不关心,如果它是一个向前或向后循环
P.S.: I don't really care if it is a forwards or backwards loop.
PPS:很抱歉,如果我做的指针明显Ç错误;指出这些问题的评论,我会编辑。 ;)
P.P.S.: Sorry if I made obvious C mistakes in the pointers; point them out in the comment and I'll edit them. ;)
杰森
推荐答案
当然可以。
char *wordp;
for(wordp = word + end; wordp >= word; wordp--){
printf("%c", *wordp);
}
这篇关于在C中的挑战循环:能不能做到另一种方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!