#include <stdio.h> void fun( char *a ) { int n=0,i,c=0,t; char *b; b=a; while (*b=='*') n++; for(i=0;b[i]!='\0';i++) c=i+1; for(i=0;i<n;i++) { b[i]=b[i+n]; } for(i=c-n;i<c;i++) { b[i]='*'; } b[i]='\0'; } int main() { char s[81]; int n=0; scanf("%s",s); fun(s); puts(s); return 0; }为什么没有输出
时间: 2023-12-06 19:03:54 浏览: 66
这段代码中的函数 `fun()` 对一个字符串进行处理,将字符串中前面的星号移到后面,然后在末尾添加相应数量的星号,以保持字符串的长度不变。
根据你提供的信息,无法确定为什么没有输出。可能的原因有:
1. 在输入字符串时没有输入任何字符,导致字符串为空,函数 `fun()` 没有对其进行修改,所以输出结果为空。
2. 在输入字符串时输入了包含空格的字符串,`scanf()` 函数只会读取空格之前的部分,因此只有部分字符串被传递给了函数 `fun()` 进行处理,导致输出结果不正确。
3. 在函数 `fun()` 中有语法错误或逻辑错误,导致函数无法正常执行。
你可以检查上述情况并逐一排除,以确定问题的具体原因。
相关问题
*定义函数实现n个字符串按字典顺序排序 开头: #include<stdio.h> #include<string.h> void fun (char *str[],int n)
好的,以下是一个实现n个字符串按字典顺序排序的示例代码:
```
#include <stdio.h>
#include <string.h>
void fun(char *str[], int n) {
int i, j;
char *temp;
// 使用冒泡排序算法
for (i = 0; i < n-1; i++) {
for (j = 0; j < n-i-1; j++) {
if (strcmp(str[j], str[j+1]) > 0) {
// 交换两个字符串的位置
temp = str[j];
str[j] = str[j+1];
str[j+1] = temp;
}
}
}
}
int main() {
char *str[] = {"apple", "banana", "orange", "grape", "kiwi"};
int n = sizeof(str) / sizeof(str[0]);
fun(str, n);
printf("排序后的字符串数组:\n");
for (int i = 0; i < n; i++) {
printf("%s ", str[i]);
}
printf("\n");
return 0;
}
```
在这个示例代码中,我们使用了冒泡排序算法对字符串数组进行排序。在排序过程中,我们使用了 `strcmp()` 函数来比较两个字符串的大小关系,并使用指针变量 `temp` 来交换两个字符串在数组中的位置。最后,我们输出排好序的字符串数组。
请注意,这个示例代码中的字符串数组是在程序中直接定义的。如果您需要从用户输入中获取字符串数据,请使用 `scanf()` 或其他输入函数来读取数据,并将它们存储在字符串数组中。
请使用while函数将fun函数的内容补充完整,编写出一个C语言程序,对字符串,除首、尾字符外,将其字符按ASCII编码数值降序排列的操作:#include<stdio.h> #include<ctype.h> #include<string.h> #include<conio.h> void wwjt(); void fun(char *a,int len) { int i,j; char t; } main(){ char s[100]; int l; printf("输入字符串:"); gets(s); l=strlen(s); fun(s,l); printf("\n%s\n",s); wwjt(); }
#include<stdio.h>
#include<ctype.h>
#include<string.h>
#include<conio.h>
void wwjt();
void fun(char *a, int len) {
int i, j;
char t;
i = 1;
j = len - 2; //除去首尾字符后的最后一个字符的下标
while (i < j) {
while (i < j && a[i] > a[j]) {
t = a[i];
a[i] = a[j];
a[j] = t;
i++;
j--;
}
i++;
j--;
}
}
int main() {
char s[100];
int l;
printf("输入字符串:");
gets(s);
l = strlen(s);
fun(s, l);
printf("\n%s\n", s);
wwjt();
return 0;
}
void wwjt() {
printf("我爱计算机网络课程设计!\n");
}
相关推荐
![application/x-zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)