Find the first character that appears only once in the string. Typical C language examples

Source: Internet
Author: User

Find the first character that appears only once in the string. Typical C language examples
Original question requirement: Find the first character that appears only once in the string. If "abaccdeff" is input, 'B' is output '. Thinking process: there are many characters in a string, and there are also many characters that appear only once. The simplest way is to record the number of occurrences of each character, and then start from the first character, find the first character that appears only once. Program Implementation: Method 1: when the character array is relatively small, each element is convenient:

/* Question: Find the first character that appears only once in the string. * If "abaccdeff" is input, 'B' is output '. */# Include <stdio. h> # include <stdlib. h> int find_f (char arr [], const int len) // find the function {int I, j, k; int arr1 [20] = {0 }; // defines an array that stores the number of occurrences of each character. for (I = 0; I <len; I ++) // traverses the character array elements, and record the number of occurrences of this character {k = 0; for (j = 0; j <len; j ++) {if (arr [I] = arr [j]) k ++; arr1 [I] = k ;}} for (I = 0; I <len; I ++) // access from the first one, find the first character that appears only once, and return the subscript {if (arr1 [I] = 1) {return I ;}} return len + 1 of the original array; // if there is no separate character, return a number that is not the next mark} int main () {char arr [] = "abaccdeff"; int len = sizeof (arr) /sizeof (arr [0]), c; c = find_f (arr, len); if (c> len) printf ("this number of string groups does not appear only once \ n"); else printf ("the first character in the input string is: % c \ n ", arr [c]); system ("pause"); return 0 ;}

 

Method 2: when the character array is large, record the number of occurrences of each character (use a hash table. Specific Method: When we scan this array for the first time, each character is encountered, find the corresponding item in the hash table and increase the number of occurrences once. In this way, the number of occurrences of each character can be obtained directly from the hash table during the second scan .) Code:
# Include <stdio. h> # include <string. h> # include <stdlib. h> # define tableSize 256 // create a hash table because the ASCII table contains only 0 ~ 255 contains a total of 256 characters. Char First_Char (char * pString) {if (! PString) // return 0; int hashTable [tableSize]; for (int I = 0; I <tableSize; I ++) hashTable [I] = 0; // determine the number of occurrences of each character in the string char * pHashKey = pString; while (* (pHashKey )! = '\ 0') hashTable [* (pHashKey ++)] ++; // find the character pHashKey = pString that appears only once in the string; while (* pHashKey! = '\ 0') {if (hashTable [* pHashKey] = 1) return * pHashKey; pHashKey ++;} // if this string is empty, or each character in the string returns 0 at least twice;} int main (void) {char str [1000]; // This function is recommended when the string is particularly large, therefore, define a 1000 array printf ("Enter the string:"); gets (str); if (First_Char (str) = 0) printf ("the first character that appears only once is not found in the input string! \ N "); else printf (" the first character that appears only once in the input string is: % c \ n ", First_Char (str); system (" pause "); return 0 ;}

 

 

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.