C语⾔:在字符串中删除与某字符相同的字符⽤字符指针作函数参数编程实现如下功能:在字符串中删除与某字符相同的字符。
**输⼊格式要求:"%s"
输⼊提⽰信息:
"Input a string:"
"Input a character:"
**输出格式要求:"Results:%s\n"
程序运⾏⽰例1如下:
Input a string:hello,world!
Input a character:o
Results:hell,wrld!
1 #include <stdio.h>
2 #include <string.h>
3#define N 100
4void  Squeeze(char *s, char c);
5int main()
6 {
7char  str[20], ch;
8    printf("Input a string:");
9    gets(str);
10    printf("Input a character:");
11    ch = getchar();
12    Squeeze(str,ch);
字段字符串去重复13    printf("Results:%s\n", str);
14return0;
15 }
16void  Squeeze(char *s, char c)
17 {
18int i,j,len;
19    len = strlen(s);
20for (i = len; i>=0; i--)
21    {
22if (s[i] == c)
23        {
24for (j = i; j < len; j++)
25            {
26                s[j] = s[j + 1];
27            }
28        }
29    }
30 }
做法⼀
1 #include <stdio.h>
2 #include <string.h>
3#define N 100
4void  Squeeze(char *s, char c);
5int main()
6 {
7char  str[20], ch;
8    printf("Input a string:");
9    gets(str);
10    printf("Input a character:");
11    ch = getchar();
12    Squeeze(str, ch);
13    printf("Results:%s\n", str);
14return0;
15 }
16void  Squeeze(char *s, char c)
17 {
18char str[N];
19char *t = str;
20    strcpy(t, s);
21for (; *t != '\0'; t++)
22    {
23if (*t != c)
24        {
25            *s = *t;
26            s++;
27        }
28    }
29    *s = '\0';  /* 在字符串t2的末尾添加字符串结束标志 */
30 }
做法⼆

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。