String exercises and interview questions (Article 2)

Source: Internet
Author: User

String exercises and interview questions (Article 2)
Question: enter a string for the essay string (basic question of the Competition) to obtain its longest essay string. The meaning of a substring is a string segment that appears continuously in the original string. The meaning of the reply is: Reading and viewing are the same, such as abba and yyxyy. When judging, all punctuation marks and spaces should be ignored, and case-sensitive characters should be ignored, but the output should be unchanged (do not output extra characters at the beginning and end of the input string ). The length of the input string cannot exceed 5000 and occupies a single row. The longest input string should be output. If there are multiple input strings, the output start position is the leftmost. Sample input: Confuciuss say: Madam, I'm Adam. sample output: Madam, I'm Adam Method 1: enumerate the start and end points of the input string. Copy code 1 # include <stdio. h> 2 # include <ctype. h> 3 # include <string. h> 4 # define MAXN 5000 + 10 5 6 char buf [MAXN], s [MAXN]; 7 int p [MAXN]; 8 9 int main (void) 10 {11 int n, m = 0, max = 0, x = 0, y = 0; 12 int I, j, k, OK; 13 fgets (buf, sizeof (buf), stdin); 14 n = strlen (buf); 15 16 for (I = 0; I <n; I ++) 17 {18 if (isalpha (buf [I]) 19 {20 p [m] = I; // the actual position of the character to be saved is 21 s [m ++] = toupper (buf [I]); // save Uppercase letter 22} 23} 24 25 for (I = 0; I <m; I ++) // start position of the enumerative input string 26 {27 for (j = I; j <m; j ++) // enumerative return string termination position 28 {29 OK = 1; 30 for (k = I; k <= j; k ++) 31 {32 if (s [k]! = S [I + j-k]) 33 OK = 0; 34} 35 36 if (OK & j-I + 1> max) 37 {38 max = j-I + 1; 39 x = p [I]; 40 y = p [j]; 41} 42} 43} 44 45 for (I = x; I <= y; I ++) 46 {47 printf ("% c", buf [I]); 48} 49 printf ("\ n"); 50 51 return 0; 52} copy code Method 2: enumeration of the center of the input string. Copy code 1 # include <stdio. h> 2 # include <string. h> 3 # include <ctype. h> 4 # define MAXN 5000 + 10 5 char buf [MAXN], s [MAXN]; 6 int p [MAXN]; 7 8 int main (void) 9 {10 int n, m = 0, max = 0, x = 0, y = 0; 11 int I, j; 12 fgets (buf, sizeof (buf ), stdin); 13 n = strlen (buf); 14 15 for (I = 0; I <n; I ++) 16 {17 if (isalpha (buf [I]) 18 {19 p [m] = I; // the actual position of the stored character is 20 s [m ++] = toupper (buf [I]); 21} 22} 23 24 For (I = 0; I <m; I ++) // enumerate the middle position of the input string 25 {26 for (j = 0; i-j> = 0 & I + j <m; j ++) // The length of the return substring is 27 odd. {28 if (s [I-j]! = S [I + j]) 29 break; 30 if (j * 2 + 1> max) 31 {32 max = j * 2 + 1; 33 x = p [I-j]; 34 y = p [I + j]; 35} 36} 37 38 for (j = 0; i-j> = 0 & I + j + 1 <m; j ++) // The length of the input string is an even number 39 {40 if (s [I-j]! = S [I + j + 1]) 41 break; 42 if (j * 2 + 2> max) 43 {44 max = j * 2 + 2; 45 x = p [I-j]; 46 y = p [I + j + 1]; 47} 48} 49} 50 51 for (I = x; I <= y; I ++) 52 printf ("% c", buf [I]); 53 printf ("\ n"); 54 55 return 0; 56} copy code parsing: when enumerating the intermediate position of the input string, you must note that the processing method with an odd or even length is different. Question 7: encoding is used to obtain the minimum successor of a given string (all lowercase English letters). For example, the minimum successor of "abc" is "abd ", the minimum successor of "dhz" is "di ". Copy code 1 # include <stdio. h> 2 # include <string. h> 3 # define MAXN 1024 4 5 int main (void) 6 {7 char buf [MAXN]; 8 int n, m, I; 9 scanf ("% s ", buf); 10 11 n = strlen (buf); 12 for (I = n-1; I> = 0; I --) 13 {14 if (buf [I] + 1 <= 'Z') 15 {16 buf [I] + = 1; 17 buf [I + 1] = '\ 0'; 18 break; 19} 20} 21 printf ("% s \ n", buf); 22 23 return 0; 24} copy code parsing: + 1 for the last character. If it is greater than 'Z', + 1 for the first character. If it is greater than 'Z', repeat the previous step.. Question 8: What is the result of the printf output of the following code under the X86 structure? Copy code 1 # include <stdio. h> 2 3 int main (void) 4 {5 char str [20] = "Good night"; 6 int * p = (int *) str; 7 p [0] = 0x61626364; 8 p [1] = 0x31323334; 9 p [2] = 0x41424344; 10 printf ("% s \ n ", str); 11 12 return 0; 13} copy code parsing: the output result is dcba4321DCBA. In the X86 structure, the low data level is stored in the low memory address, and the high data level is stored in the high memory address. Pay attention to the ASCII code (decimal) of common characters, 'A' is 97, 'A' is 65, and '1' is 49. Question 9: compile a function, input a string, and create a new string. compress all one or more consecutive blank characters into one space. The white spaces mentioned here include space, '\ t',' \ n', and '\ R '. For example, the original string is: This Content hoho is OK? File system uttered words OK? End. After the space is compressed, it is: This Content hoho is OK? File systemuttered words OK? End. (interview questions) Reference Program: Copy code 1 # include <stdio. h> 2 # include <string. h> 3 # include <stdlib. h> 4 5 const char * p = "This Content hoho is OK \ 6 OK? \ 7 \ 8 file system \ n \ 9 uttered words OK? \ 10 end. "; 11 12 int IsSpace (char c) 13 {14 if (c ='') 15 return 1; 16 else if (c = '\ n' | c =' \ R') 17 return 2; 18 else if (c = '\ t ') 19 return 3; 20 else 21 return 0; 22} 23 24 char * shrink_space (char * dest, const char * src, size_t n) 25 {26 char * t_dest = dest; 27 char temp; 28 int pre =-1, cur =-1, key =-1; 29 while (IsSpace (* src )) // ignore the starting space of the string: 30 src ++; 31 * t_dest = * src; 32 33 w Hile (temp = * src ++ )! = '\ 0') 34 {35 key = IsSpace (temp); 36 if (0 = key) // 0 indicates character 37 {38 cur = 0; 39 * t_dest ++ = temp; 40} 41 else if (1 = key) // 1 indicates space 42 {43 if (pre = 1) // if the front is 44 continue; 45 else 46 {47 cur = 1; 48 * t_dest ++ = temp; 49} 50} 51 else if (2 = key & pre = 0) 52 {53 * t_dest ++ = ''; 54} 55 else if (3 = key) 56 {57 if (pre = 1) 58 continue; 59 else 60 {61 * t_dest ++ = ''; // convert \ t to 1 Space 62 cur = 1; 63} 64} 65 pre = cur; 66} 67 * t_dest = '\ 0'; 68 69 return dest; 70} 71 int main (void) 72 {73 int len = strlen (p); 74 char * dest = (char *) malloc (sizeof (char) * (len + 1); 75 shrink_space (dest, p, 1); 76 printf ("% s \ n", dest); 77 free (dest); 78 dest = NULL; 79 80 return 0; 81} copy code 10: write the code to find the number of occurrences of the substring in the parent string. Copy code 1 # include <stdio. h> 2 # include <string. h> 3 # define BUFSIZE 1024 4 5 int StrCount (char * strLong, char * strShort) 6 {7 int result = 0; 8 char * t_strS = strShort; 9 char * t_strD = strLong; 10 while (* t_strD! = '\ 0') 11 {12 t_strS = strShort; // substring 13 while (* t_strD = * t_strS & * t_strS! = '\ 0' & * t_strD! = '\ 0') 14 {15 t_strD ++; 16 t_strS ++; 17} 18 if (* t_strS =' \ 0') 19 result + = 1; 20 else if (* t_strD = '\ 0') 21 break; 22 else 23 t_strD ++; 24} 25 26 return result; 27} 28 29 int main (void) 30 {31 int len; 32 char strD [BUFSIZE], strS [BUFSIZE]; 33 fgets (strD, sizeof (strD), stdin); 34 len = strlen (strD ); 35 strD [len-1] = '\ 0'; 36 fgets (strS, sizeof (strS), stdin); 37 len = strlen (strS ); 38 strS [len-1] = '\ 0'; 39 printf ("% d \ n", StrCount (strD, strS); 40 return 0; 41}

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.