First of all dont use scanf() to obtain the string from user, use [search]fgets( )[/search] which is much safer option. Something like:
char buffer[BUFSIZ] = { '\0' } ;
printf( "Enter the string: " ) ;
fgets( buffer, BUFSIZ, stdin ) ;
Also there is an inbuilt function in the header file ctype.h known as [search]isdigit( ) [/search] which returns a non zero value if the given character is a digit character.
In your while loop check for digit character, increment the counter, and scale the result in the range 0 - 9 after subtracting the value with 1.
eg.
'0' - 1 = 9And I don't quite understand how you can use 'a-=1' when a is a string
a is a character array but a[i] is a character and you can add or subtract from a charcter since they are basically represented as integers ASCII values.
If any doubts repost.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
Well exactly. I didn't put a[i] in my code, I just put a.
Even I know that, I was just clarifying Mr. Andors point. I don't think I can use isdigit because I'm not supposed to decremenet 0.
What does this mean ?How is '0' - 1 = 9 ?!
If you encounter 0 in your string, how would you handle it.. surely you cant put -1 ?And what if my task was to, say, multiply every digit found by two? How would I do that?
But suppose your digit is greater than 4.. then how do you propose to do it since it would then imply making spaces for new digits ?
State your program requirements in more detail.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
For multiplication:
a[i] = ( a[i] - '0' ) * 2 + '0' ;
But this would work only for digits from 1 to 4.
~s.o.s~
Failure as a human
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734