Converting String to Int in C: Issues with atoi() and isdigit() | Code Solution

  • Thread starter TheSourceCode
  • Start date
In summary, the conversation is discussing the process of converting a string of numbers into an int array using the functions atoi() and isdigit(). The speaker suggests using a debugger to track variable changes and using pointers to call atoi() on a string starting at the next number. They also mention the importance of ensuring that the characters in the string have ASCII codes within the range of 30h to 39h.
  • #1
TheSourceCode
14
0
I need to take the values from a string , such as {23 45 67\0}, and store them into an int array. I need to do this with atoi() and isdigit() but I'm having some issues. Here is what I have.

Code:
for(i=0; i<=n; i++){
   num[i]=atoi(str);
   for(j=0; (isdigit(str[j])!=0); j++){
      for(k=0; k<n; k++)
         str[k]=str[k+1];
         n-=1;
   }
   for(j=0; j<n; j++)
      str[k]=str[k+1];
   n-=1;
}
 
Physics news on Phys.org
  • #2
Have you tried to follow what your code does with a debugger? Watching how the variables change is the best way of understanding what is happening.

I would not copy str content, instead, I would call atoi on a string starting at the place next number starts. Something like atoi(str+index_of_the_character_next_number_starts). I assume you know how pointers and tables work in C.
 
  • #3
I wasn't able to figure out how to track variables in a debugger. However, the due date was extended so I will sit down this weekend and try to figure it out. Also, I'll have another chance to talk to the prof tomorrow. We've not done anything with pointers so I'm not sure if that's an option or not; I'll ask. Thanks for the input.
 
  • #4
TheSourceCode said:
I need to take the values from a string , such as {23 45 67\0}, and store them into an int array. I need to do this with atoi() and isdigit() but I'm having some issues.
In C, a string is an array of type char. Are the numbers you show the ASCII codes of the characters in the string? For example, in the string "CAB" the actual values in memory are 67 65 66 0. If all you need to do is print the ASCII values of the characters, just loop through the string to get each character, but use printf to display it as an int.
 
  • #5
A string which contains "123" in memory would look like this:
31h
32h
33h

this because the the ASCII correspondents of numbers are 30h for 0 up to 39h for 9.

so if the string has "asdasd" this cannot be converted to int using atoi(). The string should only contain characters with ASCII code varying from 30h to 39h.

If you want to convert characters to int, then simply do this.if we have
str (char array), num (int array), n = strlen(str) then do this
for(int i=0;i<n;i++) {
num = str;
}
 
1.

How do I convert a string to an integer in C?

To convert a string to an integer in C, you can use the atoi function. This function takes a string as an argument and returns the corresponding integer value.

2.

What is the difference between atoi and strtol in C?

atoi and strtol are both functions used to convert a string to an integer in C. The main difference is that atoi does not perform any error checking, while strtol allows for more control over error handling and the ability to convert numbers in different bases.

3.

Can I convert a string to an integer in C without using a library function?

Yes, you can write your own function to convert a string to an integer without using a library function. This will involve iterating through the characters in the string and performing arithmetic operations to convert them to their corresponding integer values.

4.

What happens if the string contains non-numeric characters when using atoi in C?

If the string passed to atoi contains non-numeric characters, the function will return the integer value of any leading digits in the string. Any non-numeric characters after the leading digits will be ignored.

5.

Is there a limit to the size of the integer that can be converted from a string using atoi in C?

Yes, the maximum value that can be converted using atoi is INT_MAX, which is defined in the limits.h header file. If the string passed to atoi represents a number larger than INT_MAX, the function will return an undefined value.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
942
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
10
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
12
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Programming and Computer Science
2
Replies
55
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
940
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Programming and Computer Science
Replies
1
Views
872
Back
Top