ArticleDirectory
- 1. How to convert an integer to a string without using the ITOA function?
- 2. It is known that the function prototype is char * strcpy (char * strdest, const char * strsrc); where strdest is the destination string and strsrc is the source string.
- (1) do not call the string library functions of C ++/C. Compile the strcpy function.
- (2) Why should the strcpy function copy the content of strsrc to strdest?
- 3. Compile a function to shift n loops of a char string to the right. For example, it turns out to be "abcdefghi". If n = 2, it should be "hiabcdefg" after the shift ".
-
- 4. Put the words in a sentence upside down without putting the punctuation marks upside down. For example: I come from Beijing. After the inversion, it becomes: Beijing. From come I.
- 5. Programming: enter a line of strings to find the same and longest string, and output it and its first character position. For example, "yyabcdabjcabceg", the output result should be ABC and 3.
Basically, when a job seeker performs a written test, there is no non-test string. The string is also a relatively simple data structure, which may cause the interviewer to repeatedly ask questions. In fact, strings are also a testProgramImportant test points for program specifications and programming habits. Do not ignore these details, because these details will reflect your knowledge in the operating system, software engineering, boundary memory processing, and become a reference factor for companies to hire you.
1. How to convert an integer to a string without using the ITOA function?
Answer:
# Include <iostream> using namespace STD; int main () {int num = 12345, I = 0, j = 0; char temp [7], STR [7]; while (Num) {temp [I] = num % 10 + '0 '; // convert each digit of the integer num from the back to the char and save it in temp I ++; num = num/10;} temp [I] = 0; cout <"Temp:" <temp <Endl; I = I-1; // reverse tempwhile (I> = 0) {STR [J ++] = temp [I --];} STR [J] = 0; cout <"string:" <STR <Endl; return 0 ;}
If you can use the ITOA function, it is very simple, as follows:
# Include <iostream> # include <stdlib. h> using namespace STD; // use the ITOA function int main () {int num = 12345; char STR [7]; ITOA (Num, STR, 10 ); cout <"INTEGER:" <num <Endl <"string:" <STR <Endl; return 0 ;}
2. It is known that the function prototype is char * strcpy (char * strdest, const char * strsrc); where strdest is the destination string and strsrc is the source string. (1) do not call the string library functions of C ++/C. Compile the strcpy function. (2) Why should the strcpy function copy the content of strsrc to strdest?
Answer:
(1)CodeAs follows:
Char * strcpy (char * strdest, const char * strsrc) {assert (strdest! = NULL) & (strsrc! = NULL); char * address = strdest; while (* strdest ++ = * strsrc ++ )! = '\ 0') NULL; return address ;}
(2) return a specific value to implement a chained expression.
For example:
Int length = strlen (strcpy (strdest, "Hello World "));
3. Compile a function to shift n loops of a char string to the right. For example, it turns out to be "abcdefghi". If n = 2, it should be "hiabcdefg" after the shift ".
Answer:
(1) use the standard library function method:
Void loopmove (char * pstr, int steps) {int n = strlen (pstr)-steps; char temp [max_len]; strcpy (temp, pstr + n ); strcpy (temp + steps, pstr); * (temp + strlen (pstr) = '\ 0'; strcpy (pstr, temp );}
(2) methods that do not use standard library functions:
The last question of csdn (please refer to question 5 and comments)
4. Put the words in a sentence upside down without putting the punctuation marks upside down. For example: I come from Beijing. After the inversion, it becomes: Beijing. From come I.
Resolution:To solve this problem, you can take two steps: Step 1 replace the statement into:. gnijieb Morf EmOC I. The second step is to flip the word. If it is not a space, it starts to flip the word.
Answer:
The Code is as follows:
# Include <iostream> using namespace STD; int main () {int num =-12345, I = 0, j = 0, flag = 0, begin, end; char STR [] = "I come from Beijing. "; char temp; j = strlen (STR)-1; // The first step is to flip the entire while (j> I) {temp = STR [I]; STR [I ++] = STR [J]; STR [j --] = temp;} // flip part of the Second Step I = 0; while (STR [I]) {If (STR [I]! = '') {Begin = I; while (STR [I] & STR [I]! = '') I ++; // find STR [I] as the space character I = I-1; // return an end = I;} while (end> begin) // partial flip {temp = STR [begin]; STR [begin ++] = STR [end]; STR [end --] = temp;} I ++ ;} cout <"string:" <STR <Endl ;}
5. Programming: enter a line of strings to find the same and longest string, and output it and its first character position. For example, "yyabcdabjcabceg", the output result should be ABC and 3.
Answer:
# Include <iostream> # include <string> using namespace STD; int main () {string STR, TEP; cout <"enter a string:"; CIN> STR; for (INT I = Str. length ()-1; I> 1; I --) {for (Int J = 0; j <Str. length (); j ++) {If (J + I <= Str. length () {size_t T = 0; size_t num = 0; TEP = Str. substr (J, I); // string T = STR from large to small. find (TEP); // search for num = STR in positive order. rfind (TEP); // reverse query if (T! = Num) // If the locations of the two searches are inconsistent, the {cout <TEP <"" <t + 1 <Endl; return 0 ;}}} return 0 ;}
The C ++ interview questions have been basically completed. There may be other questions, such as the operating system, database, software testing, and computer network. Some content may be written in the next few days. Let's look forward to it together ..