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 ...
Few problems ... Algorithm and Data Structure
Started by nt_virus, Feb 10 2008 06:11 AM
12 replies to this topic
#1
Posted 10 February 2008 - 06:11 AM
|
|
|
#2
Guest_Jordan_*
Posted 10 February 2008 - 07:29 AM
Guest_Jordan_*
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
Posted 10 February 2008 - 08:31 PM
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
Posted 10 February 2008 - 09:07 PM
I just now modified your code a bit and prepared a c# program..
Now .. when i run this program it works absolutely fine .. just a problem ..
when substring not found it shows ..
I only need to display ... substring not found .. not the first line .. please check .. ;) :)
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();
}
}
}
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
Substring not found
I only need to display ... substring not found .. not the first line .. please check .. ;) :)
Attached Files
#5
Posted 11 February 2008 - 12:07 AM
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
Posted 12 February 2008 - 04:49 AM
@ 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 ...
check below attachment for exe...
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();
}
}
}
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
#7
Posted 12 February 2008 - 09:55 PM
:( ... 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_*
Posted 13 February 2008 - 05:11 AM
Guest_Jordan_*
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
Posted 13 February 2008 - 05:15 AM
Sorry, i don't have any more words to say except this --- YOU ARE GENIUS !!!
#10
Posted 13 February 2008 - 05:35 AM
Please remove case-sensitiveness .. i don't know how to do ...
#11
Guest_Jordan_*
Posted 13 February 2008 - 07:07 AM
Guest_Jordan_*
To remove case sensitivity convert your strings to all lowercase or all uppercase before your search statement.
Add that after
// Make the strings lowercase myString = myString.ToLower(); searchString = searchString.ToLower();
Add that after
string searchString = Console.ReadLine();
#12
Posted 13 February 2008 - 07:19 AM
Absolutely fine mate .... Thanks a lot ..


Sign In
Create Account


Back to top










