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