A c \ C ++ exam IV every day --- flip a string

Source: Internet
Author: User

This is also a classic C language in the test: Given a string, flip it. For example, abc => when CBA got this question, I didn't want to think about it. Simply put, I used a string tmp to cache this string and then assigned a value for a for loop. The code will be available if you have ideas. [Cpp] # include <stdio. h> # include <stdlib. h> int main () {char string [20], tmp [20]; int length; printf ("please input less than 20 char :"); scanf ("% s", string); printf ("your input string is % s \ n", string); length = strlen (string ); printf ("length is % d \ n", length); for (int I = 0; I <length; ++ I) {tmp [I] = string [I];} for (int I = 0; I <length; ++ I) {string [I] = tmp [length-i-1];} printf ("after revert: % s \ n", s Tring); return 0;} compile with gcc: gcc-o revert revert_string.c-std = c99 and run the command. The result is as expected. However, my algorithm is not elegant, because I use a loop to assign values to two character arrays and then use a loop to flip the array. This efficiency is not flattering. The elegant method should be like this: Find the middle position of the string, and then swap the character on the left with the character on the right. The implementation should be as follows: [cpp] # include <stdio. h> # include <stdlib. h> int main () {char string [20], tmp; int length; printf ("please input less than 20 char:"); scanf ("% s ", string); printf ("your input string is % s \ n", string); // get string length, very useful method for (length = 0; string [length]; length ++); printf ("length is % d \ n", length); // very beateful !!! For (int I = 0; I <length/2; I ++) {tmp = string [I]; printf ("tmp is % c \ n ", string [I]); string [I] = string [length-i-1]; printf ("string [% d] is % c \ n", I, string [length-i-1]); string [length-i-1] = tmp; printf ("string [% d] is % c \ n", length-i-1, tmp);} printf ("after revert: % s \ n ", string); return 0;} running effect: [plain] D: \ workspace \ C \ revert_string> gcc-o revert revert_string.c-std = c99 D: \ workspace \ C \ revert_string> rev Ert please input less than 20 char: abc your input string is abc length is 3 tmp is a string [0] is c string [2] is a after revert: CBA D: \ workspace \ C \ revert_string> revert please input less than 20 char: abcd your input string is abcd length is 4 tmp is a string [0] is d string [3] is a tmp is B string [1] is c string [2] is B after revert: algorithms such as dcba are much more efficient than before. As long as you use your brains, the world will become more elegant.

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.