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;
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.

## All 9 Replies

With something like this:

``````if ((a[i] > '0') && (a[i] <='9'))
{
a[i] -= 1;
}``````

In the loop.
One remark about your code. You are not obtaining the string the right way. Read this

Doesn't work now. When I type a string with no numbers it does what it's supposed to but when I include a digit it just does nothing (the black window stays open but nothing appears). Here's the code of the function now:

``````int one_less(char *a){
int i=0,k=0;
while(a[i]!='\0'){
if ((a[i]>='1') && (a[i]<='9')){
a-=1;
k++;
}
i++;
}
return k;
}``````

What's wrong with it now? And I don't quite understand how you can use 'a-=1' when a is a string. ::shrug::

As for scanf...well, I've been told it's not the best choice, but I'm incredibly thick-headed and intend to use it untill it back-fires.

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 = 9

And I don't quite understand how you can use 'a-=1' when a is a string

a is a character array but a 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.

a is a character array but a is a character and you can add or subtract from a charcter since they are basically represented as integers ASCII values.

Well exactly. I didn't put a in my code, I just put a. Works OK now. I don't think I can use isdigit because I'm not supposed to decremenet 0.

How is '0' - 1 = 9 ?!

And what if my task was to, say, multiply every digit found by two? How would I do that?

Well exactly. I didn't put a 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.

You didn't read my first post carefully. The program is supposed to decrement digits 1 through 9, but if it comes across a 0 it's supposed to leave it as it is.

For multiplication:

``a[i] = ( a[i] - '0' ) * 2 + '0' ;``

But this would work only for digits from 1 to 4.

``````#include <stdio.h>
#include <string.h>

int one_less(char *);
int main()
{
char s;
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();
return 0;
}

int one_less(char *a)
{
int i=0,k=0;
while(a[i]!='\0')
{
if (a[i]>='1' && a[i]<='9')
{
a[i] -= 1;
k++;
}
i++;
}
return k;
}``````

I don't know but this is working for me. Off course I didn't changed the string obtaining.

slight modification to your code which gives the same result

``````int one_less(char *a)
{
for (int k=0; *a ; a++)
if (*a >='1' && *a<='9') {(*a)-- ; k ++;}
return k;
}``````
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.