Thread: How to character in specified string !!

  1. #1
    Unregistered
    Guest

    Exclamation How to character in specified string !!

    Hello,
    How to find the location of first occurance & last occurance of a specified character in a given string !!

    Thanks in advance !!

    Regards,
    Hari

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Code:
    #include<stdio.h>
    #include<string.h>
    
    int main()
    {
      char str[] = "Hello";
      printf("First %c and last %c ocurrances.\n", str[0], str[ strlen(str) - 1] );
    
      return 0;
    
    }

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    Or if that wasn't quite what you wanted than use 'strchr' standard library function to find a character in a string.

    STRCHR(3) Linux Programmer's Manual STRCHR(3)

    NAME
    strchr, strrchr - locate character in string

    SYNOPSIS
    #include <string.h>

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

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

    DESCRIPTION
    The strchr() function returns a pointer to the first occurrence of the
    character c in the string s.

    The strrchr() function returns a pointer to the last occurrence of the
    character c in the string s.

    Here "character" means "byte" - these functions do not work with wide or
    multi-byte characters.

    RETURN VALUE
    The strchr() and strrchr() functions return a pointer to the matched char-
    acter or NULL if the character is not found.

    CONFORMING TO
    SVID 3, POSIX, BSD 4.3, ISO 9899

  4. #4
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    Code:
    int find_firstlast(char *str,char chr,int *first,int *last){
        char *pstr = str;
        int len = strlen(str);
       *first = -1; *last = -1; /*invalid */
        /*find first*/
        while(*pstr){
            if(*pstr == chr) {
                 *first = (int)(pstr - str);
                 break;
            }
            pstr++;
        } 
        if(*first == -1)
             return -1; /*First not found!... abort...*/
        /*find last*/
        pstr = str + len;
        while(pstr>=str){ /*there WAS a missing line... don't mind the next post :)  */
            if(*pstr == chr) {
                 *first = (int)(pstr - str);
                 break;
            }
            pstr--;
        } 
        if(*last == -1)
           return -2;/*last occurance not found ... abort*/
        return 0;/*succes ! both are found*/
    }
    int main(void){
        char test[] = "This is a test string";
        int ret = 0;
        int first,last;
        if((ret=find_firstlast(test,'i',&first,&last)) < 0) {
            switch (ret) {
        case -1:
           printf("First not found\n");
           break;
        case -2:
           printf("Last not found\n");
           break;
        default:
           printf("Unknown Error\n");
            }
        }
        else {
          printf("'%c' found at %d and %d locations\n",
               'i',first,last);
        }
        return 0;
     
    }
    Last edited by borko_b; 07-17-2002 at 09:30 AM.

  5. #5
    Banned borko_b's Avatar
    Join Date
    Jun 2002
    Location
    Well... I live in Bulgaria :)
    Posts
    100
    OOPS!!

    Code:
    ...
    while(pstr!=) {
    ...
    should be:
    Code:
    while(pstr >= str) {

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, 01:02 AM
  2. Remove character from string
    By nooksooncau in forum C Programming
    Replies: 11
    Last Post: 06-05-2006, 09:37 AM
  3. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 09: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