944,004 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Marked Solved
  • Views: 1877
  • C RSS
Nov 21st, 2006
0

How to work with digits when they are characters in a string

Expand Post »
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.

  1. #include <stdio.h>
  2. #include <string.h>
  3. int one_less(char *);
  4. main(){
  5. char s[20];
  6. printf("Type string\n");
  7. scanf("%s",&s);
  8. printf("number of changes made is %d\n",one_less(s));
  9. printf("changed string is:\n%s",s);
  10. getchar();
  11. getchar();
  12. }
  13. int one_less(char *a){
  14. int i=0,k=0;
  15. while(a[i]!='\0'){
  16. if (a[i]>='1' && a[i]<='9')
  17. k++;
  18. if (a[i]=='1') a[i]='0';
  19. else if (a[i]=='2') a[i]='1'; /*making the switches the stupidest possible way*/
  20. else if (a[i]=='3') a[i]='2';
  21. else if (a[i]=='4') a[i]='3';
  22. else if (a[i]=='5') a[i]='4';
  23. else if (a[i]=='6') a[i]='5';
  24. else if (a[i]=='7') a[i]='6';
  25. else if (a[i]=='8') a[i]='7';
  26. else if (a[i]=='9') a[i]='8';
  27. i++;
  28. }
  29. return k;
  30. }

This program works, but I think my teacher is gonna make me do 100 push-ups for doing it this way. Please help.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Monsignor is offline Offline
6 posts
since Nov 2006
Nov 21st, 2006
0

Re: How to work with digits when they are characters in a string

With something like this:
  1. if ((a[i] > '0') && (a[i] <='9'))
  2. {
  3. a[i] -= 1;
  4. }
In the loop.
One remark about your code. You are not obtaining the string the right way. Read this
Last edited by andor; Nov 21st, 2006 at 1:26 pm.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Nov 21st, 2006
0

Re: How to work with digits when they are characters in a string

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:

  1. int one_less(char *a){
  2. int i=0,k=0;
  3. while(a[i]!='\0'){
  4. if ((a[i]>='1') && (a[i]<='9')){
  5. a-=1;
  6. k++;
  7. }
  8. i++;
  9. }
  10. return k;
  11. }

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Monsignor is offline Offline
6 posts
since Nov 2006
Nov 21st, 2006
0

Re: How to work with digits when they are characters in a string

First of all dont use scanf() to obtain the string from user, use [search]fgets( )[/search] which is much safer option. Something like:
  1. char buffer[BUFSIZ] = { '\0' } ;
  2. printf( "Enter the string: " ) ;
  3. 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

Quote ...
And 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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Nov 21st, 2006
0

Re: How to work with digits when they are characters in a string

Click to Expand / Collapse  Quote originally posted by ~s.o.s~ ...
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.
Well exactly. I didn't put a[i] 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?
Last edited by Monsignor; Nov 21st, 2006 at 2:18 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Monsignor is offline Offline
6 posts
since Nov 2006
Nov 21st, 2006
0

Re: How to work with digits when they are characters in a string

Click to Expand / Collapse  Quote originally posted by Monsignor ...
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.

Quote ...
I don't think I can use isdigit because I'm not supposed to decremenet 0.
What does this mean ?

Quote ...
How is '0' - 1 = 9 ?!
If you encounter 0 in your string, how would you handle it.. surely you cant put -1 ?

Quote ...
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.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Nov 21st, 2006
0

Re: How to work with digits when they are characters in a string

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Monsignor is offline Offline
6 posts
since Nov 2006
Nov 21st, 2006
0

Re: How to work with digits when they are characters in a string

For multiplication:
  1. a[i] = ( a[i] - '0' ) * 2 + '0' ;
But this would work only for digits from 1 to 4.
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 720
Failure as a human
~s.o.s~ is offline Offline
8,872 posts
since Jun 2006
Nov 22nd, 2006
0

Re: How to work with digits when they are characters in a string

  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. int one_less(char *);
  5. int main()
  6. {
  7. char s[20];
  8. printf("Type string\n");
  9. scanf("%s", s);
  10. printf("number of changes made is %d\n",one_less(s));
  11. printf("changed string is:\n%s",s);
  12. getchar();
  13. getchar();
  14. return 0;
  15. }
  16.  
  17. int one_less(char *a)
  18. {
  19. int i=0,k=0;
  20. while(a[i]!='\0')
  21. {
  22. if (a[i]>='1' && a[i]<='9')
  23. {
  24. a[i] -= 1;
  25. k++;
  26. }
  27. i++;
  28. }
  29. return k;
  30. }
I don't know but this is working for me. Off course I didn't changed the string obtaining.
Reputation Points: 251
Solved Threads: 29
Posting Whiz in Training
andor is offline Offline
274 posts
since Jun 2005
Nov 22nd, 2006
0

Re: How to work with digits when they are characters in a string

slight modification to your code which gives the same result

  1.  
  2. int one_less(char *a)
  3. {
  4. for (int k=0; *a ; a++)
  5. if (*a >='1' && *a<='9') {(*a)-- ; k ++;}
  6. return k;
  7. }
Reputation Points: 11
Solved Threads: 1
Newbie Poster
the_count is offline Offline
10 posts
since Nov 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: Cant figure this out!
Next Thread in C Forum Timeline: Regular expression matching in a string





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC