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?
The demolition criterion in
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.
How can I do that with the zero?
contains exactly four characters. Better be here
But
contains five characters, namely a binary ‘\0’ at the end.
Your source code
This is C. e.g. to see the #include lines for the preprocessor.
You did not apply the formatting ‘Quelltext’.