|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
删除字符,若有相同则删除第一个出现字符。
输入
This is a book.
D s
输出
Thi is a book.
大佬们看下为啥删除不了,错在哪了。。。
- #include<stdio.h>
- #define m 50
- char str[m];
- int main() {
- int n = 0;
- while (1) { // 输入字符串
- scanf("%c", &str[n]);
- if (str[n] == '.')
- break;
- else
- n++;
- }
- char func, str1, str2;
- scanf("%c%c", &func, &str1);
- if (func == 'D') { // 删除字符操作
- int num = 0;
- while (str[num] != str1)
- num++;
- for (num; str[num + 1] != '\0'; num++) {
- str[num] = str[num + 1];
- }
- str[num] = '\0';
- }
- printf("%s", str);
- return 0;
- }
复制代码
- #include <stdio.h>
- #include <string.h>
- #define m 50
- char str[m];
- int main(void)
- {
- int n = 0;
- while (1)
- { // 输入字符串
- scanf("%c", &str[n]);
- if (str[n] == '.')
- break;
- else
- n++;
- }
- char func, str1, str2;
- //清空输入缓冲区,否则下次输入D s,func得到\n,str1得到D
- while (getchar() != '\n')
- ;
- //两个%c中间加空格,否则输入D空格s,str1得到空格
- scanf("%c %c", &func, &str1);
- if (func == 'D')
- { // 删除字符操作
- int num = 0;
- while (str[num] != str1)
- num++;
- for (num; str[num + 1] != '\0'; num++)
- {
- str[num] = str[num + 1];
- }
- str[num] = '\0';
- }
- printf("%s", str);
- return 0;
- }
复制代码
|
|