Home > OS >  Check if a word exists in another string in c# without using any inbuilt function
Check if a word exists in another string in c# without using any inbuilt function

Time:02-10

string sentence = "This is a goodday";
string word = "good";

I know this can be done with .Contains() method. I was asked in an interview how to do it without contains method.

CodePudding user response:

how to do it in english.

pick the first letter of word.
walk down sentence a character at a time till you find that letter
now look at the next letters in sentence to see if they are the rest of that word
yes - done
no - keep going

the thing to use is that word[x] is the x-1th character of word, so you just need 2 indexes and a loop or 2

CodePudding user response:

Q: Check if a word exists in another string in c# without using any inbuilt function
A: Tricky

It depends on how detailed that "any inbuilt function" really is.

In general the algorithm is simple:

Loop through the string you're searching in
    for each position, see if you've found the word

    you do this by looping through all the characters in what we're looking for
        and compare each character from the first string with one from the second
    if they all matched, we've found a match

but then ... "without using any inbuilt function".

I assume this would mean, do not use the obvious ones, such as Contains, IndexOf, a regular expression, all those things.

But taken to the extreme, does that mean I cannot even know how long the strings are? Is s.Length a built-in function? And thus not allowed?

public bool Contains(string value, string whatWereLookingFor)
{
    return IndexOf(value, whatWereLookingFor) >= 0;
}

public int Length(string s)
{
    int result = 0;
    for (int i = 0; i <= 2147483647; i  )
    {
        try
        {
            char c = s[i];
        }
        catch (IndexOutOfRangeException)
        {
            break;
        }
        result = i   1;
    }
    
    return result;
}

public int IndexOf(string value, string whatWereLookingFor)
{
    int iMax = Length(value);
    int whatMax = Length(whatWereLookingFor);
    for (int i = 0; i <= iMax - whatMax; i  )
    {
        bool isMatch = true;
        for (int j = 0; j < whatMax; j  )
        {
            if (value[i   j] != whatWereLookingFor[j])
            {
                isMatch = false;
                break;
            }
        }
        if (isMatch)
            return i;
    }
    return -1;
}

CodePudding user response:

How about using a for loop? Loop through the sentence checking for the first character of the work. Once you find that check for the next character of the word until you find the whole word (done - word exists in sentence) or a different character so you start again looking for the first character of the word until you run out of characters.

As the comments say, it would have been nicer to let us know what you answered.

CodePudding user response:

try this:

    static bool checkString(string inputString="",string word="")
    {      
            bool returV=false;            
            if(inputString =="" ||  word=="")
                return false;
            int intexcS=0;    
            Dictionary<int,string> d = new Dictionary<int, string>();
            foreach (char cW in word)
            {
                foreach (char cS in inputString)
                {                      
                    if(cW==cS){                        
                        if(!d.ContainsKey(intexcS) && d.Count<word.Length){
                            d.Add(intexcS,cW.ToString());  
                        }
                    }
                intexcS  ;
                }
                intexcS=0;
            }
            int i=0;
            foreach(var iitem in d) { if(iitem.Value!=word[i].ToString()) returV=false; else returV=true; i  ;  }
            return returV;
    }
  • Related