1168_字符串的查找刪除

// 1168_字符串的查找刪除.cpp : 定義控制檯應用程序的入口點。
//題目1168:字符串的查找刪除
//時間限制:1 秒內存限制:32 兆特殊判題:否提交:5244解決:2155
//題目描述:
//給定一個短字符串(不含空格),再給定若干字符串,在這些字符串中刪除所含有的短字符串。
//輸入:
//輸入只有1組數據。
//輸入一個短字符串(不含空格),再輸入若干字符串直到文件結束爲止。
//輸出:
//刪除輸入的短字符串(不區分大小寫)並去掉空格,輸出。
//樣例輸入:
//in
//#include 
//int main()
//{
//
//  printf(" Hi ");
//}
//樣例輸出:
//#clude
//tma()
//{
//
//  prtf("Hi");
//}
//提示:
//注:將字符串中的In、IN、iN、in刪除。

#include "stdafx.h"
#include "stdio.h"
#include "iostream"
#include "string"
#include "algorithm"
using namespace std;

int main()
{
    string a,b;
    char temp[1000];
    cin>>a;
    transform(a.begin(),a.end(),a.begin(),::toupper);
    getchar();
    while(getline(cin,b)){
        int j = 0;      
        for(int i = 0;i<b.length();i++){
            if(b[i]==' ')
                continue;
            else
                temp[j++] = b[i];
        }
        temp[j] = '\0';
        string c(temp);
        string d = c;
        transform(c.begin(),c.end(),c.begin(),::toupper);
        for(int i = 0;i<=((int)c.length()-(int)a.length());i++){
            int x = c.length() - a.length();
            if(c.substr(i,a.length()) == a){
                d.erase(i,a.length());
                c.erase(i,a.length());
                i--;
            }
        }
        cout<<d<<endl;
    }
    return 0;
}

/*

1.transform(a.begin(),a.end(),a.begin(),::toupper);對string型轉化爲大寫字母,
  如果是char *則應對每個字母用toupper函數

2.string類型的length()函數返回的是無符號數,
  所以若 a = "xy" ,b = "x",則b.length() - a.length() 答案不是-1

3.string的erase函數

4.其實可以直接用str的find函數:
  string st2("aabcbcabcbabcc");
  string str1("abc");
  cout << st2.find(str1, 2) << endl;

*/


發佈了60 篇原創文章 · 獲贊 0 · 訪問量 1萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章