String exercises and interview questions (Part 2) and exercises (Part 2)

Source: Internet
Author: User

String exercises and interview questions (Part 2) and exercises (Part 2)

Link 1:Explanation of string exercises and interview questions (Article 1)


6 questions: reply string (basic questions of the Competition)

Enter a string to obtain its longest echo substring. 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.

# Include <stdio. h> # include <ctype. h> # include <string. h> # define MAXN 5000 + 10 char buf [MAXN], s [MAXN]; int p [MAXN]; int main (void) {int n, m = 0, max = 0, x, y; int I, j, k, OK; fgets (buf, sizeof (buf), stdin); n = strlen (buf ); for (I = 0; I <n; I ++) {if (isalpha (buf [I]) {p [m] = I; // the actual position of the character s to be saved: s [m ++] = toupper (buf [I]); // save it as a capital letter }}for (I = 0; I <m; I ++) // start position of the enumerative input string {for (j = I; j <m; j ++) // Enumeration Return string termination position {OK = 1; for (k = I; k <= j; k ++) {if (s [k]! = S [I + j-k]) OK = 0;} if (OK & j-I + 1> max) {max = j-I + 1; x = p [I]; y = p [j] ;}}for (I = x; I <= y; I ++) {printf ("% c ", buf [I]);} printf ("\ n"); return 0 ;}
Method 2:Enumerative the center of the input string.
# Include <stdio. h> # include <string. h> # include <ctype. h> # define MAXN 5000 + 10 char buf [MAXN], s [MAXN]; int p [MAXN]; int main (void) {int n, m = 0, max = 0, x, y; int I, j; fgets (buf, sizeof (buf), stdin); n = strlen (buf); for (I = 0; I <n; I ++) {if (isalpha (buf [I]) {p [m] = I; // Save the actual position of the character s [m ++] = toupper (buf [I]) ;}}for (I = 0; I <m; I ++) // enumerate the center position of the input string {for (j = 0; I-j> = 0 & I + j <m; j ++) // The length of the substring is odd {if (s [I-j]! = S [I + j]) break; if (j * 2 + 1> max) {max = j * 2 + 1; x = p [I-j]; y = p [I + j] ;}}for (j = 0; I-j> = 0 & I + j + 1 <m; j ++) // The length of the input string is an even number {if (s [I-j]! = S [I + j + 1]) break; if (j * 2 + 2> max) {max = j * 2 + 2; x = p [I-j]; y = p [I + j + 1] ;}}for (I = x; I <= y; I ++) printf ("% c ", buf [I]); printf ("\ n"); return 0 ;}
Resolution: when enumerating the intermediate position of the input string, you must note that the processing method with an odd or even length is different.


7 questions: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", and the minimum successor of "dhz" is "di ".(Google exam)

#include <stdio.h>#include <string.h>#define MAXN 1024int main(void){    char buf[MAXN];    int n, m, i;    scanf("%s", buf);    n = strlen(buf);    for (i = n - 1; i >= 0; i--)    {        if (buf[i] + 1 <= 'z')        {            buf[i] += 1;            buf[i + 1] = '\0';            break;        }    }    printf("%s\n", buf);    return 0;}

Resolution: + 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.


8 questions:Under the X86 structure, what is the output result of the printf code below?(Xiai interview questions)

#include <stdio.h>int main(void){    char str[20]="Good night";    int *p = (int *)str;    p[0] = 0x61626364;    p[1] = 0x31323334;    p[2] = 0x41424344;    printf("%s\n", str);    return 0;}

Resolution: 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

OK?

 

File system

Uttered words OK?

End.

After the blank space is compressed:

This Content hoho is OK? File systemuttered words OK? End.(Interview questions)

The reference procedure is as follows:

# Include <stdio. h> # include <string. h> # include <stdlib. h> const char * p = "This Content hoho is OK \ OK? \ File system \ n \ uttered words OK? \ End. "; int IsSpace (char c) {if (c ='') return 1; else if (c = '\ n' | c =' \ R ') return 2; else if (c = '\ t') return 3; else return 0;} char * shrink_space (char * dest, const char * src, size_t n) {char * t_dest = dest; char temp; int pre =-1, cur =-1, key =-1; while (IsSpace (* src )) // ignore the leading space of the string src ++; * t_dest = * src; while (temp = * src ++ )! = '\ 0') {key = IsSpace (temp); if (0 = key) // 0 represents the character {cur = 0; * t_dest ++ = temp ;} else if (1 = key) // 1 indicates space {if (pre = 1) // if the front is also a space continue; else {cur = 1; * t_dest ++ = temp ;}} else if (2 = key & pre = 0) {* t_dest ++ = '';} else if (3 = key) {if (pre = 1) continue; else {* t_dest ++ = ''; // convert \ t to 1 space. cur = 1;} pre = cur;} * t_dest = '\ 0'; return dest;} int main (void) {int len = strlen (p); char * dest = (char *) malloc (sizeof (char) * (len + 1); shrink_space (dest, p, 1 ); printf ("% s \ n", dest); free (dest); dest = NULL; return 0 ;}


10 questions:Write the code to find the number of occurrences of the substring in the parent string.(Interview questions)

# Include <stdio. h> # include <string. h> # define BUFSIZE 1024int StrCount (char * strLong, char * strShort) {int result = 0; char * t_strS = strShort; char * t_strD = strLong; while (* t_strD! = '\ 0') {t_strS = strShort; // substring while (* t_strD = * t_strS & * t_strS! = '\ 0' & * t_strD! = '\ 0') {t_strD ++; t_strS ++;} if (* t_strS =' \ 0') result + = 1; else if (* t_strD = '\ 0') break; else t_strD ++;} return result;} int main (void) {int len; char strD [BUFSIZE], strS [BUFSIZE]; fgets (strD, sizeof (strD), stdin); len = strlen (strD); strD [len-1] = '\ 0'; fgets (strS, sizeof (strS), stdin); len = strlen (strS); strS [len-1] = '\ 0'; printf ("% d \ n", StrCount (strD, strS); 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]);

8088 assembly language test questions 1. Input a string from the keyboard (the string length is less than 100 characters), count the length of the string, and output the string

I helped you complete your program. After compilation, you can enter a string (the length of the string is less than 100 characters) on the keyboard, count the length of the string, and output the string, it should be noted that the length of the final output string is hexadecimal, but this does not matter. Haha
~~~~~~~~~~~~~~~~~~~~~~~~~~~
;-----------------------------------------------------------------

;-------------------------------
Macro definition
Display MACRO string
Mov ah, 09 h
Lea dx, string
Int 21 h
ENDM
;-------------------------------
; ********************************* Data Segment
Data segment
Array db 100 dup (0); array Open Space
String1 db "Please input a string ended with ENTER: $"; string ends with $
String2 db "The length of the string = $"
String3 db "The string you inputed is: $"
Crlf db, "$"; 13 press enter, 10 line feed
Data ends
;**********************************
; ********************************** Code segment
Code segment
Assume ds: data, cs: code; Section ing
Main proc far; parameters used for far subprogram calling
;-----------------------------------------
Start:
Push ds
Sub ax, ax; cleared
Push ax; pressure Stack
Mov ax, data; transfers data to the data Segment
Mov ds, ax
;---------------------------------------
The main program starts.
;---------------------------------------------------------
; Displays the content of "Please input a string ended with ENTER: $"
Display stri ...... remaining full text>

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.