adding strings hard problem...

This is a discussion on adding strings hard problem... within the C Programming forums, part of the General Programming Boards category; sorry if i do some thing wrong posting that code .. anyway .. as have been said before there are ...

  1. #16
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    sorry if i do some thing wrong posting that code ..

    anyway ..
    as have been said before there are more then two ways to aproach this :
    1. to convert the strings to int then do math stuff on it;
    2. to work on strings (like i did);
    3. more options .

    if you prefear to work with numbers and not chars

    [Qoute]
    (int) char_varibale
    [/Qoute]

    but be aware that numbers more then 32K will have to be dobule and not int.
    so you probably will need to some cutting on the string
    look on what i posted and work around that.

    p.s.
    sorry if i misspell something (i don't know how to make my gcc warn my about English syntax errors ).

  2. #17
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    Quote Originally Posted by quzah View Post
    Code:
    signed int x = 'z' - '0';
    if( x < 0 || x > 9 )
        puts( "You didn't enter a digit." );
    That's what error checking is for. You could always just use isdigit.


    Quzah.
    better will be :
    Code:
    if !((x[i] >= '0') && (x[i ]<= '9'))
      printf("sorry mate but you have enterd a non digit value %c\n",x[i]);

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    A typecast won't make a '9' into a 9.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by jabka
    better will be :
    both ways will work.

  5. #20
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by jabka View Post
    better will be :
    Code:
    if !((x[i] >= '0') && (x[i ]<= '9'))
      printf("sorry mate but you have enterd a non digit value &#37;c\n",x[i]);
    Even better would be if your example actually compiled. But we'll pretend it's a typo.


    Quzah.
    Hope is the first step on the road to disappointment.

  6. #21
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    quzah can you comment on post 15? thanks

    edit: nevermind, i think what you mentioned was similar to what was just discussed. wow i am not thinking tonight.
    Last edited by nadroj; 04-22-2007 at 02:35 AM.

  7. #22
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    @nadroj,
    quzah said to do error checking in that manor, not comparing each ascii character in a control then setting a variable to it's corresponding value.

    Doing it the way you mention'd is rather crappy, considering if you had to do upto 255 comparisons.

  8. #23
    Registered User
    Join Date
    Apr 2007
    Posts
    112
    Quote Originally Posted by quzah View Post
    Even better would be if your example actually compiled. But we'll pretend it's a typo.


    Quzah.
    my bad lost the braces before the not ..

    here is the compiled form ..
    Code:
    #include <stdio.h>
    
    int main(){
    	int i=0;
    	char x[10] = {'0','1','2','a','4','5','6','7','8','x'};
    
    	for (i = 0;i < 10; i++)
     	 if (!((x[i] >= '0') && (x[i] <= '9')))
       	   printf("non digit on : %d \n",i);
    
    }
    why Gaos didn't had a wife ?
    http://bsh83.blogspot.com

  9. #24
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    of course you wouldnt have to do 255 comparisons. you compare 0 to 9.

  10. #25
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Who said you wouldn't?

    If someone also wanted to allow the alphabet thats another 26 or 52 , but then they wanted to allow some special chars, its another n chars... It'd become very messy and bloated

  11. #26
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    what we were discussing is how to convert the char to a number. the (poor) way that i was thinking to do it is you compare the character to each character '0' to '9', 10 comparisons. nothing about 255 comparisons or the alphabet. but i already know this is a efficient way to go.

  12. #27
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Yes I am aware of that, but every line of code you write should keep future modification in mind, especially if you work in a group.

    However a look-up table could be considered a crude universal way of solving the problem, one may even call it a 'hack'.

  13. #28
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    initially i said what i was going to do was going to be in a function, 'char2int'. which had that one purpose, so it wouldnt require future modification. but i agree that it is good practice to keep that in mind.

    edit: in the event that mathematicians invent a new number, id be screwed though!
    Last edited by nadroj; 04-22-2007 at 03:01 AM.

  14. #29
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by zacs7 View Post
    Yes I am aware of that, but every line of code you write should keep future modification in mind, especially if you work in a group.

    However a look-up table could be considered a crude universal way of solving the problem, one may even call it a 'hack'.
    If you're going to worry about future modifications, then a lookup table is a lousy way to go, since it would be locale specific.


    Quzah.
    Hope is the first step on the road to disappointment.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Strings and Conversions
    By patso in forum C Programming
    Replies: 8
    Last Post: 04-09-2008, 01:01 PM
  2. problem with strings
    By agentsmith in forum C Programming
    Replies: 5
    Last Post: 04-08-2008, 01:07 PM
  3. Problem with else on strings
    By [Z-D] in forum C++ Programming
    Replies: 6
    Last Post: 10-30-2006, 10:41 PM
  4. array of strings problem
    By dayknight in forum C Programming
    Replies: 3
    Last Post: 11-08-2005, 10:41 AM
  5. Replies: 5
    Last Post: 05-25-2004, 05:36 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21