Nice try, but the OP said NO pre-defined functions, so that rules out ToLower() and ToCharArray(), and the property Length (which is really just a get_Length() method).
Of course, using no pre-defined functions is going to make this much harder, because of course == is just an operator method, so they can't use that either.
So it looks like it's going to be much more amusing than it first appears :-)
Its good but its too lengthy code.I mean to say in above code u are seraching for word 'Hello'.So as it is a 5 letter word u used 5 if conditions.Assume that I want to find a word which contains 10 characters then it will b too lengthy code.
I have a string as "Hello how r u?".I want to find word 'Hello' in that string without using any predefined functions/methods in C#.
If you are asked to do this as part of a job interview you should politely stand up and say "Thank you for your time but I would not want to work for any organisation that thinks that this sort of question is even remotely sensible" and then run away.
If this is a homework question then you should double check that you have understood the requirements. If you are correct then you should transfer to another school/college/university as your teacher is a fool and causing you mental damage.
rakesh.sahuk...
Member
48 Points
44 Posts
find a word in string
Feb 10, 2012 10:15 AM|LINK
Hi frnds,
I have a string as "Hello how r u?".I want to find word 'Hello' in that string without using any predefined functions/methods in C#.
Pls reply me ASAP.
Thanks
Rakesh
nishantcomp2...
Member
82 Points
21 Posts
Re: find a word in string
Feb 10, 2012 10:17 AM|LINK
string str="Hello how r u?";
if(srr.contains("Hello"))
{
//contains
}
else
{
//not contains
}
nishantcomp2512
rakesh.sahuk...
Member
48 Points
44 Posts
Re: find a word in string
Feb 10, 2012 10:36 AM|LINK
I dont want to use any predefined functions/methods
andrew victo...
Member
142 Points
38 Posts
Re: find a word in string
Feb 10, 2012 11:03 AM|LINK
int a,flag = 0; string strString = "thHello india".ToLower(); char[] chrChar = strString.ToCharArray(); for(int i=0;i<=chrChar.Length;i++) { if (chrChar[i] == 'h') { a = i + 1; if (chrChar[a] == 'e') { a = a + 1; if (chrChar[a] == 'l') { a = a + 1; if (chrChar[a] == 'l') { a = a + 1; if (chrChar[a] == 'o') { flag = 1; break; } } } } } } if (flag == 0) { //not contains } else { //contains }DMW
All-Star
15943 Points
2353 Posts
Re: find a word in string
Feb 10, 2012 11:42 AM|LINK
andrew victor
Nice try, but the OP said NO pre-defined functions, so that rules out ToLower() and ToCharArray(), and the property Length (which is really just a get_Length() method).
Of course, using no pre-defined functions is going to make this much harder, because of course == is just an operator method, so they can't use that either.
So it looks like it's going to be much more amusing than it first appears :-)
Dave
rakesh.sahuk...
Member
48 Points
44 Posts
Re: find a word in string
Feb 10, 2012 11:55 AM|LINK
Its good but its too lengthy code.I mean to say in above code u are seraching for word 'Hello'.So as it is a 5 letter word u used 5 if conditions.Assume that I want to find a word which contains 10 characters then it will b too lengthy code.
subbu.tpt
Member
265 Points
80 Posts
Re: find a word in string
Feb 10, 2012 12:20 PM|LINK
check this link:http://en.wikipedia.org/wiki/Rabin%E2%80%93Karp_algorithm
saalameejen
Member
210 Points
50 Posts
Re: find a word in string
Feb 10, 2012 02:58 PM|LINK
Match Exact string
U can use a regular expression like so:
bool contains = Regex.IsMatch("Hello1 Hello2", @"\bHello\b"); // yields false bool contains = Regex.IsMatch("Hello1 Hello", @"\bHello\b"); // yields truethe \b is a word boundary check, and used like above it will be able to match whole words only.
Paul Linton
Star
13403 Points
2531 Posts
Re: find a word in string
Feb 10, 2012 08:23 PM|LINK
If you are asked to do this as part of a job interview you should politely stand up and say "Thank you for your time but I would not want to work for any organisation that thinks that this sort of question is even remotely sensible" and then run away.
If this is a homework question then you should double check that you have understood the requirements. If you are correct then you should transfer to another school/college/university as your teacher is a fool and causing you mental damage.
marko4
Member
320 Points
78 Posts
Re: find a word in string
Feb 11, 2012 02:12 AM|LINK
uasing parsing
you can access the characters in the string directly from the string variable like this
string mystr = "123";
char c = mystr[0];
you can create a loop like this
for (int i=0; i<mystr.length; i++)
{
char c = mystr[i];
}
this should get you started