#include <stdio.h> void fun(char s[], char t) { int i = 0; while (s[i]) { if (s[i] == t) s[i] = t - 'a' + 'A'; i++; } } int main() { char str[100] = "abcdefg", c = 'd'; fun(str, c); printf("%s\n", str); return 0; }解答程序
时间: 2024-01-18 10:04:46 浏览: 194
这个程序的功能是将字符串中所有与给定字符相同的小写字母转换为大写字母。具体来说,函数`fun`接受两个参数,一个字符串`s`和一个字符`t`,函数会遍历字符串`s`中的所有字符,如果字符与`t`相同且为小写字母,就将其转换为对应的大写字母。函数`main`中定义了一个字符串`str`和一个字符`c`,将字符串`"abcdefg"`赋值给`str`,将字符`'d'`赋值给`c`,然后调用`fun(str, c)`函数,最后打印输出字符串`s`的值。执行结果为:
```
abCDefg
```
其中,字符`'d'`被转换成了大写字母`'D'`。
相关问题
#include <string.h> #include <stdio.h> #include <ctype.h> void fun(char *p) { int i, t; char c[80]; /********found********/ For (i=0, t=0; p[i]; i++) if (!isspace(*(p+i))) c[t++] = p[i]; /********found********/ c[t] = "\0"; strcpy(p, c); } void main() { char c, s[80]; int i = 0; c = getchar(); while (c != '#') { s[i] = c; i++; c = getchar(); } s[i] = '\0'; fun(s); puts(s); }
这段代码的功能是将输入的字符串中的空格删除,并输出删除后的字符串。但是代码有两处错误:
1. 第一个错误是在第 10 行,`For` 应该改为小写字母 `for`。
2. 第二个错误是在第 13 行,字符串结束符应为 `'\0'` 而不是 `"\0"`。
正确的代码应该是:
```c
#include <string.h>
#include <stdio.h>
#include <ctype.h>
void fun(char *p) {
int i, t;
char c[80];
for (i = 0, t = 0; p[i]; i++) {
if (!isspace(*(p+i))) {
c[t++] = p[i];
}
}
c[t] = '\0';
strcpy(p, c);
}
int main() {
char c, s[80];
int i = 0;
c = getchar();
while (c != '#') {
s[i] = c;
i++;
c = getchar();
}
s[i] = '\0';
fun(s);
puts(s);
return 0;
}
```
请使用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");
}
阅读全文
相关推荐
















