Hi To All,
I would like to convert a string into its equivalent ascii values, I know how to convert a char into its ascii value but the string has got me all stringed out!!
I would be greatfull if someone could help me with my problem.
thanks
Hi To All,
I would like to convert a string into its equivalent ascii values, I know how to convert a char into its ascii value but the string has got me all stringed out!!
I would be greatfull if someone could help me with my problem.
thanks
A string is just an array of chars. So the algorithm would be. Take the first character of the string, then convert it, do this until the end of the string is reached.
Just wanted to compliment you for giving direction without handing him the code.
What I'm trying to do is convert a string into ascii integer, so as I can add the integer values up, sorry if I was not to clear with my problem.
thanks
Re-read Shiro's post and then come back to this one if you're still stuck (he gave you the answer). Back already? Okay. Have a 'total' variable, initialized to zero. Increment the value of 'total' by the value of the first character in the string:
Now do the same thing for the rest of the characters in the string. This is an ideal place for a for loop.Code:total += my_string[0];
Jason Deckard
er...hope i don't get barked at for showing code...but it would be something on the lines of:
enjoy.Code:#include <stdio.h> #include <string.h> void main() { char x[25]; int y,Total=0; printf("\n\tWord: "); scanf("%s",&x); y=strlen(x); while (y>=0) { Total=Total+x[y]; y--; } printf("\n\tTotal: %i\n\t",Total); }
heh...i'm a beginner programming student too..and i get real bored so i like to throw other peoples code together for ...lame excuse....
Wouldn't the strlen() return a value for the end of line char \0, if so wouldn't you want to start the loop ((y-1) >= 0). I ran across this thread and don't fully understand the strlen() myself.
Thanks very much guys I have now got my little problem sorted.
thanks again
SavesTheDay: I'm sure you've been told this before, but void main is evil. The main function returns an int, nothing else.
void main ( void ) /* Bad, evil, criminal */
int main() /* Better, most common, but still not fully standard */
int main ( void ) /* Correct */
int main ( int argc, char *argv[] ) /* Correct */
If you use void main your program is undefined and can do anything from work properly to launch nuclear missiles and cause the world to errupt into thermonuclear war (provied you have the proper hardware installed).
-Prelude
My best code is written with the delete key.
Are those functions contained in <3rdworldcountries.h>?
Yeah...I get that *about the main function* alot....I'm in my first programming class this semester...and thats how they taught us to do it for some reason.........so i've been doing it...i'll try to stop.
I think you are correct.Originally posted by serious
Wouldn't the strlen() return a value for the end of line char \0, if so wouldn't you want to start the loop ((y-1) >= 0). I ran across this thread and don't fully understand the strlen() myself.