| | |
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:
Solved Threads: 0
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.
This program works, but I think my teacher is gonna make me do 100 push-ups for doing it this way. Please help.
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.
c Syntax (Toggle Plain Text)
#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.
With something like this:
In the loop.
One remark about your code. You are not obtaining the string the right way. Read this
c Syntax (Toggle Plain Text)
if ((a[i] > '0') && (a[i] <='9')) { a[i] -= 1; }
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)
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
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:
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.
c Syntax (Toggle Plain Text)
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:
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
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.
c Syntax (Toggle Plain Text)
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
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
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
•
•
Join Date: Nov 2006
Posts: 6
Reputation:
Solved Threads: 0
•
•
•
•
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.
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.
Even I know that, I was just clarifying Mr. Andors point.
What does this mean ?
If you encounter 0 in your string, how would you handle it.. surely you cant put -1 ?
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 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?
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
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
For multiplication:
But this would work only for digits from 1 to 4.
c Syntax (Toggle Plain Text)
a[i] = ( a[i] - '0' ) * 2 + '0' ;
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
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
c Syntax (Toggle Plain Text)
#include <stdio.h> #include <string.h> int one_less(char *); int 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(); 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; }
If you want to win, you must not loose (Alan Ford)
slight modification to your code which gives the same result
c Syntax (Toggle Plain Text)
int one_less(char *a) { for (int k=0; *a ; a++) if (*a >='1' && *a<='9') {(*a)-- ; k ++;} return k; }
The day will soon come.
![]() |
Similar Threads
- connecting characters (C)
- Counting specific characters in a string (C)
- Removing characters from a string (C)
- Initializing an array of strings and printing it. (C++)
Other Threads in the C Forum
- Previous Thread: Cant figure this out!
- Next Thread: Adding two binary numbers and find over flow bits
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays asterisks binarysearch calculate centimeter char command convert copyimagefile copypdffile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax directory drawing dynamic executable fflush file fork forloop frequency getlasterror givemetehcodez graphics gtkgcurlcompiling hacking hardware highest homework i/o inches incrementoperators infiniteloop kernel km lazy linked linkedlist linux linuxsegmentationfault list lists locate logical_drives match matrix microsoft motherboard multi mysql number open opendocumentformat opensource owf pattern pdf performance pointer pointers posix problem probleminc program programming radix recursion recv repetition research scanf scheduling scripting segmentationfault send sequential shape socketprograming spoonfeeding stack standard string strings structures student systemcall testautomation turboc unix user variable voidmain() wab windows.h






