Thread: how to compare char array with one char?

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    how to compare char array with one char?

    for example:

    char buff[10] = "123456789";
    char buff2[2] = "2";

    how can i compare with the char"2" with the string above?

    cos when i use strcmp(buff[i],buff2); there will be errors.

    please help....thank you !!

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>strcmp(buff,buff2);
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    thank you hammer for helping

    thank you hammer, i am sorry that i should have asked the question more precisely.

    mmmmm here is the situation:

    char[20] = "000000000000000000ham001000000000"
    assuming you dun know where "ham" is....and...not every string contains "ham"(cos the data is from the com port, and the position of "ham" is different every time.)

    what i wanna do is to know where is the ham, and get the "1" out.

    cos i am thinking of if i can know the postion of "h", then i know where is the "1".(assume the one is always two "0"s after "m")

    sorry hammer

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    thank you very much

    thank you so much.

    and i would like to ask is....what will it return?
    would you please explain a bit more?

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    117

    i think i understand it....

    thank you very much!!

    i have found it in the MSDN....

  6. #6
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    I'm sure this will provoke future response if I am wrong, and then we'll both learn.

    strstr takes two strings, the first is the string to search and the second is the string to search for. The return value of strstr is a pointer to the first letter of the first instance of the substring being sought. If the substring isn't found, then p will be NULL. If the substring is found, then, given your assurance that the string will always be "ham001" and not just "ham", the 1 will occur at p + 5. Now, my only question is whether the p + 5 correlates with p[5]. If p were to be interpretted as a string that started at p and extends to the end of buff, then the answer would be that p + 5 is equivalent to p[5]; I'm just not sure of that.

    The real question, though is how do you use the information, now that you know the substring occurs at least once in the search string? Knowing that it exists may be enough, but I suppose you might want to know where p is relative to some spot in the search string. Then, since you know 1 is a p + 5, you will know where 1 is.

    To do that you can subtract the address at the start of the search string, &buff[0] or just buff, from p. Since each char takes up 4 bits of memory you can then determine how far away from the start of the search string the 1 occurs.
    Last edited by elad; 03-28-2003 at 09:33 AM.

  7. #7
    Registered User
    Join Date
    Dec 2001
    Posts
    27

    Comparing single char's

    Hi,

    The program below searches for a single char in a string.

    #include <stdio.h>
    #include <conio.h>
    #include <stdlib.h>
    #include <string.h>

    int main()
    {
    char s[30] = "12345678922543329";

    char a = '2'; // NOTE single quotes
    int x;

    for(x = 0; x < strlen(s); ++x)
    {

    if(s[x] == a) // could use '2' instead of the char a
    {
    printf("\nMatch at %d",x);
    }

    }

    getch();
    return 0;
    }
    Pappy
    You learn something new everyday.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 01:01 AM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  3. Replies: 2
    Last Post: 07-11-2008, 07:39 AM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM