gpt4 book ai didi

Check if a word exists in another string in c# without using any inbuilt function(在不使用任何内置函数的情况下,检查c#中的另一个字符串中是否存在单词)

转载 作者:bug小助手 更新时间:2023-10-25 14:04:53 27 4
gpt4 key购买 nike



string sentence = "This is a goodday";
string word = "good";

I know this can be done with .Contains() method. I was asked in an interview how to do it without contains method.

我知道这可以通过.Containes()方法来完成。在一次采访中,我被问到如何在没有CONTAINS方法的情况下做到这一点。


更多回答

And you haven't even made an attempt?

而你甚至都没试过?

You've been interviewed, not us. So this is primarily your task. So please be so kind to post what you've tried and where specifically you need our help. Not just ship your task to us.

你被采访了,不是我们。所以这主要是你的任务。因此,请将您尝试过的内容以及您特别需要我们帮助的地方张贴出来。不仅仅是将您的任务发送给我们。

First off, read as many MSDN documentation, tutorials and articles as you can on the web. There are a few classes and techniques you should know well cause you will use them often. Other than that, you can achieve this via using IndexOf() or via using advanced Regex.

首先,在网上尽可能多地阅读MSDN文档、教程和文章。有几个类和技术你应该很好地了解,因为你会经常使用它们。除此之外,您可以通过使用IndexOf()或通过使用高级Regex来实现这一点。

Without using any inbuilt functions, you're basically limited to using either Regex or a recursive function that uses for loops cycling through each character in the string.

在不使用任何内置函数的情况下,基本上只能使用Regex或用于循环遍历字符串中每个字符的递归函数。

@RivoR. IndexOf() wouldn't meet the "No inbuilt functions" rule

@Rivor。IndexOf()不符合“没有内置函数”规则

优秀答案推荐

how to do it in english.

如何用英语做这件事。


pick the first letter of word.
walk down sentence a character at a time till you find that letter
now look at the next letters in sentence to see if they are the rest of that word
yes - done
no - keep going

the thing to use is that word[x] is the x-1th character of word, so you just need 2 indexes and a loop or 2

要使用的是单词[x]是单词的第x-1个字符,因此您只需要2个索引和一个循环或2



Q: Check if a word exists in another string in c# without using any inbuilt function

A: Tricky

问:在不使用任何内置函数的情况下,检查c#中的另一个字符串中是否存在单词A:Tricky


It depends on how detailed that "any inbuilt function" really is.

这取决于“任何内置函数”到底有多详细。


In general the algorithm is simple:

一般来说,该算法很简单:


Loop through the string you're searching in
for each position, see if you've found the word

you do this by looping through all the characters in what we're looking for
and compare each character from the first string with one from the second
if they all matched, we've found a match

but then ... "without using any inbuilt function".

但后来..。“而不使用任何内置函数”。


I assume this would mean, do not use the obvious ones, such as Contains, IndexOf, a regular expression, all those things.

我假设这意味着,不要使用显而易见的,如包含、索引Of、正则表达式等所有这些东西。


But taken to the extreme, does that mean I cannot even know how long the strings are? Is s.Length a built-in function? And thus not allowed?

但从极端来看,这是不是意味着我甚至不知道弦有多长?S.Length是内置函数吗?因此是不被允许的?


public bool Contains(string value, string whatWereLookingFor)
{
return IndexOf(value, whatWereLookingFor) >= 0;
}

public int Length(string s)
{
int result = 0;
for (int i = 0; i <= 2147483647; i++)
{
try
{
char c = s[i];
}
catch (IndexOutOfRangeException)
{
break;
}
result = i + 1;
}

return result;
}

public int IndexOf(string value, string whatWereLookingFor)
{
int iMax = Length(value);
int whatMax = Length(whatWereLookingFor);
for (int i = 0; i <= iMax - whatMax; i++)
{
bool isMatch = true;
for (int j = 0; j < whatMax; j++)
{
if (value[i + j] != whatWereLookingFor[j])
{
isMatch = false;
break;
}
}
if (isMatch)
return i;
}
return -1;
}


Since we cannot use Contains method from string class, How about this if we loop through the sentence and use Extension Method to Extend string class and implement own custom SubString method to check wether the given word exists in string or not.

既然我们不能使用字符串类中的CONTAINS方法,那么如果我们循环遍历语句,并使用扩展方法来扩展字符串类,并实现自己的自定义SubString方法来检查给定的单词是否存在于字符串中,如何?


string sentence = "This goodday";
string word = "good";
bool flag = false;
for (int i = 0; i < sentence.Length - word.Length; i++)
{
if (word.Equals(sentence.CustomSubstring(i, word.Length)))
{
flag = true;
break;
}
}

public static class MyExtendStringClass{
public static string CustomSubstring(this string sentence, int startIndex, int noOfLetters){
string result = string.Empty;

for (int i = startIndex; i < sentence.Length; i++)
{
if (noOfLetters != 0)
{
result += sentence[i];
noOfLetters--;
}
else
{
break;
}
}

return result;
}
}


How about using a for loop?
Loop through the sentence checking for the first character of the work. Once you find that check for the next character of the word until you find the whole word (done - word exists in sentence) or a different character so you start again looking for the first character of the word until you run out of characters.

使用for循环怎么样?在句子中循环检查作品的第一个字符。找到后,检查单词的下一个字符,直到找到整个单词(完成词存在于句子中)或不同的字符,这样您就重新开始查找单词的第一个字符,直到字符用完。


As the comments say, it would have been nicer to let us know what you answered.

正如评论所说,让我们知道你的回答会更好。



try this:

试试这个:


    static bool checkString(string inputString="",string word="")
{
bool returV=false;
if(inputString =="" || word=="")
return false;
int intexcS=0;
Dictionary<int,string> d = new Dictionary<int, string>();
foreach (char cW in word)
{
foreach (char cS in inputString)
{
if(cW==cS){
if(!d.ContainsKey(intexcS) && d.Count<word.Length){
d.Add(intexcS,cW.ToString());
}
}
intexcS++;
}
intexcS=0;
}
int i=0;
foreach(var iitem in d) { if(iitem.Value!=word[i].ToString()) returV=false; else returV=true; i++; }
return returV;
}

更多回答

"Substring" probably counts as "built-in method"

“子字符串”可能算作“内置方法”

@HansKesting thanks for correcting me, i have other alternative, Where we can implement our own substring method by Extending string class in this way we can achieve the same result by considering all constraints

感谢您纠正我,我有其他的选择,在那里我们可以通过扩展字符串类来实现我们自己的子字符串方法,以这种方式我们可以通过考虑所有约束来实现相同的结果

Let’s assume he didn’t have the answers in mind during his interview. Hence the question here.

让我们假设他在面试时并没有想好答案。因此,这里出现了这样的问题。

well a) this uses all sorts of in built functions b) its very inefficient

嗯,a)这使用了各种各样的内置函数b)效率非常低

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com