求指正-C语言-字符串&&排序
/*编写程序,其功能是:从键盘输入一个字符串,对字符串下标为奇数的字符按ASCII码大小递增排序,下标为偶数不变*/#include<stdio.h>
#include<string.h>
#define M 50
void main()
{
char str;
char temp;
int i,j;
scanf("%s",str);
temp=str;
/*printf("%d",strlen(str));*/
for(i=1;i=strlen(str)-1;i+=2)
{
for(j=1;j=strlen(str)-i-1;j+=2){
temp=str;
str=str;
str=temp;
}
}
} 本帖最后由 jackz007 于 2020-12-12 19:58 编辑
排序的要点是要对有先后次序的两个数做比较,合乎要求的维持原样,否则就交换位置。但是,在楼主的代码中,却没有看到有这样的比较。
#include<stdio.h>
#include<string.h>
#define M 50
int main(void)
{
char str , temp ;
int i , j , m ;
scanf("%s" , str) ;
for(m = strlen(str) , i = 1 ; i < m - 2 ; i += 2) {
for(j = i + 2 ; j < m ; j += 2) {
if(str < str) {
temp = str ;
str = str ;
str = temp ;
}
}
}
printf("%s\n", str) ;
}
编译、运行实况
D:\00.Excise\C>cl x.c
Microsoft (R) 32-bit C/C++ Optimizing Compiler Version 12.00.8804
Copyright (C) Microsoft Corp 1984-1998. All rights reserved.
x.c
Microsoft (R) Incremental Linker Version 6.00.8447
Copyright (C) Microsoft Corp 1992-1998. All rights reserved.
/out:x.exe
x.obj
D:\00.Excise\C>x
a9b8c7d6e5f4g3h2i1j0
a0b1c2d3e4f5g6h7i8j9
D:\00.Excise\C> 本帖最后由 风过无痕1989 于 2020-12-12 23:47 编辑
下面的程序已经调试好了,并提供两种输出方式(因题目没有给出输出要求),用开关 #if() . . . #endif 控制,哪个为 #if(1) 就使用哪种方式输出:
/*编写程序,其功能是:从键盘输入一个字符串,对字符串下标为奇数的字符按ASCII码大小递增排序,下标为偶数不变*/
#include<stdio.h>
#include<string.h>
#define M 50
void main()
{
char str, sj;
char temp;
int i, j, n, count = 0;
scanf("%s", str);
/*printf("%d",strlen(str));*/
n = strlen(str);
for (i = 0;i < n;i++)// 将下标为奇数的元素转入另一个数组
{
if (i % 2 != 0)
{
sj = str;
str = '9'; // 作个标志,该单元字符已被移出
count++;
}
}
for (i = 0;i < count;i++) // 冒泡排序
{
for (j = 0;j < count - 1;j++)
{
if (sj > sj)
{
temp = sj;
sj = sj;
sj = temp;
}
}
}
sj = '\0'; // 在字符串的末尾加上一个结束符
// 排序后插回原数组输出
#if(0)
j = 0;
for (i = 0;i < n;i++)// 将下标为奇数的元素转入原数组
{
if (i % 2 != 0)
{
str = sj;
j++;
}
}
printf("%s\n", str);
printf("\n");
#endif
// 排序后分别输出
#if(1)
for (i = 0;i < n;i++)// 后面不为字符9的元素前移
{
for (j = 0;j < n;j++)
{
if (str == '9' && str != '9' && j > i && str != '\n')
{
str = str;
str = '9';
}
}
}
for (i = 0;i < n;i++)
if (str == '9')
str = '\0'; // 在字符串的末尾加上一个结束符
printf("%s\n", str);
printf("%s\n", sj);
#endif
} 你要的程序已经调试好了 jackz007 发表于 2020-12-12 19:55
排序的要点是要对有先后次序的两个数做比较,合乎要求的维持原样,否则就交换位置。但是,在楼主的 ...
非常感谢 风过无痕1989 发表于 2020-12-12 23:48
你要的程序已经调试好了
非常感谢
页:
[1]