473,397 Members | 2,056 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

How to use isdigit function?

Hi,

I am trying to use the isdigit function to extract digits from an array containing a string file. What I am wondering is if the variable used in it can be a char?

this is what I have,

for(k=0;k<10;k++)

{
i = student [k];

if( isdigit(i))

num [j] = i;

j++;

printf("%s",num);

}

For some reason it is printing weird characters repetivaly as if it is not looping through the whole student array.

any help is greatly appreciated.
Dec 3 '10 #1

✓ answered by donbock

Questions #1 and #5 from my earlier reply still apply. (Banfa has reiterated question #5.)

Questions #2 and #3 are addressed by hard-coding the size of the arrays (magic number = 100) and hard-coding the maximum number of characters to look at in each line (magic number = 10). This is a brittle solution.

New issues raised by your new program:
  1. still applies
  2. brittle solution
  3. brittle solution
  4. fixed
  5. still applies
  6. fixed
  7. Initializing i on line 7 serves no purpose. This is not a big deal because it does no harm either.
  8. You don't check if fopen failed on line 15.
  9. You have a tight while loop in lines 18-22 for reading the input file into the student array. The loop is so tight that you discard all but the last line of the file.
  10. j is not initialized when you first use it on line 33. It starts at some totally random value.
  11. Were you to fill num independently for each line of the input file (see #9 above), j is not reset when you start the next line (because you don't initialize it, see #10 above). This means that the digits for all lines in the file would be written sequentially to num. You would almost certainly write past the end of the num array.
  12. It is not uncommon for stdout to be buffered. If it were, then your printf on line 45 would yield no apparent output. Output characters are withheld (buffered) until a newline character is sent to the output. Whether or not stdout is buffered is implementation dependent -- just because it seems to work ok for your compiler doesn't mean the behavior wouldn't be different on another compiler.
  13. You don't check if fgets failed on line 20.

8 4724
donbock
2,426 Expert 2GB
In the future, please use CODE tags around code snippets.

You didn't tell us the types of the variables. I will assume the following; please correct me if I guessed wrong.
Expand|Select|Wrap|Line Numbers
  1. int j, k;
  2. char i;
  3. char *student;
  4. char num[...];
  1. What if the student string doesn't fill the student array? For example, char student[10] = "bill";
  2. How do you know you don't read past the end of the student array?
  3. How do you know the num array is large enough to hold all the digits you extract from student?
  4. You increment j whether the character is a digit or not.
  5. The num string is not null-terminated.
  6. You print the num string after every character in student. Wouldn't you rather wait and print it once after you're done with all the characters in student?
Dec 3 '10 #2
Yes you were right in geussing the types except for student, i made student an array that the file is being read into. Also i apologize for not using the code tags.

I changed it around a bit and this is what I have now:
Expand|Select|Wrap|Line Numbers
  1. int main(int argc, char *argv[])
  2. {
  3.  
  4.            char   student [100];
  5.            char num [100];
  6.            int con [100];
  7.            char i = 0;
  8.  
  9.            int k,m,j,n = 0;
  10.  
  11.  
  12.  
  13.  
  14.  
  15.            FILE * fp = fopen(argv[1],"r");
  16.  
  17.  
  18.     while(!feof(fp))
  19.     {
  20.     fgets(student,100,fp);
  21.  
  22.     }
  23.  
  24.  
  25.  
  26.     for(k=0;k<10;k++)
  27.  
  28.     {
  29.         i = student [k];
  30.  
  31.         if( isdigit(i)){
  32.  
  33.             num [j] = i;
  34.             j++;
  35.         }
  36.  
  37.  
  38.  
  39.  
  40.  
  41.  
  42.  
  43.  
  44.     }
  45.         printf("%s",num);
and it is still printing weird characters, is there something wrong with my arrays?
Thanks,
Dec 3 '10 #3
Banfa
9,065 Expert Mod 8TB
You are treating num as if it is a c style string. However c style strings have a 0 ('\0' or NUL) terminating character and you never put that zero terminator into the array num so it is never really a c style string just an array of char.

And since it is not a c style string use %s in printf shows the wrong results.
Dec 3 '10 #4
donbock
2,426 Expert 2GB
Questions #1 and #5 from my earlier reply still apply. (Banfa has reiterated question #5.)

Questions #2 and #3 are addressed by hard-coding the size of the arrays (magic number = 100) and hard-coding the maximum number of characters to look at in each line (magic number = 10). This is a brittle solution.

New issues raised by your new program:
  1. still applies
  2. brittle solution
  3. brittle solution
  4. fixed
  5. still applies
  6. fixed
  7. Initializing i on line 7 serves no purpose. This is not a big deal because it does no harm either.
  8. You don't check if fopen failed on line 15.
  9. You have a tight while loop in lines 18-22 for reading the input file into the student array. The loop is so tight that you discard all but the last line of the file.
  10. j is not initialized when you first use it on line 33. It starts at some totally random value.
  11. Were you to fill num independently for each line of the input file (see #9 above), j is not reset when you start the next line (because you don't initialize it, see #10 above). This means that the digits for all lines in the file would be written sequentially to num. You would almost certainly write past the end of the num array.
  12. It is not uncommon for stdout to be buffered. If it were, then your printf on line 45 would yield no apparent output. Output characters are withheld (buffered) until a newline character is sent to the output. Whether or not stdout is buffered is implementation dependent -- just because it seems to work ok for your compiler doesn't mean the behavior wouldn't be different on another compiler.
  13. You don't check if fgets failed on line 20.
Dec 3 '10 #5
Would I want to assign the null at the end of the array, something like this.
Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.  
  9.            char student [100];
  10.            char num [100];
  11.            int con [100];
  12.            char i = 0;
  13.  
  14.            int k,m,j,n = 0;
  15.  
  16.  
  17.  
  18.  
  19.  
  20.            FILE * fp = fopen(argv[1],"r");
  21.  
  22.  
  23.     while(!feof(fp))
  24.     {
  25.     fgets(student,100,fp);
  26.  
  27.  
  28.  
  29.  
  30.  
  31.     for(k=0;k<100;k++)
  32.  
  33.     {
  34.  
  35.         i = student [k];
  36.  
  37.         if( isdigit(i)){
  38.  
  39.             num [j] = i;
  40.             j++;
  41.  
  42.         }
  43.  
  44.  
  45.  
  46.  
  47.     }    
  48.  
  49.  
  50.  
  51.  
  52.  
  53.     num[j] = '\0';
  54.  
  55.  
  56.         printf("%s",num);
Dec 4 '10 #6
donbock
2,426 Expert 2GB
That is a fine way to null-terminate the string ... but only if you initialize j before using on line 39 (see issue #10 above).
Dec 4 '10 #7
what should I initialize it as? a pointer?
Dec 4 '10 #8
donbock
2,426 Expert 2GB
I mean the initial value of j -- the value you want j to have the first time you get to line 39.
Dec 4 '10 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: muser | last post by:
I have written a function that checks the first four characters in an address are valid; i.e. 1d2 sour st would prove to be invalid. bool checkdigitsinaddress( const char* string ) { for( int...
4
by: James Gregory | last post by:
I read that the argument to isdigit() can be "an integer whose value is representable as an unsigned char, or the value of the macro EOF.". This seems to say that it should work for values greater...
15
by: Carramba | last post by:
hi! I am trying to confirm if input is digit, so I thought it would by easy to do it with with isdigit() funktion, but how do I pass arrays to it if the imput is more then 1 sign? #include...
2
by: dolbz.cardiff | last post by:
Hi, I would have asked this in the MRTG newsgroups but they all appear to be dead. Sorry if this is the wrong place for the question. I have been looking at the MRTG...
10
by: mdh | last post by:
Could I get some help as to understanding why I am not getting a result I expect. #include <stdio.h> #include <ctype.h> int main (){ int i,j,k,c; i=9;
0
by: origenes | last post by:
Hi, I have this function that works... function _getNoCategories($parent){ $sql = 'SELECT id FROM mod_catalogue_category WHERE parent='.$parent; $cats = $this->db->get_results($sql); ...
5
by: zivon | last post by:
I have a function on one form, and I want when the other form is closed to excute the function on the second form... this is what I'm trying Private Function MainFormIsOpen() ...
2
by: bozo789 | last post by:
Hi Forum, First let me begin by stating I am a student and have read the proper posting procedure concerning students. I am a begginer learning with the Miracle C Work Bench. I know my code is...
5
by: yueying53 | last post by:
Hi people! I'm working with a Telit gsm module and am using PythonWin. I am trying to use the function isdigit( ) to check if a string contains only digits. However, PythonWin doesn't seem to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.