C language traversal of the character array lookup method (custom header file)

Source: Internet
Author: User

Title: Find the first character in a string that appears only once. If you enter "Abaccdeff\0", Output ' B '. #ifndef  __select_first__#define __select_first__char select_first (char *p,int size); Char select_first1 (char *p, int size); Char select_first2 (char *p, int  size); #endif                                //header file    function declaration # include<stdio.h>//function Body #include<string.h>char select_first (char arr[],int size) {int  i = 0, j = 0;while  (Arr[i]) {if  ((arr[i] != arr[j]&& j< (size-1)) | | I==J) {j + +;} else{   j = 0;i++;} if  (j ==  (size - 1)) return arr[i];} return  ' + ';} Char select_first1 (char *p, int size) {char ch =  ' + '; char arr[20];int  i = 1,j=1;strcpy (arr,  p);while  (arr[0]) {ch = arr[0];i = 1; j = 1;while  (Arr[i]) { if  (Ch == arr[i]) {j + +;} Else{arr[i - j] = arr[i];} i++;} if  (j == 1) return ch;arr[i - j] =  ' \ n ';} return  ' + ';} Char select_first2 (char p[]) {int arr[255] = { 0 };char * ptr  = NULL;ptr = &p[0];while  (*p) {arr[*p]++;p + +;} while  (*ptr) {if  (arr[*ptr] == 1) return *ptr;ptr++;} return  ' + ';} #include <stdio.h>            //main function # include " Select_first.h "Int main () {char arr[] = " Abbaccdeff "; Char ch=select_first2 (Arr,strlen ( ARR));p rintf ("%c\n", ch);}


C language traversal of the character array lookup method (custom header file)

Related Article

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.