我的代码有问题。它不会打印出我的字符串中的偶数字符
Something wrong with my code. It doesn't print out the even characters in my string
我设法编写了一段代码,但它没有给我想要的输出。我已经通过 cs50 的调试器尝试 运行 它,但它显示我的变量 i
的值为 4198496
。我不明白为什么,因为我声明 i
为 0。有人能指出我为什么不打印偶数字符吗?
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(void){
string input = get_string("Say something: ");
int n = strlen(input);
for (int i = 0; i < n; i ++){
if (input[i] % 2 == 0)
printf("%c", input[i]);
}
}
您正在检查字符是否为偶数,而不是它的索引,这是您应该做的 (IIAC):
if (i % 2 == 0) {
printf("%c", input[i]);
}
我设法编写了一段代码,但它没有给我想要的输出。我已经通过 cs50 的调试器尝试 运行 它,但它显示我的变量 i
的值为 4198496
。我不明白为什么,因为我声明 i
为 0。有人能指出我为什么不打印偶数字符吗?
#include <stdio.h>
#include <cs50.h>
#include <string.h>
int main(void){
string input = get_string("Say something: ");
int n = strlen(input);
for (int i = 0; i < n; i ++){
if (input[i] % 2 == 0)
printf("%c", input[i]);
}
}
您正在检查字符是否为偶数,而不是它的索引,这是您应该做的 (IIAC):
if (i % 2 == 0) {
printf("%c", input[i]);
}