【发布时间】:2023-03-29 09:03:01
【问题描述】:
快速问题,每次 char 数组中的字符不是后续字符时,我都会尝试打印一个新行。一个例子是如果 text[i] 是 'a' 而 text[i + 1] 不是 'b',那么 printf("\n");
I/O 示例如下:
input: "abk123@XY"
output: ab
123
XY
现在的输出是:
\n
\n
\n
这是我现在拥有的当前代码:
void printNext(const char *t){
//variable declerations
int i;
for(i = 0; t[i] != '\0'; i++){
if(t[i] != t[i + 1])//line in question, which isn't working
printf("\n");
else if(t[i] >= '0' && t[i] <= '9')
printf("%c",t[i]);
else if (t[i] >= 'A' && t[i] <= 'Z' )
printf("%c",t[i]);
else if(t[i] >= 'a' && t[i] <= 'z')
printf("%c",t[i]);
}//end for
}//end printNext
主要功能是:
#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void printNext(const char *);
int main(void){
const char t[40] = "abk123@XY";
printf("the original sequence of strings is: %s\n", text);
printf("new string is: \n");
printNext(t);
}
【问题讨论】:
-
您实际上并没有提出问题。如果您的程序不起作用,请说明确切的输入、预期输出和实际输出。另外,请提供minimal reproducible example。
-
把if语句改成if(t[i] + 1 != t[i+1])怎么样?
-
@minigeek 实际输出或具体问题在哪里?我们所拥有的只是“不工作”。
-
@CMcorpse 检查解决方案 :)
-
每次数组中的一个字符不是后续字符是什么意思?在您的问题中,您尝试 only 插入
\n字符,那么k和@去了哪里?您的问题的确切要求是什么,因为您作为输入提供的 none 字符是下一个字符,因此您应该获得与 input 一样多的\n字符。
标签: c arrays if-statement for-loop