Charred_Phoenix
01-21-2003, 01:52 AM
Is there any way I might place an integer in a string, not so the numbers become their ASCII equivalents but as if I was printf'ing to the character array with a cast including a %d?
|
Click to See Complete Forum and Search --> : integer -> character Charred_Phoenix 01-21-2003, 01:52 AM Is there any way I might place an integer in a string, not so the numbers become their ASCII equivalents but as if I was printf'ing to the character array with a cast including a %d? Mr_B 01-21-2003, 02:18 AM yeah, I'm pretty sure you can. But when the integer is entered into the array...a space will be placed before and after the integer. So make sure that array is at least 3 spaces.. for a single digit integer phlipant 01-21-2003, 02:22 AM your probably looking for sprintf e.g. sprintf(string."i have %d gallons of gas\n",ngallons); Charred_Phoenix 01-21-2003, 02:50 AM Thanks! Is there anyway i can reverse this, that is, after manipulating the string (like adding 3 digits onto the end or something) set the integer to this new number value. Spawn913 01-21-2003, 02:59 AM man strtol phlipant 01-21-2003, 03:04 AM if you would like to change a number in a string (at will) it is best to break the problem up into substrings, unless you are keen on counting array addresses. try somthing like sprintf(string1,"i have "); sprintf(string2,"%d",ngallons); sprintf(string3," gallons of gas\n"); strcpy(string,string1); strcat(string,string2); strcat(string,string3); if you would like to modify the number, then do sprintf(string2,"%d",ngallons); strcpy(string,string1); strcat(string,string2); strcat(string,string3); Charred_Phoenix 01-21-2003, 03:24 AM No, I meant so I could move it back into an integer variable after editing it in its string for like: sprintf(stringy, "%d", theinteger); strcat(stringy, 333); theinterger = stringy; or at least the working equivalent =P. Could you explain the usage of strtol, cos I saw the man page and I just know its scheming against me..... :| Also while i'm at it, how can I go through a character array 1 character at a time, i thought it was getc(chararray) but that causes an error -_-'. Thanks! And when i'm a master coder i'll look after you ;) phlipant 01-21-2003, 03:28 AM sprintf(stringy, "%d", theinteger); theinterger = atoi(stringy); or maybe you meant sprintf(stringy, "%d", theinteger); strcat(stringy, "333"); theinterger = atoi(stringy); Charred_Phoenix 01-21-2003, 03:48 AM Bleh, sorry 'bout the quotation marks. Hmm according to the man page atoi is the same as strtol, but without the errors. Thanks! Now someone just has to answer my question stated above as Also while i'm at it, how can I go through a character array 1 character at a time, i thought it was getc(chararray) but that causes an error -_-'. and i will become supreme ruler of the universe, BWAHAHAHHAHAHAA! :| Charred_Phoenix 01-21-2003, 03:53 AM Bah! scratch that, atoi converts to int strtol converts to long int -_-' phlipant 01-21-2003, 03:58 AM a for loop would work, as in int i; for(i=0;i<strlen(stringy);i++) { printf("%c",stringy[i]); } printf("\n"); phlipant 01-21-2003, 04:00 AM Bah! scratch that, atoi converts to int strtol converts to long int -_-' atol converts to long int Charred_Phoenix 01-21-2003, 04:08 AM I know, but so does strtol, and it has error functions! :P Thanks Charred_Phoenix 01-21-2003, 08:25 PM Why doesn't this function work? :( int anynum(char number[255]) { char c; int howmany; long ib; ib = 1; while(c != 'f') { c = number[ib]; if(c == '2') { howmany++; } ib++; } return howmany; } It always returns 0, the usage is anynum("<string of numbers>f"); Spawn913 01-21-2003, 10:04 PM 1) a note about atoi() and strtol()... atoi converts directly to decimal, but has the disadvantage of not returning error codes if the conversion fails. strtol has more parameters, since you can convert to different bases, and returns error codes. when tracking bugs, it *would* be easier if you check for errors in all (well, almost all) function calls anyway, just my 2 cents. 2) with regards to your function, I'm not sure why it's returning 0 (I believe it should be some random value). You forgot to initialize the value of howmany such that your howmany++ line wouldn't know what it's value should be. I guess putting howmany=0 before your while() loop would fix it. by the way, you should also initialize c . Charred_Phoenix 01-21-2003, 11:54 PM I think it's just not initializing howmany cos what the code inside the file section is doing is setting a value, not manipulating it, so wouldn't it be the same thing if I set a base value'? Thanks Charred_Phoenix 01-21-2003, 11:59 PM Nevermind, you were right, without this it counts 1 less, sorry. :D All hail Spawn, king of C! o.o Charred_Phoenix 01-22-2003, 12:13 AM It still returns one less than it should :( truls 01-22-2003, 10:28 AM You function counts the number of 2's in the string. This works fine when you initialize all your variables. Can you give me the array you are sending to anynum() please? Charred_Phoenix 01-22-2003, 06:45 PM well if i send "222f" it counts 2, but if I send " 222f" it counts 3 :| bwkaz 01-22-2003, 07:16 PM Heh. That's because arrays (strings are just character arrays) are indexed starting at 0, not 1. You're starting at 1. Which means you aren't counting the first character. I didn't even see this until you posted the strings, either... But initialize ib to 0 instead of 1. Charred_Phoenix 01-22-2003, 08:17 PM Hmmmm.... i think that's what i was doing at first but *shrugs* truls 01-23-2003, 10:18 AM bwkaz: I missed that too, how embarrasing :p (Of course all the strings I used to test it with started with ANYTHING BUT 2, now ain't that typical)... Charred_Phoenix 01-23-2003, 05:43 PM *blames the government* justlinux.com
Copyright Internet.com Inc. All Rights Reserved. |