首页 > 试题广场 >

字符串去特定字符

[编程题]字符串去特定字符
  • 热度指数:9842 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 64M,其他语言128M
  • 算法知识视频讲解
输入字符串s和字符c,要求去掉s中所有的c字符,并输出结果。

输入描述:
测试数据有多组,每组输入字符串s和字符c。


输出描述:
对于每组输入,输出去除c字符后的结果。
示例1

输入

heallo
a

输出

hello
while True:
    try:
        print(input().replace(input(),""))
    except:
        break

一行搞定。

编辑于 2017-09-12 15:34:34 回复(1)
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
//关键是那个string的函数要记住
using namespace std;
int main(){
    string s;
    char c;
    cin>>s;
    cin>>c;
    s.erase(remove(s.begin(),s.end(),c),s.end());
    cout<<s<<endl;
    return 0;
}

发表于 2019-02-16 08:46:41 回复(1)
#include<iostream>
#include<string>
using namespace std;
int main()
{
	string str;
	char s;
	while(cin>>str>>s)
	{
		for(int i=0;i<str.size();i++)
		{
			if(str[i]!=s)
				cout<<str[i];
		}
              cout<<endl;
	}
	return 0;
}


发表于 2017-06-23 11:08:46 回复(0)
import java.util.Scanner;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        while(sc.hasNext()){
            String str = sc.nextLine();
            String s = sc.nextLine();
            System.out.print(str.replaceAll(s,""));
        }
    }
}

发表于 2018-08-03 04:18:59 回复(0)
#include <iostream>
#include <string>
using namespace std;

int main()
{
    string tmp;
    while(cin >> tmp)
    {
        char c;
        cin >> c;
        int i;
        for(i=0;i<tmp.size();)
            if(tmp[i] == c)
                tmp.erase(tmp.begin()+i);
            else ++i;
        cout << tmp << endl;
    }
    return 0;
}
发表于 2019-04-20 09:53:15 回复(0)
#include<stdio.h>//循环前移即可
#include<string.h>
int main()
{
    char a[100],m;
    scanf("%s %c",a,&m);
    int n=strlen(a);
    for(int i=0;i<=n;i++)//包含\0
        if(m==a[i])//循环前移
        {
            for(int j=i;j<=n;j++)//把\0也前移
                a[j]=a[j+1];
            n--;//数组长度减一
            i--;//从刚才的位置重新遍历
        }
    printf("%s",a);
}

编辑于 2020-04-08 15:23:59 回复(0)
Java
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        String s = scanner.next();
        String r = scanner.next();
        System.out.println(s.replace(r, ""));
    }
}


编辑于 2020-03-19 13:27:35 回复(0)
#include<bits/stdc++.h>

using namespace std;

int main()
{
    char a[1000];
    cin.getline(a, 1000);
    int len = strlen(a);
    char b;
    cin >> b;
    for(int i = 0; i < len; i++)
    {
        if(a[i] == b)
        {
            continue;
        }
        else
            cout << a[i];
    }
    cout << endl;
}

发表于 2020-02-07 16:55:33 回复(0)
#include<bits/stdc++.h>
using namespace std;
int main(){
    char ch[500],c;
    while(cin>>ch){
        cin>>c;
        for(int i=0;i<strlen(ch);i++)
            if(ch[i]!=c)
                cout<<ch[i];
        cout<<endl;
    }
}

发表于 2020-01-13 18:47:39 回复(0)

#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
using namespace std;
string str;
string c;
int main()
{
        while(cin>>str)
        {
            cin>>c;
            while(str.find(c)!=-1)
            {
                int pos=str.find(c);
                str.erase(str.begin()+pos,str.begin()+pos+1);
            }
            cout<<str<<endl;
        }
}
 
发表于 2019-03-26 11:47:00 回复(0)
#include<stdio.h>
#include<string.h>

int main(){
    const int N = 100;
    char a[N];char del;
    scanf("%s",a);
    del = getchar();
    scanf("%c",&del);
    int k=0,i=0; // 用k保存要删除的字符数
    while(a[i]!='\0')
    {
        if(a[i] == del) k++;
        else
        {
            a[i-k] = a[i];
        }
        i++;
    }
    a[i-k] = a[i]; //截断字符
    printf("%s\n",a);
    return 0;
}
发表于 2019-02-22 08:58:06 回复(0)

运行时间:38ms
占用内存:10808k

import java.util.Scanner;

/**
 * @author Allen_Hua
 * @create_time 创建时间:May 12, 2018 7:44:47 PM 类说明
 */
public class Main {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);
        while (scan.hasNext()) {
            String str = scan.nextLine();
            // charAt(0)获取键盘输入的单个char
            char c = scan.next().charAt(0);
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < str.length(); i++) {
                if (str.charAt(i) != c) {
                    sb.append(str.charAt(i));
                }
            }
            System.out.println(sb.toString());
        }
    }
}
发表于 2018-05-12 19:53:56 回复(0)
#include <stdio.h>
#include <string.h>
int main(){
    char s[1000],c,ch;
    int i,len;
    while(scanf("%s",s)!=EOF){
        ch=getchar();
        scanf("%c",&c);
        for(i=0;i<strlen(s);i++){
            if(s[i]!=c)
                printf("%c",s[i]);
        }
        printf("\n");
    }
}

发表于 2018-04-28 09:57:57 回复(0)
#include <iostream>
#include <string.h>
using namespace std;
int main(){
    string str;
    char c;
    while(cin>>str){
        cin>>c;
        for(int i=0;i<str.length();i++){
            if(str[i]==c) continue;
            else cout<<str[i];
        }
        cout<<endl;
    }
}
//注意这种后边要加cin.get()去掉缓冲区内的换行符
#include <iostream>
#include <string.h>
using namespace std;
int main(){
    char str[1000]={""};
    char c;
    while(gets(str)){
        cin>>c;
        for(int i=0;i<strlen(str);i++){
            if(str[i]==c) continue;
            else cout<<str[i];
        }
        cout<<endl;
        cin.get();
    }
}

发表于 2017-12-14 16:13:01 回复(0)
#include<iostream>
#include<cstring>
#define N 10000
using namespace std;
int Delete(char *a,char b)
{
 int k = strlen(a); int n = 0;
 for(int i = 0;i<k;i++)
 {
  if(a[i]==b)
  {
   if(i==k-1){
    k--;
    break;
   }
   else {
    for(int m = i+1;m<k;m++)
    {
     a[m-1] = a[m];
    }
    k--;
    i--;
   }
  }
 }
 return k;
}
int main()
    {
    char a[N];
    while(cin>>a)
    {
     char b;
     cin>>b;
     int k = Delete(a,b);
     for(int n = 0;n<k;n++)cout<<a[n];cout<<endl;
 }
    return 0;
}

发表于 2017-04-30 15:13:17 回复(0)
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in=new Scanner(System.in);
        String str=null;
        String str1; while (in.hasNext())
        {
            str=in.nextLine();
            str1=in.nextLine();
            System.out.println(str.replaceAll(str1,""));
        }
    }
}

发表于 2016-10-29 11:03:37 回复(0)
#include<bits/stdc++.h>
int main(){
    char c,a[1000];
    while(scanf("%s %c ",a,&c)!=EOF){
        for(int i=0;i<strlen(a);i++)
            if(a[i]!=c)//特定字符不输出
                printf("%c",a[i]);
        printf("\n");
    }
}
编辑于 2019-03-19 10:45:52 回复(1)
#include<stdio.h>
int main(){
    char str[3000];
    scanf("%s",str);
    char tmp,c;
    scanf("%c%c",&tmp,&c);
    for(int i=0;str[i]!=0;i++){
       if(str[i]==c) continue;
       else printf("%c",str[i]);
    }
}

编辑于 2024-03-17 16:52:20 回复(0)
#include <iostream>
#include <string>
using namespace std;

int main() {
    string str;
    char c;
    while (cin >> str >> c) {
        int find;
        while ((find = str.find(c)) != -1) {
            str.erase(find, 1);
        }
        cout << str << endl;
    }
    return 0;
}

编辑于 2024-03-04 09:59:04 回复(0)
#include <iostream>
#include<string>
using namespace std;

int main() {
    string str1,str2;
    while (cin >>str1>>str2) {
        while(str1.find(str2)!=string::npos)
        {
            str1.erase(str1.find(str2),1);
        }
        cout<<str1;
    }
}

发表于 2023-03-27 21:08:08 回复(0)