#include<stdio.h> #include<string.h> void fun1(char * s) { char temp[100]; int i,j; char *p=s; j=0; for(i=0;p[i]!='\0';i++) { if(p[i]>='0' && p[i]<='9') { continue; } temp[j]=p[i]; j++; } temp[j]='\0'; strcpy(s,temp); }解释一下
时间: 2024-03-08 08:45:15 浏览: 21
这是一个函数定义,函数名为`fun1`,接收一个参数为字符串指针类型的`s`。该函数的作用是将字符串`s`中的数字字符去掉,将其他字符存入一个临时的字符数组`temp`中,最后再将`temp`中的内容复制回`s`中。
函数内部定义了一个临时的字符数组`temp`,容量为100,用于存储去掉数字字符后的字符串。定义了两个整型变量`i`和`j`,其中`i`用于循环遍历`s`中的每一个字符,`j`表示在`temp`中存储的字符的个数。
在循环中,首先定义了一个字符指针`p`,指向`s`的开头,然后对`s`中的每一个字符进行判断。如果该字符是数字字符,则使用`continue`跳过该字符继续下一个循环;否则将该字符存入`temp`中,并将`j`的值加1。
最后,将`temp`中最后一个字符的下一个位置赋为`\0`,表示字符串结束。然后使用`strcpy`函数将`temp`中的内容复制回`s`中。
该函数可以用于去掉字符串中的数字字符,返回一个去掉数字字符的新字符串。
相关问题
*定义函数实现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()` 或其他输入函数来读取数据,并将它们存储在字符串数组中。
#include <stdio.h> #include <string.h> void fun(char *s,int num) { } void main() { char s[10]; gets(s); fun(s,7); printf("%s", s); }
这是一道需要填空的题目,题目思路是将字符串 `s` 中的前 `num` 个字符反转。可以使用两个指针分别指向字符串的首位和末位,依次交换两个指针所指向的字符,直到两个指针相遇为止。具体实现可以这样写:
```c
#include <stdio.h>
#include <string.h>
void fun(char *s, int num) {
int i = 0, j = num - 1;
while (i < j) {
char temp = s[i];
s[i] = s[j];
s[j] = temp;
i++;
j--;
}
}
void main() {
char s[10];
gets(s);
fun(s, 7);
printf("%s", s);
}
```
其中,`i` 和 `j` 分别表示指向字符串的首位和末位的指针,`temp` 是一个临时变量,用于交换两个指针所指向的字符。在循环中,每次将指针向中间移动一位,并交换两个指针所指向的字符,直到两个指针相遇为止。最终输出交换后的字符串即可。
相关推荐
![c](https://img-home.csdnimg.cn/images/20210720083646.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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)