Ask Experts Questions for FREE Help !
Ask
    mikko3000's Avatar
    mikko3000 Posts: 1, Reputation: 1
    New Member
     
    #1

    May 24, 2006, 07:34 AM
    C-problem
    Please help me.

    Program has the following definition:

    char string[] = "1234";

    How do I make a program which converts numbers given in a string to integer without using the "atoi" -function?

    My greatest problem is how to compare a single character in a string.
    For example

    if (string[0]=="1") // this naturally is wrong

    Thanks in advance.

    Mikko, Finland
    LTheobald's Avatar
    LTheobald Posts: 1,051, Reputation: 127
    Ultra Member
     
    #2

    May 24, 2006, 09:28 AM
    Maybe this (rather nice looking) site will help. I've not touched C so this site will be more help than me :P

    Eddie's Basic Guide To C Programming

    In particular:
    C - atoi, itoa, sprintf and sscanf
    C - Copying Strings: strcpy, strncpy and strcmp - Using strcpy to Copy Strings, Using strncpy to Copy Strings, Using strcmp to Compare Strings
    ansahahmed's Avatar
    ansahahmed Posts: 3, Reputation: 2
    New Member
     
    #3

    Jul 20, 2006, 02:00 AM
    Quote Originally Posted by mikko3000
    Please help me.

    Program has the following definition:

    char string[] = "1234";

    How do i make a program which converts numbers given in a string to integer without using the "atoi" -function?

    My greatest problem is how to compare a single character in a string.
    For example

    if (string[0]=="1") // this naturally is wrong

    Thanks in advance.

    Mikko, Finland

    hi mikko,

    In C we can't compare two strings using relational operators.But using strcmp() we can compare two strings. Instead of using if (string[0]=="1") you just use if(string[0]='1') to compate two chracters.you can convert a character in to number by subtracting character 'zero' or integer 48.




    for(I=0;string[i]!='\0';i++)
    {
    int j=string[i]-'0';

    num=num*10+j;

    }
    ansahahmed's Avatar
    ansahahmed Posts: 3, Reputation: 2
    New Member
     
    #4

    Jul 20, 2006, 02:02 AM
    hi mikko,

    In C we can't compare two strings using relational operators.But using strcmp() we can compare two strings. Instead of using if (string[0]=="1") you just use if(string[0]='1') to compate two chracters.you can convert a character in to number by subtracting character 'zero' or integer 48.




    for(I=0;string[i]!='\0';i++)
    {
    int j=string[i]-'0';

    num=num*10+j;

    }
    sarjeet's Avatar
    sarjeet Posts: 1, Reputation: 1
    New Member
     
    #5

    Jul 16, 2008, 03:50 AM
    Hhhhhhhhhhhhhh

Not your question? Ask your question View similar questions

 

Question Tools Search this Question
Search this Question:

Advanced Search

Add your answer here.


Check out some similar questions!

Playboy girlfirend problem.. Real problem [ 14 Answers ]

I have this new girlfriend.. that I really, really like (more than any other). She seems really nice... she was wearing everything of the "playboy" (earrings, bag, shirt, etc, etc) brand name / merchandise. I thought it was cute and a one time thing. Until I saw her bedroom.. It was 100%...


View more questions Search