character in string

This is a discussion on character in string within the C Programming forums, part of the General Programming Boards category; is there a function that determins the position of a certain character in a character array?...

  1. #1
    Registered User canine's Avatar
    Join Date
    Sep 2001
    Posts
    125

    character in string

    is there a function that determins the position of a certain character in a character array?
    In a perfect world every dog would have a home and every home would have a dog.
    Visit My Web Site, Canine Programming
    I use Win32 API

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789

    Re: character in string

    Originally posted by canine
    is there a function that determins the position of a certain character in a character array?
    I seem to remember strchr()....

    Code:
     
    char str[] = "Hello World";
    char *ptrChar = strchr(str,'o');
    printf("%s",ptrChar);
    This gives a pointer to the first 'o' in the phrase
    Last edited by Fordy; 01-23-2002 at 05:49 PM.

  3. #3
    Registered User canine's Avatar
    Join Date
    Sep 2001
    Posts
    125
    i need to know the actual position though

    .................................................. .
    char str[]="abcdefg";
    int pos;
    pos = ?????(str,'e');



    <pos = 4>
    In a perfect world every dog would have a home and every home would have a dog.
    Visit My Web Site, Canine Programming
    I use Win32 API

  4. #4
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Okay, so you have a pointer to the first occourance of the element in the string...
    And you have a pointer to beginning of the string...
    And you want the distance to that element from the beginning of the string?
    Subtract.
    Callou collei we'll code the way
    Of prime numbers and pings!

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,789
    Originally posted by canine
    i need to know the actual position though

    .................................................. .
    char str[]="abcdefg";
    int pos;
    pos = ?????(str,'e');



    <pos = 4>
    Code:
    char str[] = "Hello World";
    int x,pos = 0;
    for(x = 0; x < strlen(str);x++){
    if(str[x] == 'o'){
    pos = x;
    break;}
    }
    printf("o was found at position %d",pos);
    return 0;

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    34
    Code:
    #include <stdio.h> 
    
    char FindChar(char *p, char c, int *i);
    
    int main()
    {
    	char MyString[31] = "this is my string";
    	char ch = 'm';
    	int location = 0;
    
    	if (FindChar(MyString, ch, &location))
    		printf("found %c at %d", ch, location);
    	else
    		printf ("character not found");
    
    	return 0;
    }
    
    char FindChar(char *p, char c, int *i)
    {
           while (*p)
           {
                 if (*p == c)
                       break;
    
                 (*i)++;
                 p++;
          }
    	
          return *p;    // return the NULL if search character not found
    }
    One restriction with this solution: you can't search for a NULL since that is the character that signals the caller that we didn't find the search character.

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,824
    > char str[]="abcdefg";
    > int pos;
    > pos = ?????(str,'e');

    Would be
    Code:
    char str[]="abcdefg"; 
    int pos; 
    char *p = strchr( str, 'e' );
    if ( p != NULL ) {
        pos = p - str;
    } else {
        pos = -1;  // something not found
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  2. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 10:37 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  4. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  5. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM

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