Thread: Finding the ASCII value of a string?

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    28

    Finding the ASCII value of a string?

    If I define a string as
    Code:
    char found[100];
    and then proceed to try
    Code:
    if ((int) found[7]==80))
    {
     printf ("%s",found);
    }
    It should only occur if the seventh char in found is P, whose ascii value is 80, correct? Is there an easier way to do this?

  2. #2
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Code:
    if(found[6] == 'P')
    {
      printf("%s\n", found);
    }
    Since 0 is included you would put [6] not [7] if you want to find the 7th letter

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    Thanks, that fixed the problem. I'm new to strings, so I should've checked that first...

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by Nicheter View Post
    It should only occur if the seventh char in found is P, whose ascii value is 80, correct? Is there an easier way to do this?
    The ASCII code for 'P' is 80, but there is no guarantee that the system's character set is actually ASCII. It is better to compare explicitly with 'P' not just for clarity but because you do not necessarily know the character set. This of course assumes that the compiler is using the same character set as the platform, which is usually true.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    Ah, well, that's a good tip, thanks. One other question, while I'm on the subject of strings. Is there any way to do multiple string comparisons in a shorter form than, for example,
    Code:
    if((strcmp(str1,"apple")==0)||(strcmp(str1,"bananna")==0)||(strcmp(str1,"fruit")==0)||...
    and so on.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    You could have an array of strings, e.g.
    Code:
    const char *fruits[]= { "apple", "banana", "pear", ... } ;
    and then loop through that array to see if you find it.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  7. #7
    Registered User
    Join Date
    Apr 2007
    Location
    Sydney, Australia
    Posts
    217
    Well you don't need so much "(" and ")"

    Code:
    if(strcmp(str1,"apple")==0 || strcmp(str1,"bananna")==0 || strcmp(str1,"fruit")==0)
    Last edited by 39ster; 07-16-2008 at 10:08 AM. Reason: Added "need"

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Code:
    int bFound = 0;
    bFound = (strcmp(str1,"apple")==0);
    if(!bFound)
        bFound = (strcmp(str1,"bananna")==0);
    if(!bFound)
        bFound = (strcmp(str1,"fruit")==0);
    ...
    for example
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  9. #9
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    Thanks guys, but Vart, isn't your solution even longer then the initial example?

  10. #10
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by Nicheter View Post
    Thanks guys, but Vart, isn't your solution even longer then the initial example?
    Strings are short - so IMHO code is more readable...
    It takes alittle more lines... so what? Put this code into another function, add loop and array as suggested by matsp... but do not write lines longer than the screen width
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  11. #11
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    matsp's suggestion is probably best, especially if you have over, say, five strings. It's pretty easy to do, too.
    Code:
    /*! Returns true if \a needle is a fruit.
        \param a The string that might represent a fruit.
        \return True (nonzero) if \a needle is a fruit.
    */
    int match(const char *needle) {
        const char *haystack[] = {
            "apple", "banana", "pear", /* ... */
        }
        size_t x;
    
        for(x = 0; x < sizeof(haystack) / sizeof(*haystack); x ++) {
            if(!strcmp(needle, haystack[x])) return 1;
        }
    
        return 0;
    }
    Plus, if you get a lot of strings, you can sort them and use a binary search instead of a linear search quite easily.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  12. #12
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    Well, thanks! One final, unrelated, question:
    if i use my main statement as
    Code:
    int main(int argc, char *argv[])
    it allows me to take command line arguments, and works fine. But if, using windows, I try to drop a file onto the program so as to make that the second argument, it does not work properly? Well, that is to say, it executes with no errors, but the outputted file is nowhere to be found...

  13. #13
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Nicheter View Post
    Well, that is to say, it executes with no errors, but the outputted file is nowhere to be found...
    Nowhere to be found, or "not where you expect it"?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  14. #14
    Registered User
    Join Date
    Dec 2007
    Posts
    28
    it outputs a file 'reformatted.txt' and a search of the entirety of the computer turns up nothing. After checking the program's execution one more time, it does seem to be executing properly, just the file isn't appearing where it should/at all? I thought it may have been someplace else, but apparently this is not the case.

  15. #15
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Nicheter View Post
    it outputs a file 'reformatted.txt' and a search of the entirety of the computer turns up nothing. After checking the program's execution one more time, it does seem to be executing properly, just the file isn't appearing where it should/at all?
    Can you post the code?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Lame null append cause buffer to crash
    By cmoo in forum C Programming
    Replies: 8
    Last Post: 12-29-2008, 03:27 AM
  3. string to ASCII and back...
    By skora in forum C Programming
    Replies: 3
    Last Post: 11-23-2003, 10:59 PM
  4. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM
  5. converting a string to it equivalent ascii value
    By pauljhot in forum C Programming
    Replies: 12
    Last Post: 02-16-2002, 02:35 AM