zwei aufeinanderfolgende zahlen in einem char-array richtig programmieren?

#include <stdio.h>

#include <stdlib.h>

int istZiffer(char zeichen){

if(‘0′<=zeichen && zeichen <=’9’){

  return 1;

}

return 0;

}

int ziffernHintereinander(char text[]){

  int temp=0;

  for(int i=0;i!=’\0′;i++){

    if(istZiffer(text[i])==1 && istZiffer(text[i+1])==1){

      temp=1;

      return temp;

    }

  }

  return temp;

}

int main(){

  char t[]={‘a’,’b’,’1′,’1′};

  printf(“%d” ,ziffernHintereinander(t));

  

}

was habe ich hier falsch gemacht?

(1 votes)
Loading...

Similar Posts

Subscribe
Notify of
6 Answers
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
tunik123
1 year ago

The demolition criterion in

for(int i=0;i!='\0';i++) 

is wrong. ‘\0’ is the same as 0, so the loop is never going through.

Self text[i] != ‘\0’ would not work. Once, because your text is not completed with ‘\0’ and also because you are on text[i+1] to access. That would be behind the ‘0’.

It would best if the calling program gives the length of the text as an argument. Alternatively you could complete the text with ‘\0’ and the length with strlen(text) to determine.

Why the variable temperature I am also unclear.

Addendum:

There. isCiffer(text[i]) for ‘\0’ always returns 0 isCiffer(text[i+1]) not called. That’s good, but you don’t do that anyway.

tunik123
1 year ago
Reply to  BeJe16
char t[]={'a','b','1','1'};

contains exactly four characters. Better be here

static const char t[] = {'a','b','1','1'};

But

static const char t2[] = "ab11";

contains five characters, namely a binary ‘\0’ at the end.


geri3d
1 year ago

Your source code

#include 

#include 

int istZiffer(char zeichen){

if('0'<=zeichen && zeichen <='9'){

  return 1;

}

return 0;

}

int ziffernHintereinander(char text[]){

  int temp=0;

  for(int i=0;i!='\0';i++){

    if(istZiffer(text[i])==1 && istZiffer(text[i+1])==1){

      temp=1;

      return temp;

    }

  }

  return temp;

}

int main(){

  char t[]={'a','b','1','1'};

  printf("%d" ,ziffernHintereinander(t));

  

}
Ist das Java++ oder stamme ich als Diosaurier aus einer anderen Zeit?
wunschname0302
1 year ago
Reply to  geri3d

This is C. e.g. to see the #include lines for the preprocessor.

wunschname0302
1 year ago

what have I done wrong here?

You did not apply the formatting ‘Quelltext’.