Today, a problem was encountered, the main idea is to enter two strings, and then give the two strings according to the ASCII code from small to large to sort, and finally in the merging of two strings, requires the deletion of the same characters. The beginning of the feeling is quite simple, but it is still a small problem is a lot of. Just look at the code, there are a lot of notes in the code that need to be noted.
1#include <stdio.h>2#include <string.h>3 voidSortChar*P)//sort the string with the first address of the string4 {5 Chartemp;6 Char*head,*min,*Next;7 for(head=p; (*head)! =' /'; head++)//sort strings by ASCII using the Select Sort method8 {9Min=head;//min to record the characters with the least ASCII code valueTen for(next=head+1;(*next)! =' /'; next++) One { A if((*min) > (*next)) -min=Next; - } theTemp=*head;//Exchange after a comparison is completed -*head=*min; -*min=temp; - } + - } + voidJoinChar*STR1,Char*STR2,Char*STR)//concatenate strings and remove the same characters, with parameters of two strings with content and a large empty string A { at Char*p,*Next; -strcat (STR,STR1);//concatenate str1 and str2 together with a string join function and put it in an empty str - strcat (STR,STR2); -Str[strlen (str) +1]=' /';//adds a string terminator to the end of STR -Sort (str);//to sort str by code value - for(p=str;*p!=' /';p + +)//retrieving the same characters in a string by selecting the Sort method in { - for(next=p+1; *next!=' /'; next++) to { + if(*p==*next)//if the previous character and the following word typeface, etc. - { the for(; *next!=' /'; next++)//move all the following characters forward one bit, overwriting the same characters **next=* (next+1); $ }Panax Notoginseng Else //if not equal, jump directly out of the character after the next character with it - Break; the } + } A the } + intMain () - { $ Charstr1[Ten]=" "; $ Charstr2[Ten]=" "; - Charstr[ -]=" ";//If you want to assign the STR to null first, there will be a problem with the connection string. -printf"Please enter string 1:"); thescanf"%s", str1); - sort (str1);Wuyiprintf"Please enter string 2:"); thescanf"%s", str2); - sort (str2); Wuprintf"the sorted string 1 is%s\n", str1); -printf"the sorted string 2 is%s\n", str2); About join (STR1,STR2,STR); $printf"the merged string is%s\n", str); - return 0; -}
The previous review of the selection of the sorting method, the program is to be familiar with the consolidation. In the 24th and 25 rows, if you do not initialize the STR array to NULL, then the concatenated string output will appear with various errors.
There is also a judgment error, in the 28th line of the program and 30 lines, beginning without adding *, so become a dead loop. Therefore, not only to consider the overall situation, but also pay attention to the small details.
Questions about string Sort merging