DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   C (http://www.daniweb.com/forums/forum118.html)
-   -   Need help to convert a string to an integer (http://www.daniweb.com/forums/thread142285.html)

c_ytsui Aug 25th, 2008 7:29 pm
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;
}

Ancient Dragon Aug 25th, 2008 7:39 pm
Re: Need help to convert a string to an integer
 
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);

Aia Aug 25th, 2008 8:00 pm
Re: Need help to convert a string to an integer
 
>I use the atoi function to do the conversion
Take a look at strtol(), it would be better, atoi() has poor error handling

jephthah Aug 26th, 2008 7:03 pm
Re: Need help to convert a string to an integer
 
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 Aug 26th, 2008 7:35 pm
Re: Need help to convert a string to an integer
 
bah.

extra credit if you fix my bug

:P

Aia Aug 26th, 2008 10:04 pm
Re: Need help to convert a string to an integer
 
Quote:

Originally Posted by jephthah (Post 678695)
[...]strtok... very powerful function[...]

Almost as powerful as
exit()
;)

Quote:

Originally Posted by jephthah (Post 678709)
bah.

extra credit if you fix my bug

:P

For which one?

jephthah Aug 26th, 2008 10:32 pm
Re: Need help to convert a string to an integer
 
meanie :(




lol :D


All times are GMT -4. The time now is 7:05 am.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC