请编写函数fun,其功能是:将s所指字符串中下标为偶数的字符删除,串中剩余字符形成新串放在t所指数组中。 #include <stdio.h> #include <string.h> void fun( char *s, char t[]) { } int main () { char s[100], t[100]; printf("\nPlease enter string S:"); scanf("%s", s); fun(s,t); printf("\nThe result is :%s\n", t); return 0; }
时间: 2024-02-23 07:57:51 浏览: 33
好的,下面是函数fun的实现:
```c
#include <stdio.h>
#include <string.h>
void fun(char *s, char t[]) {
int len = strlen(s);
int j = 0; // 记录新串t的下标
for (int i = 0; i < len; i += 2) {
t[j++] = s[i];
}
t[j] = '\0'; // 注意添加字符串结尾标志
}
int main() {
char s[100], t[100];
printf("\nPlease enter string S:");
scanf("%s", s);
fun(s, t);
printf("\nThe result is :%s\n", t);
return 0;
}
```
函数fun的实现思路是:遍历原串s,将下标为偶数的字符复制到新串t中,最后在新串t的末尾添加字符串结尾标志'\0'。
相关问题
函数fun的功能是:将s所指字符串中下标为偶数的字符删除,剩余字符形成的新串放在t所指数组中
以下是函数fun的C语言代码实现:
```c
void fun(char *s, char *t) {
int i = 0, j = 0;
while (s[i] != '\0') {
if (i % 2 == 1) {
t[j++] = s[i];
}
i++;
}
t[j] = '\0';
}
```
函数中使用了两个指针i和j分别指向字符串s和数组t的下标。在while循环中,如果i是奇数下标,则将s[i]复制到t[j]中,j自增1。最后在t[j]处添加字符串结束符'\0'。
用c语言函数fun的功能是:将s所指字符串中下标为偶数同时ASCII值为奇数的字符删除,s所指串中剩余的字符形成的新串放在t所指的数组中。
以下是实现该功能的C语言函数fun的代码:
```c
void fun(char *s, char *t) {
int i, j;
for (i = 0, j = 0; s[i] != '\0'; i++) {
if (i % 2 == 0 && s[i] % 2 == 1) {
continue;
}
t[j++] = s[i];
}
t[j] = '\0';
}
```
该函数接受两个参数,s和t,分别表示原始字符串和新的字符串。函数遍历原始字符串中的每个字符,如果其下标是偶数且ASCII码是奇数,则跳过该字符,否则将其添加到新的字符串中。最后在新的字符串的末尾加上字符串结束符'\0'。注意,该函数不会改变原始字符串s的值,只会生成一个新的字符串t。