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 ...
Can you post your code please? It is a fairly simple thing to do:
Code:string myString = "concatenation"; string searchString = "cat"; int found = myString.IndexOf(SearchString); MessageBox.Show("Found at : " + found );
Thanks a lot mate .. actually i ain't made the code because i cant understand how to declare variable for that..thanks again mate ..
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 ..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();
}
}
}
when substring not found it shows ..
I only need to display ... substring not found .. not the first line .. please check ..Substring found at : 0
Substring not found![]()
![]()
Last edited by nt_virus; 02-10-2008 at 09:21 PM.
how baout that, huh?Code:if (searchString<>string.Empty) { Console.WriteLine("Substring Found at : " + (found + 1)); } else { Console.WriteLine("Substring Not Found"); }
enjoy coding...
regards,
jireh
@ 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...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();
}
}
}
... 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 ..
![]()
Try setting found to less than 0:
Code: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(); } } }
Last edited by Jordan; 02-13-2008 at 05:13 AM.
Sorry, i don't have any more words to say except this --- YOU ARE GENIUS !!!
Please remove case-sensitiveness .. i don't know how to do ...
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks