| | |
Need help to convert a string to an integer
![]() |
•
•
Join Date: Aug 2008
Posts: 1
Reputation:
Solved Threads: 0
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;
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;
c Syntax (Toggle Plain Text)
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; }
Last edited by Ancient Dragon; Aug 25th, 2008 at 7:35 pm. Reason: add code tags
you can remove the comma by shifting all other characters left one place, assuming the string is in read/write memory.
C Syntax (Toggle Plain Text)
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);
Last edited by Ancient Dragon; Aug 25th, 2008 at 7:41 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
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.
.
the function "stripCharacters" will strip out any undesired characters using the standard library function strtok... very powerful function, learn to use it.
c Syntax (Toggle Plain Text)
#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; }
.
Last edited by jephthah; Aug 26th, 2008 at 7:26 pm.
![]() |
Similar Threads
- how to convert a string to integer (HTML and CSS)
- to convert string to integer value (Legacy and Other Languages)
- String to integer conversion (C)
- Convert string to to HEX (C)
- Convert a string to an integer (PHP)
- Converting String to Integer help (C++)
- String to integer to ascii (C)
Other Threads in the C Forum
- Previous Thread: Making War card game
- Next Thread: C Beginner: Adding Two Polynomials of degree n
| Thread Tools | Search this Thread |
* adobe ansi api array asterisks binarysearch calculate centimeter changingto char character cm convert copyimagefile cprogramme creafecopyofanytypeoffileinc createcopyoffile csyntax database directory feet fflush fgets file fork forloop frequency function getlasterror givemetehcodez grade graphics gtkgcurlcompiling gtkwinlinux hacking highest histogram i/o inches input intmain() iso kernel keyboard km linked linkedlist linux linuxsegmentationfault list locate logical_drives looping loopinsideloop. lowest match microsoft mqqueue mysql number oddnumber odf opendocumentformat owf pattern pdf performance posix probleminc process program programming radix recv recvblocked repetition research reversing scanf scheduling segmentationfault send sequential socket socketprograming stack standard string systemcall threads turboc unix user variable voidmain() wab whythiscodecausesegmentationfault windows.h windowsapi







