I am a beginner programmer and I'm having trouble with this task. I have to write a function that checks if the digits in the given string are strictly ascending, or better say is each digit larger than the previous one. Function returns true or false, but it also has to be able to return -1 if there are no numbers in the string.
So for the string: "a1b2c3d" the function should return 1,
but for "a1b3c3d" the function should return 0 because they are not strictly ascending.
I cannot use arrays or create new strings.
#include <stdio.h>
#include<ctype.h>
//we have to enter our strings like this
void unesi(char niz[],int velicina)
{
char znak=getchar();
if(znak=='\n')
znak=getchar();
int i=0;
while(i<velicina-1 && znak!='\n')
{
niz[i]=znak;
i ;
znak=getchar();
}
niz[i]='\0';
}
//function checks if the string contains any digits
int cifre(const char *s)
{
int istina=1;
if(isdigit(*s ))
istina=0;
return istina;
}
int srce(const char *s)
{
int broj=1,istina=0;
if(cifre(s)==1)
return -1;
else
{
while(*s!='\0')
{
if(isdigit(*s)==1)
{
if(*s<broj)
{
broj=*s;
istina=1;
s ;
}
}
else
s ;
}
return istina;
}
}
int main() {
char a[100];
unesi(a,100);
printf("%d",srce(a));
return 0;
}
CodePudding user response:
Logic is quite simple.
The function returns -1 if there is no digits 1 if they are sorted in ascending order or zero if not
int isAsc(char *str)
{
int result = -1;
int last = -1;
while(*str)
{
if(isdigit((unsigned char)*str))
{
result = 1;
if(last != -1)
{
if(*str - '0' < last)
{
result = 0;
break;
}
}
last = *str - '0';
}
str ;
}
return result;
}
int main(void)
{
printf("%d\n", isAsc("dsfgdfgdfgfgd"));
printf("%d\n", isAsc("d0sfg1dfg4dfg7fgd"));
printf("%d\n", isAsc("d0sfg1dfg4dfg7f2gd"));
}
CodePudding user response:
//we have to enter our strings like this
It's unclear to me if the whole unesi
function is given as part of the assignment and can't be modified or not, so I won't comment it.
Just read about fgets
, in the future.
I cannot use arrays
Well, I guess that restriction is somehow relaxed:
char a[100];
That's "literally" an array of 100 chars. Hopefully, it will be null-terminated, so that we can consider it a "string".
//function checks if the string contains any digits
That comment is lying, cifre
is only checking the first character, because there is no loop, nor any calling to a function that could traverse a char
array inside it.
Besides, instead of checking beforehand if the string contains any digit, you could start searching for the first digit and then check if the other digits are in strictly ascending order.
In srce
function, we can find
int srce(const char *s)
{
int broj=1, istina=0;
// ^^^^^^ ^^ Note that the starting value of broj is 1.
// [...]
while(*s!='\0')
{
if(isdigit(*s)==1)
{
if( *s < broj )
{ //^^^^^^^^^ Are any of '0', '1', '2'... less than 1?
// Is this supposed to be the path taken when
// the digits are not in STRICTLY ascending order?
// In that case you should check if the current digit
// is less than OR EQUAL to the previous one.
broj=*s;
istina=1; // Shouldn't it be 0 when the order is not correct?
s ; // Shouldn't the traversal of the string just end now?
}
}
else
s ;
}
return istina;
}
Another subtle issue with the previous code is that isdigit
1 returns a non-zero value if the character is a numeric character, not necessarly 1.
(1) About isdigit
call, see also: Why is it OK to pass mismatch argument into isdigit function?