Thread: how can i return the first position of the first occurrence of a substring in a strin

  1. #1
    Registered User
    Join Date
    Jun 2016
    Posts
    3

    how can i return the first position of the first occurrence of a substring in a strin

    Code:
      public static int StringMatch(string baseStr, string compStr)        {
                //bool b = false;
                int count = 0;
                //int index = 0;
                for (int i = 0; i < baseStr.Length; i++)
                {
    
    
                    for (int j = 0; j < compStr.Length; j++)
                    {
    
    
                        if (baseStr[i] == compStr[j])
                        {
                            count++;
    
    
                        }
                        if (count == compStr.Length)
                        {
                            return i - compStr.Length + 1;
    
    
                        }
    
    
                    }
    
    
                }
                return -1;
    
    
            }
    
    
            static void Main(string[] args)
            {
    
    
                string txt = "dhgrogood";
                string phrase = "good";
                int res = StringMatch(txt, phrase);
                Console.WriteLine(res);
            }
        }
    }

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Didn't you ask this exact same question last week?

    You can just use string.IndexOf():

    Code:
    int res = txt.IndexOf(phrase);
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jun 2016
    Posts
    3
    yes, but i want to implement this method by hand

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well you need to reset count every time you start again.

    You also need only one for loop.

    You can cut that loop short at baseStr.Length - compStr.Length
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2016
    Posts
    3
    i want to return i when basestring contains substring and i-compStr.Length+1 will be correct

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-23-2016, 05:11 PM
  2. return array position from function
    By danieldcc in forum C Programming
    Replies: 2
    Last Post: 10-10-2011, 11:46 AM
  3. extracting a substring based on position
    By doubty in forum C Programming
    Replies: 2
    Last Post: 06-22-2009, 01:06 AM
  4. Replies: 4
    Last Post: 05-08-2009, 04:25 AM
  5. Count occurrence in bin tree
    By ronenk in forum C Programming
    Replies: 2
    Last Post: 12-24-2004, 11:35 AM

Tags for this Thread