1. Procedure 1.1 Program Introduction
Enter 6 strings from the keyboard (containing only English letters and numbers), and align the 6 strings from small to large and output the results. (c language)
1.2 Programming analysis
This is my problem in the basic test topic from the Dark Horse registration system.
First look at the topic, feel that the problem is not difficult, but is:
* Create an array of strings
* Then Loop 6 times in a loop of 6 cycles, then put the input string inside the string array
* Then create an array of lengths to count the length of the corresponding string
* Finally, the elements in the length array are sorted by size ( Note: When sorting the elements in the length array, the same operation is performed on the string array to facilitate subsequent output strings )
* Output elements from a string array in array order
1.3 The first version of the program is as follows
/** *9. Enter 6 strings from the keyboard (containing only English letters and numbers), arrange the 6 strings from small to large and output the results. (c language)*/#include<stdio.h>intMainintargcConst Char*argv[]) {//1. Create a new string variable to hold the string for each input Char*string; //2. Create a new string array to hold the address of each string Char* stringgroup[6]; //3. Define a variable to hold the length of the stringUnsignedLonglength[6]; //4. The system prompts the user to enter 6 strings in a row to determineprintf"Please enter 6 strings, including numbers and letters, and return to OK \ n"); //5. Loop prompts the user for continuous input string for(inti =0; I <6; i++) { //5.1 Prompt user to enter nth stringprintf"Please enter%d string \ n", (i+1)); //5.2 Save the nth string to the n item of a string pointer arrayscanf"%s",string); //5.3 Gets the length of the corresponding string and saves it to the length array at one timeLength[i] = strlen (string); //5.4string assignment to Stringgroup arrayStringgroup[i] =string; } //6. Create a new 2 variable to use as an intermediate variable for the interchange Operation Char*Pswap; unsignedLongswap; //7. Bubble sort for(inti =0; I <6; i++) { for(intj = i+1; J <6; j + +) { //7.1 When the current element of the length array is greater than the subsequent element if(Length[i] >Length[j]) { //7.1.1 The value of the array of lengths for the corresponding index exchangeSwap =Length[i]; Length[i]=Length[j]; LENGTH[J]=swap; //7.1.2 The value of a string array that swaps the corresponding indexPswap =Stringgroup[i]; Stringgroup[i]=Stringgroup[j]; STRINGGROUP[J]=Pswap; } } } //8. Looping through a string array for(inti =0; I <6; i++) {printf ("%s", Stringgroup[i]); } return 0;}
1.4 Program Run Error results
Results the output of the program is disappointed, and the last input string is output 6 times consecutively.
For example: The last time I entered the string is "Itheima", then the program run the result is as follows:
Itheima Itheima Itheima Itheima Itheima Itheima
2. Program Debugging 2.1 Program error analysis
First of all, the program does not directly display errors or warnings, it can also run, stating that the problem with the program is the algorithm and logic.
So I used the main program at the beginning of the place to add a breakpoint step-by-step tracking program to see what can be found.
Sure enough, the second time I saw the input loop, I found the input string of string, Stringgroup also changed, the code is as follows:
for(inti =0; I <6; i++) {//This time i==1, is already the second cycle ofprintf"Please enter%d string \ n", (i+1)); The second string input, for example: the first time I entered Itheima, the second time I entered is itcastscanf"%s",string);Length[i] = strlen (string);
At this point we will be in the left side of the variable Watch window to the magical Discovery Stringgroup array of the first element "Itheima" magically changed to "Itcast" and the most recently entered a string likeStringgroup[i] =string; }
2.2 Program Error judgment
After analysis, I found the cause of the mistake. Each time the loop body string is assigned a value, it actually assigns the pointer of string string to the Stringgroup array, which means
The input of six consecutive times is a string pointer to the Stringgroup array, so the Stringgroup array is full of strings pointing to it.
So when we last entered string strings, the Stringgroup array retains 6 identical string strings.
2.3 First modification of the program
This time I put the string variables defined in the input loop into the body, the modified code is as follows:
for(inti =0; I <6; i++) {Char*string; //this time i==1, it's the second cycle.printf"Please enter%d string \ n", (i+1)); //The second string input, for example: the first time I entered Itheima, the second time I entered is itcastscanf"%s",string); Length[i]= Strlen (string); //at this point we will be in the left side of the variable Watch window to the magical Discovery Stringgroup array of the first element "Itheima" magically changed to "Itcast" and the most recently entered a string likeStringgroup[i] =string; }
The result is still the same problem, so where is the problem?
2.4 Program Second Modification
So I followed the debug for the second time, and found that the problem still appeared on the string variable definition, and I thought the string variable would be freed and redistributed after each loop.
But actually, because we're using a statically defined method, char *string, after the for-loop {}, the string does not have to be defined once per loop and then allocated a new memory space.
The reason is found, how to solve the problem?
* with the C language itself related to the dynamic allocation of memory functions to operate, each time the input loop, call memory allocation of a new space to accept the input to the string, and then assign to Stringgroup, and finally at the end of the program to unify release.
* With OC inside the nsstring, because each time is in the heap dynamic allocation of memory alloc, so also can get the same effect, but out of the problem is to use C language to solve, so there is no use nsstring.
The final solution is as follows:
Since it is only necessary to open up 6 strings of memory space, then I will use the most violent method at the beginning of the program to define 6 string variables, and then assign a value to initialize the Stringgroup array ok.
The final complete procedure is as follows:
/** *9. Enter 6 strings from the keyboard (containing only English letters and numbers), arrange the 6 strings from small to large and output the results. (c language)*/#include<stdio.h>intMainintargcConst Char*argv[]) { //1. Open 6 100 long string spaces from memory Charstring1[ -]; Charstring2[ -]; Charstring3[ -]; Charstring4[ -]; Charstring5[ -]; Charstring6[ -]; //2. Create a new string array to hold the address of each string Char* stringgroup[6]={String1,string2,string3,string4,string5,string6}; //3. Define a variable to hold the length of the stringUnsignedLonglength[6]; //4. The system prompts the user to enter 6 strings in a row to determineprintf"Please enter 6 strings, including numbers and letters, and return to OK \ n"); //5. Loop prompts the user for continuous input string for(inti =0; I <6; i++) { //5.1 Prompt user to enter nth stringprintf"Please enter%d string \ n", (i+1)); //5.2 Save the nth string to the n item of a string pointer arrayscanf"%s", Stringgroup[i]); //5.3 Gets the length of the corresponding string and saves it to the length array at one timeLength[i] =strlen (Stringgroup[i]); } //6. Create a new 2 variable to use as an intermediate variable for the interchange Operation Char*Pswap; unsignedLongswap; //7. Bubble sort for(inti =0; I <6; i++) { for(intj = i+1; J <6; j + +) { //7.1 When the current element of the length array is greater than the subsequent element if(Length[i] >Length[j]) { //7.1.1 The value of the array of lengths for the corresponding index exchangeSwap =Length[i]; Length[i]=Length[j]; LENGTH[J]=swap; //7.1.2 The value of a string array that swaps the corresponding indexPswap =Stringgroup[i]; Stringgroup[i]=Stringgroup[j]; STRINGGROUP[J]=Pswap; } } } //8. Looping through a string array for(inti =0; I <6; i++) {printf ("%s", Stringgroup[i]); } return 0;}
3. Summary
* The string array holds only the starting address of each string element and does not mirror the new memory space.
* If you are defining a large array of strings, such as char* stringgroup[1000] then I am not scientific, I can only use a loop body, each time in the loop of the new string inside the body.
* If you find that the algorithm and logic of the program is wrong, then you can best identify the cause of the problem by using one-step debugging of the value of the variable.
* Pointer Knowledge point is the focus and difficulty of C and OC, and the characteristics of C language family, must be fully understood and mastered.
Dark Horse programmer _ios Develop _OBJECTIVE-C study notes _ Analyze and debug your own Dark Horse Basic test program