954,500 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Need help to convert a string to an integer

Hi all,

I am new to C programming and need some of your help. I am trying to convert a string(ex. 25,000) to an integer. I use the atoi function to do the conversion, however, it only reads the initial portion of the string by stopping at the first non-numerical character. In my case, it stores the value of "25" rather than "25000". How do I remove the comma and convert it to an integer? Please advise, thanks in advance.

Carol

int result;

extract_value(char* param_name)
{
	char param[256], paramvalue[2048], *valueptr, valuestr[64];
	int result;

	sprintf(param, "{%s}", param_name);	
	strcpy(paramvalue, lr_eval_string(param));

	if((valueptr = (char*)strstr(paramvalue, ">")) != NULL)
		{
		strcpy(valuestr, &valueptr[1]);
		result = atoi(valuestr);
		}
	lr_output_message("Param: %s, Value: %d", param_name, result);

	return result;
}
c_ytsui
Newbie Poster
1 post since Aug 2008
Reputation Points: 10
Solved Threads: 0
 

you can remove the comma by shifting all other characters left one place, assuming the string is in read/write memory.

char str[] = "25,000";
// locate the comma
char* ptr = strchr(str,',');
// shift remaining characters left 1 place
memmove(ptr, ptr+1, strlen(ptr+1) + 1);
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

>I use the atoi function to do the conversion
Take a look at strtol(), it would be better, atoi() has poor error handling

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

check this out. compile it and run it and see how it works.

the function "stripCharacters" will strip out any undesired characters using the standard library function strtok... very powerful function, learn to use it.

#include <stdio.h>
#include <string.h>
#define MAXLEN    32

//////////////////////////////////////////////////////
//
// function : stripCharacters
// 
// input  : character string to be modified
// output : the modified string (original is lost!!)
// return : number of chars stripped, or -1 if error
//
//////////////////////////////////////////////////////
int stripCharacters(char * user_str)
{
   char copy_str[MAXLEN], *ptr;
   const char CHARS_TO_STRIP[10] = ",$%#";
   
   //error check, make sure string is not too long
   int stringlen = strlen(user_str);
   if (stringlen > MAXLEN)
       return -1;

   //make copy of user string, modified version will replace it
   strncpy (copy_str,user_str,MAXLEN);
   
   //look for first instance of "strip characters"
   ptr = strtok(copy_str, CHARS_TO_STRIP);
   strcpy(user_str,ptr);

   //look for additional strip characters
   while(ptr)
   {
      ptr = strtok(NULL, CHARS_TO_STRIP);
      if (ptr)
         strcat(user_str,ptr);
   }
   
   //modified string is passed back in original string variable
   //return value indicates number of chars stripped, if any
   return (stringlen - strlen(user_str));
}



int main()
{
   char myString[32];
   int retVal;

   strcpy(myString,"1,234,567,890,123");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);

   strcpy(myString,"$5,299.99");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);

   strcpy(myString,"0.039%");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);

   strcpy(myString,"#103");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);

   strcpy(myString,"999888777");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);

   strcpy(myString,"123,456,789,012,345,678,901,234,567,890");
   printf("original:  %s\n",myString);
   retVal = stripCharacters(myString);
   printf("modified:  %s   (stripped %i)\n\n",myString,retVal);


   return 0;
}

.

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

bah.

extra credit if you fix my bug

:P

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 
[...]strtok... very powerful function[...]


Almost as powerful as exit() ;)bah.

extra credit if you fix my bug

:P For which one?

Aia
Nearly a Posting Maven
2,392 posts since Dec 2006
Reputation Points: 2,224
Solved Threads: 218
 

meanie :(


lol :D

jephthah
Posting Maven
2,587 posts since Feb 2008
Reputation Points: 2,143
Solved Threads: 179
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You