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

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Nov 2006
Posts: 6
Reputation: Monsignor is an unknown quantity at this point 
Solved Threads: 0
Monsignor Monsignor is offline Offline
Newbie Poster

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

 
0
  #1
Nov 21st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

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

 
0
  #2
Nov 21st, 2006
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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: Monsignor is an unknown quantity at this point 
Solved Threads: 0
Monsignor Monsignor is offline Offline
Newbie Poster

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

 
0
  #3
Nov 21st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,647
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 473
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #4
Nov 21st, 2006
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

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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: Monsignor is an unknown quantity at this point 
Solved Threads: 0
Monsignor Monsignor is offline Offline
Newbie Poster

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

 
0
  #5
Nov 21st, 2006
Originally Posted by ~s.o.s~ View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,647
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 473
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #6
Nov 21st, 2006
Originally Posted by Monsignor View Post
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.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 6
Reputation: Monsignor is an unknown quantity at this point 
Solved Threads: 0
Monsignor Monsignor is offline Offline
Newbie Poster

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

 
0
  #7
Nov 21st, 2006
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.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,647
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 473
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

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

 
0
  #8
Nov 21st, 2006
For multiplication:
  1. a[i] = ( a[i] - '0' ) * 2 + '0' ;
But this would work only for digits from 1 to 4.
I don't accept change; I don't deserve to live.

Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 275
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

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

 
0
  #9
Nov 22nd, 2006
  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.
If you want to win, you must not loose (Alan Ford)
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 10
Reputation: the_count is an unknown quantity at this point 
Solved Threads: 1
the_count's Avatar
the_count the_count is offline Offline
Newbie Poster

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

 
0
  #10
Nov 22nd, 2006
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. }
The day will soon come.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC