Home > Back-end >  To solve, won't do
To solve, won't do

Time:02-28

Enter a string, the string in all lowercase characters such as char a [20]="bcadfgih
"
To smallest to the lowercase letters (a minimum z biggest) in the above, for example a [20] into "abcdfghi
"
(using a sorting algorithm)

To enter another one character such as char b='e', insert a finished sequence of [20], insert rule is inserted into the smaller than b and the characters of larger than b,

Such as the above a [20]="abcdefghi"

CodePudding user response:

Sorting and insert operation, it is not difficult problem, it is suggested that the building Lord their first try, problems come up

CodePudding user response:

Basic function, and no optimization,
 
# define _CRT_SECURE_NO_WARNINGS
#include
#include
#include

20 # define N

//order
Void cSort (char [], int n)
{
int i, j;
char tmp;
For (I=0; I & lt; N - 1; I++)
{
For (j=0; J & lt; N - I - 1; J++)
{
If (a [j] & gt; A [m + 1])
{
TMP=a, [j].
A [j]=a, a + 1 bonus to [j].
A [j + 1)=TMP;
}
}
}
}

//insert
Void cInsert (char [], int n, char b)
{
Int I, index=0;
For (I=0; I & lt; n; I++)
{
If (a [I] & gt; B)
{
The index=I;
break;
}
}

For (I=n; I & gt;=index; I -)
{
A [I]=a, [I - 1].
}
A [index]=b;
}

Int main (void)
{
Char a [N]={0}, t;
Printf (" input a string (lowercase) : \ n ");
The scanf (" % s ", a);
Int n=strlen (a);
If (n & lt;=N - 1)
{
CSort (a, n);
}
The else
{
Printf (" string crossing the line! \n");
exit(1);
}
Printf (" sorted the string: \ n \ n % s ", a);

Getchar ();

If (n & lt; N - 1)
{
Printf (" input of characters to be inserted: \ n ");
The scanf (" % c ", & amp; t);
CInsert (a, n, t);
}
The else
{
Printf (" string has reached its limit, can't insert the character, \ n ");
}

Printf (" % s \ n ", a);

system("pause");
return 0;
}

CodePudding user response:

//enter a string, the string in all lowercase characters such as char a [20]="bcadfgih 
"//the lowercase letters for smallest to largest (a minimum z) to the above, for example a [20] into "abcdfghi
"//(using a sorting algorithm)
//to enter another one character such as char b='e', insert a finished sequence of [20], insert rule is inserted into the smaller than b and the characters of larger than b,
//such as the above a [20]="abcdefghi"
#include
#include
Int main () {
[20] char a, b;
Int I, j, N.

The fgets (a, 19, stdin);
N=strlen (a);
If (=='\ n' a/n - 1) {a [n - 1)=0; N -; }
Printf (" % s \ n ", a);

for (i=0; iFor (j=I + 1; JIf (a [I] & gt; A [j]) {
B=a, [I]. A [I]=a, [j]. A [j]=b;
}
}
}
Printf (" % s \ n ", a);

The rewind (stdin);
B=getchar ();
for (i=0; iIf (bFor (j=N; J>=I; J -) a [j + 1)=a, [j].
[I]=b;
break;
}
}
Printf (" % s \ n ", a);

return 0;
}
  • Related