Find the first occurrence of a character in a string

Source: Internet
Author: User

Topic:

finds the first occurrence of a character in a string. If you enter "Abaccdeff", Output ' B '.

Topic Analysis:

For the first occurrence of the character in the string, contact the ASCII code table, you can get 256 different characters, you can use the character encoding, each character encoding as the following table of the array, create an array of 256 elements arr[256], Each element in the array corresponds to the number of statistical characters, traversing from the first character of the string, encountering a character to the corresponding array element plus 1, when one of the elements in the array changes to 2, the output of this element corresponds to the array of the following table, that is, the ASCII code of this character.


    below for the specific program:

#define  _CRT_SECURE_NO_WARNINGS 1#include <stdio.h> #include  <stdlib.h> #include  <string.h> char find (char *p)   {      if (p  == null)       {           return  ';      }         ' Int arr1[256];      int i=0;      for (i= 0;i<256;i++)      //Initialize array     {           arr1[i] = 0;      }       char *q=p;      while (*q)! = ' + ')        {          arr1[*q]++;           q++;      }      q=p;       while (*q!= ')       {           if (arr1[*q]==1)           {               return *q;           }           q++;      }      return  ';  '  }  int main ()   {      char arr[256];       printf ("Please enter:");       gets (arr);       printf ("find:   %2c", Find (arr));      system ("Pause") ; &NBSP;&NBSP;&NBSP;&NBSP;RETURN&Nbsp;0;  } 

This article from "Unintentional persistent" blog, reproduced please contact the author!

Find the first occurrence of a character in a string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.