Skip to content

Position of a string

harish reddy

Asked Jan 22, 2018, 1:41 AM EST1 answer
Share
LinkedInX
C#
    //Write a program in C# Sharp to search the position of a substring within a string. Not able to get the output and proceed further.... pls help

            string sentence = Console.ReadLine();
            foreach (string item in sentence)
            {
                Console.WriteLine(item);
            }


            int pos = 1;


            Console.WriteLine("Enter the substring");

            string substring = Console.ReadLine();
char[] substringchar = substring.ToCharArray();

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • +1
    try this below code
    1. string str1;  
    2. string findstr;  
    3. Console.Write("\n\nSearch the position of a substing within a string :\n");  
    4. Console.Write("-------------------------------------------------------\n");  
    5. Console.Write("Input a String: ");  
    6. str1 = Console.ReadLine();  
    7. Console.Write("Input a substring to be found in the string: ");  
    8. findstr = Console.ReadLine();  
    9. int index = str1.IndexOf(findstr);  
    10. if(index<0)  
    11. Console.WriteLine("The substring no found in the given string \n"); else  
    12. Console.WriteLine("Found '{0}' in '{1}' at position {2}", findstr, str1, index);  
    +1