Thread: strstr with integer

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    16

    strstr with integer

    Say I have a user enter an integer from 0 - 255, and I want to search an arbitrary string for all occurrences of the ASCII value of that integer. I've thought about typecasting, itoa, and creating a function where I create a character array with all possible characters, but I keep thinking there has to be an easier way, like using %c in a printf statement.

    The following code doesn't work, but hopefully it will show you my thinking:

    Code:
    do {
    flag = 0;
    printf( "Enter ASCII value( 0-255 ): " );
    scanf( "%d", &value );
       if( value < 0 || value > 255 ) {
    	printf( "Value must be 0 - 255, try again.\n" );
    	flag = 1;
       }
    }while( flag == 1);
    			
    printf( "Searching for character %c. . .\n\n\n", value );
    			
    
    /*  The following is a poor attempt, I know...but I am stuck at the
    integer to character conversion.
    
    int found[size];
    for( i=0; i<size; i++ ) {
       if( strstr( file, (char)value ) != NULL ) {               // file is a file i've read into a string
    	found[i] = 1;
    }
       else{ found[i] = 0; }
    }  */

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    A single character cannot be a string. If you have entered a value as an int, you don't even have any characters to deal with.

    The second half seems to deal with attempting to examine the value of a single character. Surely it is not too difficult to compare this value as being less than some constant?
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    What I need to do is if the user enters the integer 65, I need to search the string named file for the ascii character A. If they enter 77, I search for M, etc... So what I need to know is how to convert the input integer into it's ascii char equivalent.

  4. #4
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Stop thinking of conversion. A character is a number is a character. It's representation. There is no real conversion in that regard.

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And I think strchr() would work better than strstr() - as the former is designed to take a character, whilst the latter is for searching for a STRING.

    --
    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.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    16
    You are right, I should have been using strchr(), but I'm still not quite sure how to pass the integer value to strchr. I know a char is an 8-bit int, but I'm confused as to whether or not I need to create a new unsigned char( capable of storing 0-255) copying values bits to it.


    I'm so confused. . .

    This is what I'm thinking, but it seg faults:

    Code:
    int value, use = 0;
    unsigned char search_for;
    
    /* Code here that prompts the user for an integer from 0-255 */
    
    printf( "Searching for character &#37;c[%d]. . .\n\n\n", value, value );
    			
    search_for = value|use;
    char *hit = strstr( file, search_for );
    printf( "hit: %X\n", hit );
    printf( "search_for: %c\n", search_for );   // Prints the ASCII character associated with the user-entered integer.
    Last edited by k2712; 10-08-2007 at 12:12 PM.

  7. #7
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Your code and posts are just filled with misconceptions about data and its representation. You're making this much more difficult than it should be.

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    I know a char is an 8-bit int, but I'm confused as to whether or not I need to create a new unsigned char( capable of storing 0-255) copying values bits to it.
    Not at all.

    k, here's strchr's prototype:

    char *
    strchr(const char *s, int c);

    and that is all we need to know about strchr to use it. It's first parameter is a string to search for c in, and c, the second parameter, is the character (promoted to an integer anyway) that you want to find. The function will return a pointer to the first search result, or NULL for nothing. Coincidentally, that is all you have to care about to use it correctly, much of the standard library, and indeed, libraries in general are built to work like black boxes.

    But I would pass a character constant to strchr() in your case, such as 'a', since the bitwise work you're doing seems pointless. The result of n | 0 for any n not 0 is n.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    The result of n | 0 for any n not 0 is n.
    I think that holds true for n == 0 too, doesn't 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.

  10. #10
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It may, but in my opinion, 0 | 0 is always false, and that, so far, has been logically harmless for me.
    Last edited by whiteflags; 10-08-2007 at 02:10 PM.

  11. #11
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by k2712 View Post
    What I need to do is if the user enters the integer 65, I need to search the string named file for the ascii character A. If they enter 77, I search for M, etc... So what I need to know is how to convert the input integer into it's ascii char equivalent.
    You have to do nothing at all. Just call strchr(). This is exactly what it's for.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  2. Link List math
    By t014y in forum C Programming
    Replies: 17
    Last Post: 02-20-2009, 06:55 PM
  3. Quick Ques on String Manipulation
    By ckuttruff in forum C Programming
    Replies: 8
    Last Post: 06-22-2008, 09:32 PM
  4. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  5. load gif into program
    By willc0de4food in forum Windows Programming
    Replies: 14
    Last Post: 01-11-2006, 10:43 AM