char str[]="SSSWLIA",c; int k; for(k=2;(c=str[k])!='\0';k++) { switch(c) { case'I':++k;break; case 'L':continue; default:putchar(c);continue; } putchar('*'); }这段程序用C语言函数封装
时间: 2023-07-15 12:15:06 浏览: 235
以下是使用C语言函数进行封装的代码:
```c
#include <stdio.h>
#include <string.h>
void processString(char str[]) {
char c;
int k;
for(k=2;(c=str[k])!='\0';k++) {
switch(c) {
case 'I':
++k;
break;
case 'L':
continue;
default:
putchar(c);
continue;
}
putchar('*');
}
}
int main() {
char str[] = "SSSWLIA";
processString(str);
return 0;
}
```
在此代码中,我们将处理字符串的代码封装在`processString`函数中。该函数接受一个字符数组作为参数,并在其中执行字符串处理操作。然后在`main`函数中调用`processString`函数,并将需要处理的字符串传递给它。
相关问题
void a(char *p,chart,int l){charstr=NULL;str=(char)malloc(l);if(str!=NULL){strncpy(str,t,l));*p=str;}}
这段代码存在一些语法错误,可能会导致编译错误。下面是修正后的代码:
```c
void a(char *p, char *t, int l) {
char *str = NULL;
str = (char*)malloc(l);
if(str != NULL) {
strncpy(str, t, l);
*p = *str;
free(str); // 释放动态分配的内存
}
}
```
该函数的作用是将参数t指向的字符串的前l个字符复制到一个动态分配的字符数组中,然后将该字符数组的第一个字符赋值给*p所指向的变量。由于*p的值取决于函数调用时传入的实参,因此无法确定*p的值。
C语言设计接口函数,char str[] ="geek" 自行封装MyStrlen()
您可以封装一个名为MyStrlen()的函数来计算字符串的长度。以下是一个示例代码:
```c
#include <stdio.h>
int MyStrlen(char str[]) {
int length = 0;
while (str[length] != '\0') {
length++;
}
return length;
}
int main() {
char str[] = "geek";
int len = MyStrlen(str);
printf("Length of the string: %d\n", len);
return 0;
}
```
在这个示例中,函数MyStrlen()使用一个循环来遍历字符数组,直到遇到字符串的结尾字符'\0'为止。每次循环,都会增加length的值,从而计算出字符串的长度。最后,通过调用MyStrlen()函数并打印结果,您可以得到字符串"geek"的长度为4。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)