Click here to Skip to main content
15,610,517 members
Please Sign up or sign in to vote.
1.50/5 (2 votes)
See more:
Hello friends,
I tried below given code it is working fine with all strings(like "here is her home" trying to find "he") But when i am trying with "anana is an banana" and trying to find "ana" this is throwing exception. Please help my in this.

What I have tried:

C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace StringTest
{
    class Program
    {
        static void Main(string[] args)
        {          
            int count = 0, count1 = 0;
            int i, j, l1, l2; 
            Console.WriteLine("Enter a string : ");
            string str=Console.ReadLine();
            Console.WriteLine("Enter a string : ");
            string sub=Console.ReadLine(); 
            l1 = str.Length; l2 = sub.Length;
            for (i = 0; i < l1;)
            {
                j = 0;
                count = 0;                             
                    while ( (j<l2)>
                    {                        
                            count++;                        
                            i++;                      
                            j++;                        
                    }
                if (count == l2)
                {
                    count1++;                                   
                    count = 0;
                }
                else
                i++;
            }    
            Console.WriteLine("{0} occurs {1} times in {2}", sub, count1, str);
            Console.ReadLine();
       }   
   }
}
Posted
Updated 7-May-16 16:21pm
v3
Comments
Sergey Alexandrovich Kryukov 7-May-16 18:17pm    
All those "using" tells the tail: dirty work. Never write what you are not actually using.
Another sign of dirty work is that you never defined a single method, except Main. You did not even try to define a function corresponding to your question.
With your lack of care, it's hard to hope for good results.
Next time, please also format the code sample in your questions.
—SA
PIEBALDconsult 7-May-16 18:39pm    
Regular Expressions.
Patrice T 7-May-16 19:31pm    
I guess the goal is to learn programming
PIEBALDconsult 8-May-16 0:24am    
That's always a worthwhile goal too.

I see a few problems in your code.
But my main worry is that it does nothing !
C#
for (i = 0; i < l1;)
{
    j = 0;
    count = 0;
        while ( (j<l2)>
        {
                count++;
                i++;
                j++;
        }
    if (count == l2)
    {
        count1++;
        count = 0;
    }
    else
    i++;
}

This code is playing with pointers and counters, but that's all.

Use Improve question to update your question.

Question: if you have the input banana and search for ana, how many occurrences should you have ?
Be cause there is 2 possibilities banana and banana, but they overlap.
 
Share this answer
 
v2
Comments
ridoy 8-May-16 13:38pm    
5ed.
Try this,
made some corrections, check inline comments

C#
using System;
namespace StringTest
{
    class Program
    {
        static void Main(string[] args)
        {
            // always define the variable names more understandable. it will be easier for debugging
            int occurances = 0;
            int inputLength, findLength;
            Console.WriteLine("Enter a string : ");
            string input = Console.ReadLine();
            Console.WriteLine("Enter a string to find : ");
            string find = Console.ReadLine();
            inputLength = input.Length; findLength = find.Length;

            // if you have only one line of code, next to 'for' and 'if' statements, its not mandatory to use '{','}' brackets as below.

            for (int i = 0; i < inputLength; i++)  // loop each character from the input string
                if (inputLength >= i + findLength)  // check the input length is greater than equal to loop index + find key length (validation, to avoid index exception in the next line )
                    if (find == input.Substring(i, findLength))  // match the finding key with the current instance (index + key length)
                        occurances++; // if the condition satisfies , do increament 


            Console.WriteLine("'{0}' occurs {1} times in '{2}'", find, occurances, input);
            Console.ReadLine();
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900