【问题标题】:How to directly compare a first and second element in an array?如何直接比较数组中的第一个和第二个元素?
【发布时间】: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


【解决方案1】:

从每个条件中删除 else。 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; 

}

【讨论】:

  • hmm,这似乎不起作用,此代码的输出是:a \nb \nk \n 1 \n 2 \n 3\n \n X \n Y
  • @CMcorpse 是的,你说你想在每次输入后换行
  • 不只是当一个字符不是后续字符时。
  • @CMcorpse 如果条件改变..现在再次检查
  • 不,那个编辑给了我一个 \n b \n k12 \n 3 \n X \n Y
【解决方案2】:

我建议这样

#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("abk123@XY");
}

【讨论】:

  • @minigeek 你想说什么?
  • 我还没有看到 c boolthanx 中的布尔值......我们可以像在 java 中一样使用它
  • @minigeek bool 可以从 C99 开始使用(在 &lt;stdbool.h&gt; 中)
【解决方案3】:

@minigeek 答案的可读性稍强的版本:

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");
    }
}

【讨论】:

    猜你喜欢
    • 2022-11-16
    • 2020-02-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-14
    • 1970-01-01
    相关资源
    最近更新 更多