C语言中 如何把字符串中某一位清空? c语言中怎样写删除字符串中某个字符的程序啊?

\u5728C\u8bed\u8a00\u4e2d\u5982\u4f55\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u7684\u67d0\u4e00\u4e2a\u7279\u5b9a\u7684\u5b57\u7b26

\u627e\u5230\u8981\u5220\u9664\u7684\u5b57\u7b26\u540e,\u89c1\u6b64\u5b57\u7b26\u4e4b\u540e\u7684\u5b57\u7b26\u5168\u90e8\u524d\u79fb\u4e00\u4f4d,\u4e5f\u5c31\u5b9e\u73b0\u4e86\u5220\u9664\u8fd9\u4e2a\u5b57\u7b26\u7684\u76ee\u7684
\u7136\u540e\u518d\u4ece\u4e0b\u4e00\u4e2a\u5b57\u7b26\u5f00\u59cb\u5bfb\u627e\u8981\u5220\u9664\u7684\u5b57\u7b26,\u5982\u6b64\u53cd\u590d\u76f4\u81f3\u7ed3\u675f,\u5c31\u5b9e\u73b0\u4e86\u5c06\u5b57\u7b26\u4e32\u4e2d\u6240\u6709\u7684ch\u5b57\u7b26\u5220\u9664

c\u8bed\u8a00\u4e2d\u4e66\u5199\u5220\u9664\u5b57\u7b26\u4e32\u4e2d\u67d0\u4e2a\u5b57\u7b26\u7684\u7a0b\u5e8f\u7684\u5177\u4f53\u64cd\u4f5c\u6b65\u9aa4\u5982\u4e0b\uff1a
1\u3001\u7f16\u5199\u5934\u51fd\u6570\uff0c\u5305\u62ec\u201c#include .\u201d\u4e0e\u201c#include .\u201c\u3002

2\u3001\u5b9a\u4e49\u4e24\u4e2a\u76f8\u540c\u5bb9\u91cf\u7684\u5b57\u7b26\u4e32\u50a8\u5b58\u7684\u53d8\u91cf char a\u3010100\u3011; char b\u3010100\u3011\u3002\u4ee5\u53ca\u6574\u6570\u53d8\u91cf int c\uff1bint i=0\uff0cj=0\uff1b\u5176\u4e2da[100]\u662f\u5b58\u5b57\u7b26\u4e32\u7684\uff0cb\u3010100\u3011\u662f\u5b58\u4f60\u5220\u9664\u5bf9\u5e94\u5b57\u7b26\u7684\u5b57\u7b26\u4e32\u7684\uff0c\u6574\u6570C\u662f\u60f3\u5220\u9664\u7b2c\u51e0\u4f4d\u3002j \uff0ci \u662f\u540e\u9762for\u5faa\u574f\u4e2d\u8981\u4f7f\u7528\u5230\u7684\u53d8\u91cf\u3002

3\u3001\u7f16\u5199printf\uff08\u201c\u8bf7\u8f93\u5165\u4f60\u7684\u5b57\u7b26\u4e32\u201d\uff09\uff1bscanf\uff08\u201c%s\u201d\uff0ca\uff09\uff1bprintf\uff08\u201c\u8bf7\u8f93\u5165\u4f60\u60f3\u8981\u5220\u9664\u7b2c\u51e0\u4f4d\u5b57\u7b26\u201d\uff09\uff1bscanf\uff08\u201c%d\u201d\uff0c&c\uff09\u3002
\u8fd9\u51e0\u884c\u4ee3\u7801\u4e3b\u8981\u662f\u663e\u793a\u754c\u9762\uff0c\u4ee5\u53ca\u5177\u4f53\u60f3\u8981\u5220\u9664\u4ec0\u4e48\u5b57\u7b26\u4e32\u4ee5\u53ca\u5177\u4f53\u5220\u9664\u7b2c\u51e0\u4f4d\u5b57\u7b26\u4f7f\u7528\u7684\u3002

4\u3001\u7ee7\u7eed\u7f16\u5199for\uff08i=0\uff1bi<strlen\uff08a\uff09\uff1bi++\uff09 \uff08 if\uff08i+1!=c\uff09{ b\u3010j\u3011=a\u3010i\u3011\uff1b j++\uff1b\uff09\uff08 printf\uff08" %s"\uff0cb\uff09\uff1b\uff09\u3002
\u8fd9\u51e0\u884c\u4ee3\u7801\u903b\u8f91\u8bed\u53e5\u662f\uff1a\u53ea\u8981\u5f53i+1\u4e0d\u7b49\u4e8ec\u65f6\u628aa\u3010i\u3011\u91cc\u9762\u7684\u5b57\u7b26\u8d4b\u7ed9b\u3010i\u3011\uff0c\u90a3\u4e48\u8fd9\u6837\u5f53i+1=c\u65f6\u4e0d\u628a\u5b83\u8d4b\u7ed9b\u3010i\u3011\u4ece\u800c\u5b9e\u73b0\u5220\u9664\u529f\u80fd\u3002

5\u3001\u7a0b\u5e8f\u7f16\u8f91\u5b8c\u6bd5\uff0c\u53ef\u4ee5\u8fdb\u884c\u5220\u9664\u5b57\u7b26\u4e86\u3002

假设这个一维字符串数组是str[80]----不一定是一维字符串数组,多维数组,结构都可以。

方法1:使用运行库函数memset():
memset(str, 0, sizeof(str));

方法2:使用Windows API函数ZeroMemory():
ZeroMemory(str, sizeof(str));

但不能用于指针。指针的情况下,必须这样:
struct mystr *p;
...
memset(p, 0, sizeof(struct mystr));
或:
ZeroMemory(p, sizeof(struct mystr));


找到A中B的子串。。然后把后面的覆盖过来。。

#include "stdio.h"
#define MAXSIZE 50
void DeleteStr(char a[],char str[]);
int GetLength(char a[]);

int main()
{
char a[MAXSIZE];
char b[MAXSIZE];
scanf("%s",a);
scanf("%s",b);
DeleteStr(a,b);
printf("%s\n",a);
return 0;
}

int GetLength(char a[])
{
char *p = a;
int len = 0;
while (*p !='\0' )
{
len++;
p++;
}
return len;
}

void DeleteStr(char a[],char str[])
{
int n = 0;
int i = 0;
int len_a = GetLength(a);
int len_str = GetLength(str);
while (n < len_a)
{
while (a[n+i] == str[i] && str[i] != '\0')
{
i++;
}
if (str[i] == '\0')
{
int k = n;
while(1)
{
a[k] = a[k+len_str];
if (a[k] == '\0')
{
break;
}
k++;
}
i = 0;
}
n++;
}
}

#include <stdio.h>
#include <stdlib.h>
int main()
{
char s[]="123abcZXY",b;
int i,j,L,big=0;
printf("orig string: %s\n",s);
L = strlen(s);
for (i=1;i<L;i++) if (s[i] > s[big] ) big=i;
b=s[big];
for (i=big;i>0;i--) s[i]=s[i-1];
s[0]=b;
printf("results: %s\n",s);
return 0;
}

--
orig string: 123abcZXY
results: c123abZXY


char *c;
char *d;
c = strstr(A,B);
if (NULL != c)
{
d = c+strlen(B)-1;
strcpy(c,d);
}

扩展阅读:c语言字符对照表 ... c语言字符拼接在一起 ... c语言 字符串怎么声明 ... c语言截取字符串后2位 ... c截取字符串中特定部分 ... c语言合并两个字符串 ... 修改字符串的某一位 ... 字符串长度怎么自己数 ... c语言字符串连接不用函数 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网