C language: Two applets with time complexity of O (1)

Source: Internet
Author: User

C language: Two applets with time complexity of O (1)
Both examples use the same programming philosophy: For character operations, the char type can contain a maximum of 256 characters, but can meet the needs of upper and lower case, number, and other basic characters. Use the ascii code value of the character as the subscript of the array with the size of 256, and fill the content/condition to be searched in the array. In this way, you can view the content of the corresponding underlying object to perform corresponding operations. This is the programming idea that the time complexity is O (1). It is also based on a hash table. You can view the hash table on your own. The first example is as follows: when lowercase letter a is input, uppercase letter Z is output, and lowercase letter B is input, uppercase letter Y is output ,... when the lowercase letter z is input, the uppercase letter A is output.

1 # include <stdio. h> 2 # include <string. h> 3 4 void change (char arr [], int ch) {5 int I = 0; 6 int j = 90; 7 for (I = 97; I <123; I ++) {// Add Z-A 8 arr [I] = j -- one by one in the subscript of a-z in arr [256 --; 9} 10 printf ("% c \ n", arr [ch]); 11} 12 13 int main () {14 char arr [256] = {0 }; 15 int ch = 0; 16 ch = getchar (); 17 change (arr, ch); 18 return 0; 19}

 

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.