Home > Back-end >  Consult a C language problem, given a string, how to sort by the length of the word
Consult a C language problem, given a string, how to sort by the length of the word

Time:09-25

For example: char STR="frank ok from the"
Processing: STR="ok from frank"

CodePudding user response:

First divided into an array of strings, then according to the length of the sequence, and then the connection string in sorted order

CodePudding user response:

This is very simple, isn't it, first press the space division (the last one to end the character '\ 0' for the identity division), then the strlen () according to the size once line,

CodePudding user response:

Who is a great god help me to look at when the three gibberish, how be to return a responsibility? Thank you,

//cpp11.13.11 
Void menu () {
Printf ("=================================================\ n ");
Printf (" 1. Print the source character list 2. ASCII order print \ n ");
Printf (" 3. According to the increasing length of print 4. According to the length of the first word printing \ n ");
Printf (" 5. Exit \ n ");
Printf ("=================================================\ n ");
Printf (" please enter the option: ");
}
Void remove_blank_char_only_save_one (char * s, char * ret) {
Int m=0;
int i;

For (I=0; S [I]!='\ 0') {
//attention to judge character Spaces to use single quotation marks', can't use double quotation marks
[I] if (s=='\ t') {
S [I]=' ';
}
If (isblank (s) [I]) {
Ret [m] [I]=s;
M++;
i++;
While (isblank (s) [I]) {
i++;
}
}
The else {
Ret [m] [I]=s;
M++;
i++;
}

}
Ret [m]='\ 0';
}
Void remove_blank_on_start (char * s) {
If (s==NULL | | * s=='\ 0')
{
return s;
}

int len=0;
Char * p=s;
While (* p!='\ 0' & amp; & Isspace (* p))
{
+ + p;
+ + len.
}

Memmove (s, p, strlen (s) - len + 1);

}
Void remove_blank_on_end (char * s)
{
If (s==NULL | | * s=='\ 0')
{
return s;
}

Int len=strlen (s);
Char * p=s + len - 1;
While (p & gt;=s & amp; & Isspace (* p))
{
* p='\ 0';
- p;
}
}
Int get_str_words_num (char * s, int n) {

for (int i=0; i [I] if (s==' ') {
n++;
}
}
Return the n + 1;
}
Void sort_str_by_word_len (char * s, char (* des) [10], int num) {
Int j=0, I, k=0;
Char t [50];//temporary array, in exchange for
for (int i=0; i <=strlen (s); I++) {
If (s [I]!=' ') {
//each word segmentation by Spaces, when faced with the blank space, the space character before deposit into a two-dimensional array
Des [j] [k++] [I]=s;
}
The else {
//when the blank space, add the end of the first set of one-dimensional series logo, and will start the second J + 1 one-dimensional array assignment, and let the K value to 0, starting from scratch,
Des [j] [k]='\ 0';
J++;
K=0;
}
}
//use the bubbling method, according to the two dimensional array of the length of the one-dimensional array sorted
For (int m=0; M & lt; num; M++) {
For (int n=0; N & lt; Num - m - 1; N++) {
If (strlen (des) [n] & gt; Strlen (des) [n + 1]) {
Strcpy (t, des [n]);
Strcpy (des [n], des [n + 1));
Strcpy (des (n + 1), t);
}
}
}
}

Void case2 (char * s) {
//bubble sort method
Char temp.
Char res [50].
Strcpy (res, s);
for (int i=0; i For (int j=0; J & lt; Strlen (res) - I - 1; J++) {
If (res [j] & gt; Res [m + 1]) {
Temp=res [j];
Res [j]=res [j + 1);
Res [j + 1)=temp;
}
}
}
Printf (" % s \ n ", res);
}
Void case3 (char * s) {
Char DST [10] [50];
Char res [50].
Int n_words=0;
Strcpy (res, s);
Char * temp=res;
Remove_blank_char_only_save_one (s, temp);
Remove_blank_on_start (temp);
Remove_blank_on_end (temp);
N_words=get_str_words_num (temp, n_words);
Sort_str_by_word_len (temp, DST, n_words);
for (int i=0; i Printf (" % s ", DST [I]);
}
printf("\n");
}


Void myprint (char * s) {
Int num.

The menu ();

While (the scanf (" % d ", & amp; Num)==1) {
The switch (num) {
Case 1:
Printf (" % s \ n ", s);
break;
Case 2:
Case2 (s);
break;
Case 3:
Case3 (s);


break;

Case 4:

break;
Case 5:
Printf (" bye!" );
return;
Default:
Printf (" please input the correct option! \n");
break;
}
The menu ();
}

}
Void cpp11_13_11 () {
char ch;
Char STR [50].
int i=0;

Printf (" do input 10 char: \ n ");
While (I & lt; 49) {
Ch=getchar ();
STR [I]=ch;
If ((STR [I]=='\ n') & amp; & (STR [I - 1]=='\ n')) {
break;
}
i++;
}
STR [I]='\ 0';
Setbuf (stdin, NULL);
Myprint (STR);
}

CodePudding user response:

To get every word, and then according to the length of the string sorting ~

CodePudding user response:

Code after the problem is that local variable in a function call released automatically, the data cannot be returned, into a global variables can be solved, the small white I were found by single step debugging problems,
  • Related