简单的问题是,每次字符数组中的字符不是后续字符时,我都试图打印一个新行例如,如果文本[i]是“a”,而文本[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);
}
最佳答案
从每个条件中删除其他条件Else if仅在“if”失败时被选中。但是,如果希望检查下一个条件,也可以更改检查条件的顺序。
for(i = 0; t[i] != '\0'; i++){
if(t[i] >= '0' && t[i] <= '9' )
printf("%c",t[i]);
if (t[i] >= 'A' && t[i] <= 'Z' )
printf("%c",t[i]);
if(t[i] >= 'a' && t[i] <= 'z')
printf("%c",t[i]);
if(t[i] + 1 != t[i + 1])
printf("\n");
}//end for
主要变化
int main(){
const char t[80] = "abk123@XY";
printf("the original sequence of strings is: %s\n", t);
printf("new string is: \n");
printNext(t);
return 0;
}