Some interview questions about strings

Source: Internet
Author: User
The most common examples of computer test and interview are string operations. String processing is the most common problem for programmers in their daily work. it can reflect the basic skills of programmers. Google written test: the minimum successor to the encoding implementation for a given string (all in lowercase English letters), ZTE: the encoding implementation string shifts n places to the right, xinmail: string inversion ......

The most common examples of computer test and interview are string operations. String processing is the most common problem for programmers in their daily work. it can reflect the basic skills of programmers. Next, I will share with you some questions about string processing in various written examinations and interviews over the past month:

1. google test: the minimum successor of a given string (all lowercase English letters) is obtained by Coding. for example, the minimum successor of "abc" is "abd ", the minimum successor of "dhz" is "di ".

Idea: The question is relatively simple. for the last character + 1, if it is greater than 'z', it is for the first character + 1. if it is greater than 'z', repeat the previous step. Therefore, when writing code, we only need to add 1 to each character from the back to the back of the string loop until the occurrence of + 1 does not exceed 'Z. If the first character of the exit loop is greater than 'z', the system prompts that it does not exist. otherwise, set the last character of the exit loop to '\ 0.

int MinNextStr(const char* src,char* &minnext){int srclen=strlen(src);minnext=(char*)malloc((srclen+1)*sizeof(char));if(minnext==NULL){return -1;}strcpy(minnext,src);int i=srclen-1;while(i>=0){minnext[i]++;if(minnext[i]<='z'){break;}i--;}if(i<0){return 0;}else{minnext[++i]='\0';return 1;}}

If you change the given string to lowercase English letters, you only need to change if (minnext [I] <= 'Z ') change to if (minnext [I] <= 'Z' & minnext [I]> 'A' | minnext [I] <= 'Z ')

Note that minnext [I] <= 'Z' & minnext [I]> 'A' is used to prevent minnext [I] from being capitalized, because the uppercase English letter (: 65) it is smaller than the ASCII code in lower case (a: 97). if minnext [I]> 'A' is not added, minnext [I] is ('Z' + 1, ASCII code: 91) will also jump out of the loop.

2. ZTE: encode a string to shift n places to the right, for example, "diopHeg" to "egdiopH"

Train of Thought 1: Save the Last n characters to be moved, move all the remaining ones to the next n positions, and then fill the last n characters that have been saved to the previous n positions. Note: if n exceeds the length of the string, the length of the n mod string must be obtained first.

Int RightMoveStr (char * src, int n) {int len = strlen (src); int mov = n % len; char * rstr = (char *) malloc (mov + 1) * sizeof (char); if (rstr = NULL) {return 0;} int I = 0; while (I
 
  
= 0) {src [I + mov] = src [I]; I --;} I = 0; while (I
          
   

Train of Thought 2: use the string library function to simplify Coding. Use a new string ss to save the result. First, move the source string pointer s to the first character position of the substring to be shifted right, for example, if "diopHeg" shifts two places to the right, it moves s to point to 'E', then strcpy (ss, s), assigns the position indicated by s to '\ 0', and finally strcat (ss, s ).

Int RightMove (char * src, char * & ssrc, int n) {int len = strlen (src); ssrc = (char *) malloc (sizeof (char) * (len + 1); n = n % len; if (ssrc = NULL) {return 0;} int I = 0; char * s = src; while (I
        
     

3. xinmail: string inversion: given the string "we; tonight; you;", encode the output "ew; thginot; uoy ;"

Idea: use two variables, first and end, to record the subscripts of the first and last characters of the substring to be reversed, each encounter '; ', the substring between first and end is reversed before and after.

Void ReverseStr (char * src) {int len = strlen (src); int I = 0; int first = 0; int end = 0; while (I
              
       

If ';' is not at the end of a given string, such as "we; tonight; you", encode the output to "ew; thginot; uoy ".

You only need to modify lines 9, 10, and 11 of the code:

if(src[i]==';'||i==len-1){if(src[i]==';')end=i-1;else  end=i;}

4. Xiai: Under the X86 structure, what is the output result of the following code?

Char str [20] = "Good night"; int * p = (int *) str; p [0] = 0x61626364; p [1] = 0x31323334; p [2] = 0x41424344; cout <
            
         

Problem solving: knowledge points:

  1. Memory size of int: 32bit = 4 byte; memory size of char: 4bit = 1 byte;
  2. ASCII code of common characters, 'A': 97; 'A' = 65; '0' = 48;
  3. Hexadecimal conversion, 0x61 = 97;
  4. Size-end mode: the so-called small-end mode stores low data index data in the low address of the memory, while the high data is stored in the high address of the memory, this storage mode effectively combines the high and low addresses with the data bit permissions. The high Address part has a high weight, and the low address part has a low weight, which is consistent with our logic method. The so-called big-end mode stores the index data Low (that is, the few digits after the smaller weights) in the memory's high address, while the data is high, stored in a low address in the memory, this storage mode is a bit similar to processing data as a string order: the address increases from small to large, and the data is placed from high to low;

The X86 structure is in the small-end mode, so

P [0] = 0x61626364; // 97,98, 99,100 corresponds to a, B, c, d, and the String str on the small end is dcba p [1] = 0x31323334; // similarly, 4321 p [2] = 0x41424344; // similarly, DCBA

Code output: dcba4321DCBA

This article is available at http://www.nowamagic.net/librarys/veda/detail/286.

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.