CMcorpse
CMcorpse

Reputation: 83

How to directly compare a first and second element in an array?

Quick Question, I'm trying to print a new line every time a character from an char array is NOT the subsequent character. An example being if text[i] is 'a' and text[i + 1] is NOT 'b', then printf("\n");

an example I/O would be:

 input: "abk123@XY"
 output: ab
         123
         XY

output right now is:

\n
\n
\n

This is the current code I have now:

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

Main function is:

#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);


 }

Upvotes: 2

Views: 888

Answers (3)

Lym
Lym

Reputation: 326

A slightly more readable version of @minigeek's answer:

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

Upvotes: 0

minigeek
minigeek

Reputation: 3166

Remove else from every condition. Else if is checked only if 'if' fails.but you want next condition to be checked though, change the sequence of checking condition as well.

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

Changes in main

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; 

}

Upvotes: 1

BLUEPIXY
BLUEPIXY

Reputation: 40155

I suggest like this

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

Upvotes: 1

Related Questions