2017-03-14 53 views
2

快速问题,我试图每次从字符数组中的字符不是随后的字符时打印一个新行。例如,如果text [i]是'a'而text [i + 1]不是'b',那么printf(“\ n”);如何直接比较数组中的第一个和第二个元素?

示例I/O将是:

input: "[email protected]" 
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] = "[email protected]"; 

    printf("the original sequence of strings is: %s\n", text); 
    printf("new string is: \n"); 
    printNext(t); 


} 
+2

你居然没问一个问题。如果你的程序不能正常工作,请说明确切的输入,预期输出和实际输出。另外,请提供[mcve]。 – kaylum

+1

如何将if语句更改为if(t [i] + 1!= t [i + 1])? – Shiping

+0

@minigeek实际输出或具体问题在哪里?我们所有的是“不工作”。 – kaylum

回答

1

从各种情况中删除其他。否则,如果仅在“如果”失败时被选中,但您希望通过检查下一个条件,则还要更改检查条件的顺序。

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] = "[email protected]"; 
    printf("the original sequence of strings is: %s\n", t); 
    printf("new string is: \n"); 
    printNext(t);  
    return 0; 

} 
+0

嗯,这似乎不工作,这个代码的输出是:a \ nb \ nk \ n 1 \ n 2 \ n 3 \ n \ n X \ n Y – CMcorpse

+0

@CMcorpse是啊你说你想要换行每输入一次后 – minigeek

+0

不只是当一个字符不是后面的字符。 – CMcorpse

1

我建议这样的@ minigeek的回答

#include <stdio.h> 
#include <ctype.h> 
#include <stdbool.h> 

bool sameKindAndSeq(unsigned char a, unsigned char b){ 
    if(!a || !b || a + 1 != b) 
     return false; 
    if(islower(a)) 
     return islower(b); 
    if(isupper(a)) 
     return isupper(b); 
    if(isdigit(a)) 
     return isdigit(b); 
    return false; 
} 

void printNext(const char *t){ 
    bool first = true;//flag of top of sequence of same kind 
    for(; *t; ++t){ 
     if(first){ 
      if(sameKindAndSeq(*t, t[1])){ 
       putchar(*t); 
       first = false; 
      } 
     } else { 
      if(sameKindAndSeq(t[-1], *t)){ 
       putchar(*t); 
      } 
      if(!sameKindAndSeq(*t, t[1])){ 
       putchar('\n'); 
       first = true; 
      } 
     } 
    } 
} 

int main(void){ 
    printNext("[email protected]"); 
} 
+0

[DEMO](http://ideone.com/tQGZUv) – BLUEPIXY

+0

布尔值也存在于c! – minigeek

+0

@minigeek你想说什么? – BLUEPIXY

0

稍微更可读的版本:

void printNext(const char *t) 
{ 
    int i; 
    int isdigit(int); 
    int isupper(int); 
    int islower(int); 

    for(i = 0; t[i] != '\0'; i++){ 
     if (isdigit(t[i])) 
      printf("%c",t[i]); 
     if (isupper(t[i])) 
      printf("%c",t[i]); 
     if(islower(t[i])) 
      printf("%c",t[i]); 
     if(t[i] + 1 != t[i + 1]) 
      printf("\n"); 
    } 
} 
相关问题