Tuesday, July 14, 2009

A simple question on C, "Convert string to integer"???? pls help urgent.?

So i have this project.. where i have to specify a=1,b=2, c=3... etc.. and when i enter a string as an input say "cab" the program should print "312" as the answer... pls give a "C program" Thank you..

A simple question on C, "Convert string to integer"???? pls help urgent.?
Change string to integer











#include %26lt;stdlib.h%26gt;


#include %26lt;stdio.h%26gt;





int main(void)


{


char num[80];





gets(num);


printf("%d", abs( atoi( num ) ) );


return 0;


}





======================================...


There are several methods, the easiest being 'atoi()' the most flexible IMHO (in terms of error checking) being 'strtol()':








/* ATOF.C: This program shows how numbers stored


* as strings can be converted to numeric values


* using the atof, atoi, and atol functions.


*/





#include %26lt;stdlib.h%26gt;


#include %26lt;stdio.h%26gt;





void main( void )


{


char *s; double x; int i; long l;





s = " -2309.12E-15"; /* Test of atof */


x = atof( s );


printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );





s = "7.8912654773d210"; /* Test of atof */


x = atof( s );


printf( "atof test: ASCII string: %s\tfloat: %e\n", s, x );





s = " -9885 pigs"; /* Test of atoi */


i = atoi( s );


printf( "atoi test: ASCII string: %s\t\tinteger: %d\n", s, i );





s = "98854 dollars"; /* Test of atol */


l = atol( s );


printf( "atol test: ASCII string: %s\t\tlong: %ld\n", s, l );


}





Output





atof test: ASCII string: -2309.12E-15 float: -2.309120e-012


atof test: ASCII string: 7.8912654773d210 float: 7.891265e+210


atoi test: ASCII string: -9885 pigs integer: -9885


atol test: ASCII string: 98854 dollars long: 98854





/* STRTOD.C: This program uses strtod to convert a


* string to a double-precision value; strtol to


* convert a string to long integer values; and strtoul


* to convert a string to unsigned long-integer values.


*/





#include %26lt;stdlib.h%26gt;


#include %26lt;stdio.h%26gt;





void main( void )


{


char *string, *stopstring;


double x;


long l;


int base;


unsigned long ul;


string = "3.1415926This stopped it";


x = strtod( string, %26amp;stopstring );


printf( "string = %s\n", string );


printf(" strtod = %f\n", x );


printf(" Stopped scan at: %s\n\n", stopstring );


string = "-10110134932This stopped it";


l = strtol( string, %26amp;stopstring, 10 );


printf( "string = %s", string );


printf(" strtol = %ld", l );


printf(" Stopped scan at: %s", stopstring );


string = "10110134932";


printf( "string = %s\n", string );


/* Convert string using base 2, 4, and 8: */


for( base = 2; base %26lt;= 8; base *= 2 )


{


/* Convert the string: */


ul = strtoul( string, %26amp;stopstring, base );


printf( " strtol = %ld (base %d)\n", ul, base );


printf( " Stopped scan at: %s\n", stopstring );


}


}








Output





string = 3.1415926This stopped it


strtod = 3.141593


Stopped scan at: This stopped it





string = -10110134932This stopped it strtol = -2147483647 Stopped scan at: This stopped itstring = 10110134932


strtol = 45 (base 2)


Stopped scan at: 34932


strtol = 4423 (base 4)


Stopped scan at: 4932


strtol = 2134108 (base 8)


Stopped scan at: 932


======================================...
Reply:giving program is tough but you can use switch case for that.
Reply:you have to put the letters "cab" into a different type of put statement. Try this:


var a:=1


var b:=2


var c:=3





put ""c" "a" "b""





I belive this is how you do it. I haven't worked with C in awhile so you may have to do some slight tweaking. (where I put the letters"var" you may have to change to "int"
Reply:If the numbers of the letters remained fixed and ascending (so a is some number, b is always 1 higher than a, c is always 2 higher than a etc...) Then you can take advantage of the fact that c stores strings as an array of chars and characters are stored as numbers. Also all your characters must either be lower case or upper case.





Here's an example





void print_string(char *string)


{


for(int i = 0; string[i] != '\0'; i++)


{


printf("%d", (int)(string[i] - 'a' + 1); //So a is 1, b is 2, c is 3 etc...


}


printf("\n");


}
Reply:use atoi()





you will have to convert the chars to the correct number before trying to get the value of it.





do you need it to be an int? can't you just use strings that are numbers? ie "1", or "2".
Reply:In C characters and integers are like twins. I would explain like this.





1)Each character is stored in memory as its ASCII Value. For example the ascii value of 'A' is 65 so 65 is stored there.





2) So first I get the string. Since string is an array of characters, I traversing through it.





3)I convert them to upper case because 'a' has a different ascii value than 'A'.





4)Then I subtract the ASCII value of each character from 64 (because A starts from 65).





5)There you have the answer!!





#include%26lt;string.h%26gt;


void main()


{


char name[25];


int i;


clrscr();


printf("Enter the string:");


scanf("%s",name);


for(i=0;name[i]!='\0';i++)


printf("%d",toupper(name[i])-64);


getch();


}
Reply:You can check here. http://zombay.com/forum/convert_string_t...





I think this is the most best answer.





If you have any other problem in programming, you can post in http://zombay.com/forum/programming-b45....





You may get good help. It's free to register.


No comments:

Post a Comment