Problem of the Day
A new programming or logic puzzle every Mon-Fri

String Replace

Welcome back to another fantastic Monday!

Today's objective is to write a method that takes a given string and replaces all occurrences of that string with another string and returns the number of replacements made. For instance, given the string "problem of the day" and a replacement value of " ", the final result would be "problem-of-the-day" with a return value of 3. If your language of choice doesn't support pass by reference of strings feel free to return multiple values.

Permalink: http://problemotd.com/problem/string-replace/

Comments:

  • Nick Krichevsky - 10 years, 6 months ago

    C++ solution using the magic of pointers!

    #include <iostream>
    #include <string>
    
    using namespace std;
    int stringReplace(string &s,char replace, char replacment);
    int main(){
        string q = "Hello!!!";
        int j = stringReplace(q,'!','.');
        cout<<q<<endl;
        cout<<"Replaced "<<j<<" chars."<<endl;
    }
    
    int stringReplace(string &s,char replace, char replacment){
        int replaced = 0;
        for (int i=0; i<s.length(); i++){
            char c = s.c_str()[i];
            if (c==replace){
                s[i]=replacment;
                replaced++;
            }
        }
        return replaced;
    }
    

    reply permalink

  • Anonymous - 10 years, 6 months ago

    #python 2.7
    def count_replace(sentence, replacestring):
        a = sentence.count(replacestring)
        b = sentence.replace(replacestring, "-")
        print b
        return a
    

    reply permalink

  • Mre64 - 10 years, 6 months ago

    /* Mre64 6/16/14

    ''If I were a single man, I might ask that mummy out. That's a good-looking mummy.''
        -Bill Clinton, mummy exhibit at the National Geographic museum
    

    */ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections;

    public class ReplaceString {

    public static void main(String[] args) {
    //the sample string
    String str = "this is is is the string to replace is";
    //call replace function
    findAndReplaceString(str, "is", "not is");
    }
    private static void findAndReplaceString(String str,String toFind, String toReplace){
        //add contents to an arrayList
        ArrayList <String> myList =  new ArrayList<String>(Arrays.asList(str.split(" ")));
        //call the replace method
        replace(myList, toFind, toReplace);
    }
    private static void replace(ArrayList list, String toFind, String toReplace){
            // see if the word your looking for exists
            if(list.contains(toFind)){
                int freq = Collections.frequency(list, toFind);
                Collections.replaceAll(list, toFind, toReplace);
            System.out.println(list.toString() + " frequency: " + freq);
            }else{
                System.out.println("404 NOT FOUND");
        }
    }
    

    }

    reply permalink

  • Anonymous - 10 years, 6 months ago

    #python 2.7
    def str_replace(string,replace,substitute):
        return ( substitute.join(string.split(replace)) , len(string.split(replace))-1 )
    print str_replace("problem of the day"," ","-")
    

    reply permalink

Content curated by @MaxBurstein