String exercises, Interview Questions (article 1), and exercises

Source: Internet
Author: User

String exercises, Interview Questions (article 1), and exercises

Question 1: WERTYU (Competition basics)

When you place your hand on the keyboard, you will be right and wrong when you do not pay attention to it. In this case, Q will change to W, J will change to K, and so on. Enter a wrong string and output the sentence that the typist intended to print.

Sample input: o s, gomr ypfsu/

Sample output: I AM FINE TODAY.

 1 #include <stdio.h>   2 const char *str = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";   3    4 int main(void)   5 {   6     char c;   7     int i;   8     while ((c = getchar()) != EOF)   9     {  10         for (i = 0; str[i] && str[i] != c; i++);  11         if (str[i])  12             putchar(str[i - 1]);  13         else  14             putchar(c);  15     }  16       17     return 0;  18 }  

Resolution: Pay attention to two points in this question: (1) determine the condition for loop termination. The constant string ends with "\ 0 ", the value of str [I] can be used to determine whether the last character is found. (2) The Backslash "\" is a special character and must use an escape sequence or directly reference its ASCII code.

 

Question 2: TeX brackets (Competition basics)

In TeX, the Left Double quotation mark is '', And the right double quotation mark is ''. Enter an article that contains double quotation marks. Your task is to convert it to the TeX format.

Sample input:

"To be or not to be," quoth the Bard, "thatis the question ".

Sample output:

''To be or not To be, "quoth the Bard,'' that is the question ".

 1 #include <stdio.h>   2    3 int main(void)   4 {   5     char c;   6     int q = 1;   7     while ((c = getchar()) != EOF)   8     {   9         if (c == '"')  10         {  11             printf("%s", q ? "``" : "''");  12             q = !q;  13         }  14         else  15             printf("%c", c);  16     }  17       18     return 0;  19 }  

Resolution: The key to this question is how to determine whether a double quotation mark is a left double quotation mark or a right double quotation mark.

 

3 questions: Cycle string (Competition basics)

If a string can be repeated multiple times by a string with a length of k, we say the string is in the k cycle. For example, abcabcabcabc takes 3 as the cycle (note that it also takes 6 and 12 as the cycle ). Enter a string of no more than 80 characters and output its minimum cycle.

Sample input: HoHoHo

Sample output: 2

Statement 1:

1 # include <stdio. h> 2 # include <string. h> 3 # define MAXLEN 80 4 int main (void) 5 {6 char str [MAXLEN + 1] = {0}; 7 scanf ("% s", str ); 8 int len = strlen (str); 9 int result = len; 10 int I, j; 11 12 for (I = 1; I <len/2 + 1; I ++) // I is the period 13 {14 for (j = 0; j <len-I; j ++) 15 {16 if (str [j]! = Str [j + I]) 17 break; 18} 19 20 if (j = len-I) 21 {22 result = I; 23 break; 24} 25} 26 printf ("% d \ n", result); 27 28 return 0; 29}

Statement 2:

 1 #include <stdio.h>   2 #include <string.h>   3 int main(void)   4 {   5     char word[100];   6     scanf("%s", word);   7     int len = strlen(word);   8     int i, j;   9   10     for (i = 1; i <= len; i++)  11     {  12         if (len % i == 0)  13         {  14             int ok = 1;  15             for (j = i; j < len; j++)  16                 if (word[j] != word[j % i])  17                 {  18                     ok = 0;  19                     break;  20                 }  21             if (ok)  22             {  23                 printf("%d\n", i);  24                 break;  25             }  26         }  27     }  28   29     return 0;  30 }  

Analysis: the two solutions are the same.

 

4 questions:Write a function that shifts the right n digits of a char string loop. For example, it turns out to be "abcdefghi". If n = 2, it should be "hiabcdefg" after the shift ".(Interview questions)

 1 #include <stdio.h>   2 #include <string.h>   3 #define MAX_LEN 1024   4    5 void LoopMove_1(char *pStr, int steps)   6 {   7     int len = strlen(pStr) - steps;   8     char temp[MAX_LEN];   9   10     strcpy(temp, pStr + len);  11     strcpy(temp + steps, pStr);  12     *(temp + steps + len) = '\0';  13     strcpy(pStr, temp);  14 }  15   16 void LoopMove_2(char *pStr, int steps)  17 {  18     int len = strlen(pStr) - steps;  19     char temp[MAX_LEN];  20   21     memcpy(temp, pStr + len, steps);  22     memcpy(temp + steps, pStr, len);  23     memcpy(pStr, temp, len + steps);  24 }  25   26 int main(void)  27 {  28     char str1[] = "abcdefghi";  29     LoopMove_1(str1, 3);  30     printf("%s\n", str1);  31   32     char str2[] = "gklmnopqrst";  33     LoopMove_1(str2, 3);  34     printf("%s\n", str2);  35     return 0;  36 }  

Analysis: This question provides two methods.

 

5 questions:Write a program, input a set of strings of any length from the keyboard, end the input when the "#" character is entered (the string does not include "#"), and then the program outputs the string in reverse direction.(Interview questions)

Method 1:Save the input string in a data structure, and then output its content in reverse direction. For example, first apply for a stack. Each time a character is entered, the character is written into the stack until "#" is found in the stack Queue ("#" is not included in the stack ). Then, the elements in the stack are ordered out of the stack.

 1 #include <stdio.h>     2 #define MAX_LEN 1024     3      4 int main(void)     5 {     6     char arr[MAX_LEN], c;     7     int i = 0, j;     8     while ((c = getchar()) != '#')     9     {    10         arr[i++] = c;    11     }    12     arr[i] = '\0';    13     14     for (j = i - 1; j >= 0; j--)    15     {    16         printf("%c", arr[j]);    17     }    18     printf("\n");    19         20     return 0;    21 }    

Method 2:You do not need to save the input string. Therefore, you can solve this problem recursively. Recursive algorithms have stack features. Each time a recursive function is called recursively, the system saves the scene in the stack and restores the field content when the call ends.

1 # include <stdio. h> 2 3 void StrRev () 4 {5 char c; 6 scanf ("% c", & c); 7 if ('#' = c) 8 {9 printf ("reverse string:"); 10 return; 11} 12 13 StrRev (); 14 printf ("% c", c ); 15} 16 17 int main (void) 18 {19 printf ("enter an English string (end with #):"); 20 StrRev (); 21 printf ("\ n"); 22 return 0; 23}

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.