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.
#include <stdio.h>const char *str = "`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;'ZXCVBNM,./";int main(void){char c;int i;while ((c = getchar()) != EOF){for (i = 0; str[i] && str[i] != c; i++);if (str[i])putchar(str[i - 1]);elseputchar(c);}return 0;}
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 ".
#include <stdio.h>int main(void){ char c; int q = 1; while ((c = getchar()) != EOF) { if (c == '"') { printf("%s", q ? "``" : "''"); q = !q; } else printf("%c", c); } return 0;}
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:
# Include <stdio. h> # include <string. h> # define MAXLEN 80int main (void) {char str [MAXLEN + 1] = {0}; scanf ("% s", str ); int len = strlen (str); int result = len; int I, j; for (I = 1; I <len/2 + 1; I ++) // I is the period {for (j = 0; j <len-I; j ++) {if (str [j]! = Str [j + I]) break;} if (j = len-I) {result = I; break;} printf ("% d \ n ", result); return 0 ;}
Statement 2:
#include <stdio.h>#include <string.h>int main(void){ char word[100]; scanf("%s", word); int len = strlen(word); int i, j; for (i = 1; i <= len; i++) { if (len % i == 0) { int ok = 1; for (j = i; j < len; j++) if (word[j] != word[j % i]) { ok = 0; break; } if (ok) { printf("%d\n", i); break; } } } return 0;}
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)
#include <stdio.h>#include <string.h>#define MAX_LEN 1024void LoopMove_1(char *pStr, int steps){ int len = strlen(pStr) - steps; char temp[MAX_LEN]; strcpy(temp, pStr + len); strcpy(temp + steps, pStr); *(temp + steps + len) = '\0'; strcpy(pStr, temp);}void LoopMove_2(char *pStr, int steps){ int len = strlen(pStr) - steps; char temp[MAX_LEN]; memcpy(temp, pStr + len, steps); memcpy(temp + steps, pStr, len); memcpy(pStr, temp, len + steps);}int main(void){ char str1[] = "abcdefghi"; LoopMove_1(str1, 3); printf("%s\n", str1); char str2[] = "gklmnopqrst"; LoopMove_1(str2, 3); printf("%s\n", str2); return 0;}
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.
#include <stdio.h> #define MAX_LEN 1024 int main(void) { char arr[MAX_LEN], c; int i = 0, j; while ((c = getchar()) != '#') { arr[i++] = c; } arr[i] = '\0'; for (j = i - 1; j >= 0; j--) { printf("%c", arr[j]); } printf("\n"); return 0; }
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.
# Include <stdio. h> void StrRev () {char c; scanf ("% c", & c); if ('#' = c) {printf ("reverse string: "); return;} StrRev (); printf (" % c ", c);} int main (void) {printf (" enter an English string (end ): "); StrRev (); printf (" \ n "); return 0 ;}
Data Structure FAQ about strings
Create a hash table and record the number of occurrences of 26 letters in a-z
Char table [26];
First, assume that the first string is s1 and the 2nd string is s2.
For (I = 0; I <26); I ++)
Table [I] = 0;
For (I = 0; I <strlen (s1); I ++)
Table [s1 [I]-'a'] ++;
For (I = 0; I <strlen (s2); I ++)
If (table [s2 [I]-'a'] = 0)
{
Print ("the letter % c does not appear", s2 [I]);
Break;
}
If (I> = strlen (s2 ))
Print ("all letters appear ");
The complexity should be O (M + N), and M and N are the lengths of s1 and s2, respectively.
The second question is simpler:
For (I = 0; I <26); I ++)
Table [I] = 0;
For (I = 0; I <strlen (s1); I ++)
Table [s1 [I]-'a'] ++;
For (I = 0; I <26); I ++)
If (table [I] = 1)
Print ("only letters once: % c", table [I]);
Help: Exercise questions with characters and strings (drop answers)
In fact, it is very easy to find this. You just need to open Baidu encyclopedia, enter the "character string question", and search for it. There will be a lot of questions waiting for you to do ....
You searched for the information, see references:
Reference: wenku.baidu.com/.?&od=0