Jump to content

Few problems ... Algorithm and Data Structure

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
12 replies to this topic

#1
nt_virus

nt_virus

    Newbie

  • Members
  • PipPip
  • 12 posts
Hello Mate .. First of all thanks to all helpers here .. This community seems to be a place where i can find my answer ..

I m a beginner and learned basic of c# ... currently doing Algorithm and Data Structures ... I m chapter there is are few problems which sucks my mind and i cant understand what to do ...

In first problem we have to accept two strings and check whether the second strings exists within the first string .. Like .. if 1st string is "concatenation" and the second string is "cat" .. the program should display - Substring found at position 4 ... and if 2nd string is "tent" the it should show .. -- Substring not found ...

I also have to write an algorithm, will you guys please help me ...

#2
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Can you post your code please? It is a fairly simple thing to do:

 string myString = "concatenation"; 
  string searchString = "cat"; 
  int found = myString.IndexOf(SearchString); 
MessageBox.Show("Found at : " + found );


#3
nt_virus

nt_virus

    Newbie

  • Members
  • PipPip
  • 12 posts
Thanks a lot mate .. actually i ain't made the code because i cant understand how to declare variable for that..thanks again mate ..

#4
nt_virus

nt_virus

    Newbie

  • Members
  • PipPip
  • 12 posts
I just now modified your code a bit and prepared a c# program..

Quote

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter your string");
string myString = Console.ReadLine();
Console.WriteLine("Enter your search string");
string searchString = Console.ReadLine();
int found = myString.IndexOf(searchString);
while (true)
{
Console.WriteLine("Substring Found at : " + (found + 1));
break;
}
{
Console.WriteLine("Substring Not Found");
}
Console.ReadLine();
}
}
}

Now .. when i run this program it works absolutely fine .. just a problem ..

when substring not found it shows ..

Quote

Substring found at : 0
Substring not found

I only need to display ... substring not found .. not the first line .. please check .. ;) :)

Attached Files



#5
jireh

jireh

    Newbie

  • Members
  • PipPip
  • 18 posts

if (searchString<>string.Empty)

  {

       Console.WriteLine("Substring Found at : " + (found + 1));

  }

else

 { 

       Console.WriteLine("Substring Not Found");

 }


how baout that, huh?

enjoy coding...

regards,

jireh

#6
nt_virus

nt_virus

    Newbie

  • Members
  • PipPip
  • 12 posts
@ jireh, Thanks for the help mate ... First i think ... this sign .. <> means not equals to .. isn't ? But showing error when i use that sign ... or might be i m missing something ... this sign .. != works fine for not equals to.. anyway ..

From your code... if substring is not found its showing ...

Substring Found at : 0

.....

anyway after few thinking and experiment .. i m able to make this program to work successfully ...

The code for perfect program is ...

Quote

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Enter Main String");
string myString = Console.ReadLine();
Console.WriteLine("Enter SubString");
string searchString = Console.ReadLine();
int found = myString.IndexOf(searchString);
if (found < 1)
{
Console.WriteLine("Substring Not Found");
}
else
{
Console.WriteLine("Substring Found at position : " + (found + 1));
}



Console.ReadLine();

}
}
}

check below attachment for exe...

Attached Files

  • Attached File  x.rar   1.22K   4 downloads


#7
nt_virus

nt_virus

    Newbie

  • Members
  • PipPip
  • 12 posts
:( ... in the above code .. still there is some problem.... if we look for ca in cat ... the substring found at index 0 .. so if loop treating as True and printing substring not found ... please anyone suggest me what to do now .. :(

#8
Guest_Jordan_*

Guest_Jordan_*
  • Guests
Try setting found to less than 0:

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter Main String");
            string myString = Console.ReadLine();
            Console.WriteLine("Enter SubString");
            string searchString = Console.ReadLine();
            int found = -1;
            found = myString.IndexOf(searchString);
            if (found < 0)
            {
                Console.WriteLine("Substring Not Found");
            }
            else
            {
                Console.WriteLine("Substring Found at position : " + (found + 1));
            }

            

            Console.ReadLine();

        }
    }
}


#9
nt_virus

nt_virus

    Newbie

  • Members
  • PipPip
  • 12 posts
Sorry, i don't have any more words to say except this --- YOU ARE GENIUS !!!

#10
nt_virus

nt_virus

    Newbie

  • Members
  • PipPip
  • 12 posts
Please remove case-sensitiveness .. i don't know how to do ...

#11
Guest_Jordan_*

Guest_Jordan_*
  • Guests
To remove case sensitivity convert your strings to all lowercase or all uppercase before your search statement.

 // Make the strings lowercase
myString = myString.ToLower();
searchString = searchString.ToLower();

Add that after
string searchString = Console.ReadLine();


#12
nt_virus

nt_virus

    Newbie

  • Members
  • PipPip
  • 12 posts
Absolutely fine mate .... Thanks a lot ..