Linear Search works with written word by user? [closed] - c#

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
So i recently started learning C# and I have an assignment from my teacher.
I have been stuck on the last part of the code and would like to know from people with more knowledge what I am doing wrong or if there is something I have forgotten.
The assignment is to let the user write maximum 5 words. Then the user can view the word he/she wrote. This part I have done and it works.
However the search part is confusing me. Im using array and for-loop and the search part is still not working. Also worth mention is that I have assigned the array to Console.Readline();, meaning test[0] Console.Readline() and so on;, if it is of any help.
So in short, I want to have a linear search that can find the written word. Also whatever I type when doing the search it says that the word exists.
This is the part I am stuck.

If you have all the words stored away in an Array, just use Array.Contains like this
string[] userWords = { "word1", "word2", "word3", "word4" };
string search = Console.ReadLine();
if(userWords.Contains(search))
{
Console.WriteLine("Word " + search + " exists");
}
You have to include the System.Linq namespace for this to work.

If I have understood your question correctly, you want to find a string from an array of strings. You can do this using a for loop.
string search=Console.ReadLine();
for(int i=0;i<5;i++){
if(test[i]==search){
Console.WriteLine("word: " + search + " exists.");
}
}

Related

Generating folders using textboxes [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Hello experts, I have to generate series of folders from a TextBox into specified location.I am having two textboxes to specify the limit of folders(say 30 folders).The problem am facing is that the folder names that i will be providing are alpha-numeric(say 121cs3h101) .
How to set limit when i provide an alpha-numeric values?
(For example: i provide textbox1=12cs3h101 and textbox2=12cs3h131 , i need the series limit to be generated). I am working with visual studio 2013 in c# windows form application. Thanks in advance.
ok I will try to give you a lead.
To parse a string or find specific characters one can use RegEx.Match or a simler method called String.Split. In both cases you have to be aware how your string is structured and how it can vary. The limits of variation are very important.
If as you say the beginning is always"12cs3h" you can either split the string at the character 'h'.
string[] sa = s.Split('h');
Or you can even use the index of 'h' (since the length seems to be fixed) and take the rest of the string to get the numbers.
int index = s.IndexOf('h');
The rest is up to you, ... convert, enumerate and so on.
EDIT: There is a nice method that does the enumeration job for you: Enumerable.Range Good luck

What does string.IndexOf do? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I'm working through string manipulation, where I need to find characters or copy some part of the string that the user has input and divide it to 3 different areas. I'm not asking anything here about code, I'll do it myself, but I was searching in the documentation that Microsoft provide about the IndexOf method and its 8 overloads and I can't really understand how to apply it. I simply can't understand what it is supposed to do.
Returns the first appearance of a specified char.
For example
string x = "Hello World";
x.indexOf("W");
it will return 6 (0 based count).
The overloads let you choose for example, where you want to start searching.. like
x.indexOf("W", 7); it will return -1 because W is at position 6 so if starts at 7 it won't find any.
I hope this helps ! the best way is to play with it
This also works with arrays.
I believe the MSDN explanation is pretty clear.
For example:
string something = "something";
int indexOfT = something.IndexOf("t"); // => returns 4
Reports the zero-based index of the first occurrence of the specified string in this instance.
So if "t" is in the fifth position of "something", 4 would be it's zero-based index.

C# Extract Words Beginning with %! and Ending With !% [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I need to extract some words out of a paragraph of text if the word starts with %! and ends with !%
I'd imagine regex would be good for this but unfortunately my regex isn't that great...OK its pretty bad...OK its non existent :(.
EXAMPLE TEXT
You don't want no %!beef!%, boy
Know I run the streets, boy
Better follow me towards
Downtown
What you see is what you get %!girl!%
Don't ever forget girl
Ain't seen nothing yet until you're
%!Downtown!%
EXPECTED RESULT
beef, girl, Downtown
How can I achieve this in C# with or without regex?
Like this:
var reg = new Regex(#"%!(?<word>\w+)!%");
var inStr = #"You don't want no %!beef!%, boy
Know I run the streets, boy
Better follow me towards
Downtown
What you see is what you get %!girl!%
Don't ever forget girl
Ain't seen nothing yet until you're
%!Downtown!%";
var results = reg.Matches(inStr).Cast<Match>().Select(m => m.Groups["word"].Value);
This will give you a list of matched words. Converting it to a comma-separated string is an exercise I'll leave up to you..
Also, next time you should probably do some quick research, you're eventually going to have to learn simple regexes..

Grabbing part of a String [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have a string that I am grabbing that is input as:
05/22/2015
Eligibility
05/06/2015
Date of Death
I need 05/06/2015. The dates will change as the program runs through a database, and I am just a little unsure on how to always be grabbing the correct one.
So you need the second date only? Is it always going to be the 3rd line? If so you can do
var secondDate = myData.Split(new [] { Environment.NewLine }, StringSplitOptions.None)[2];
If the new line isn't for certain; /n vs /r/n, use:
var secondDate = myDate.Split(Environment.Newline.ToCharArray(), StringSplitOptions.RemoveEmptyEntries)[2];
If I understand you correctly, your string has 4 lines and you want the part of the string between linebreak 2 and 3.
Search for the positions of the second and third linebreak \n and use the substring derived of these positions.

Counting frequency of characters in a line from a .txt file [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have a C# task to do and I am stuck on part of the coding. I am using StreamReader to read a .txt file which contains group exam grade data (e.g. ADCBCBBBADEBCCBADBAACDCCBEDCBACCFEABBCBBBCCEAABCBB), I need the code to work out how many A's, B's etc there are inside each set of data, I thought about using some form of count++ code but each attempt just throws errors.
I want it to print onto console the number of A's in that line of the .txt file.
I hope that makes sense, I understand how to do the rest but I just needed a hand on this section.
Consider using System.Linq, eg...
string myString = "ADCBCBBBADEBCCBADBAACDCCBEDCBACCFEABBCBBBCCEAABCBB";
int countOfAs = myString.Count(x => x == 'A');
//Result: 9

Categories

Resources