I'm not sure if my title makes sense. My problem is the following: I input a string and the function in my program is supposed to change every digit (except for 0) it finds in the string to a value -1 of that digit (eg 'er345ut' should be changed to 'er234ut'). It should return the number of switches made (that is not a problem) but what's a good way to make the switches?
I did it this way but there MUST be a much better way to do this! I'm a beginner so please don't use any advanced functions...I've heard of atoi, is that what I'm supposed to use? I don't know how to.
#include <stdio.h>
#include <string.h>
int one_less(char *);
main(){
char s[20];
printf("Type string\n");
scanf("%s",&s);
printf("number of changes made is %d\n",one_less(s));
printf("changed string is:\n%s",s);
getchar();
getchar();
}
int one_less(char *a){
int i=0,k=0;
while(a[i]!='\0'){
if (a[i]>='1' && a[i]<='9')
k++;
if (a[i]=='1') a[i]='0';
else if (a[i]=='2') a[i]='1'; /*making the switches the stupidest possible way*/
else if (a[i]=='3') a[i]='2';
else if (a[i]=='4') a[i]='3';
else if (a[i]=='5') a[i]='4';
else if (a[i]=='6') a[i]='5';
else if (a[i]=='7') a[i]='6';
else if (a[i]=='8') a[i]='7';
else if (a[i]=='9') a[i]='8';
i++;
}
return k;
}
This program works, but I think my teacher is gonna make me do 100 push-ups for doing it this way. Please help.