469,963 Members | 1,890 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,963 developers. It's quick & easy.

Return the number of the array in c#

3
I got a little problem, i have to return the number of the array of the word that is filled in.
First there is filled in a sentence, and have to be split.
I think i splitted it but now i cant return the number of the index, i only get the number of the char.
I hope that some1 can help me.
Its written in C# console.
Like this: A line of words.
Then type one of the words like : line .
Than i need to get returned 1.
Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine("Please fill in a line of words:");
  2.             string st = Console.ReadLine(); // type the words
  3.             string[] words = st.Split(new char[] { char.Parse(" ") }); // split up the words
  4.  
  5.  
  6. Console.WriteLine("Fill in a word of the line above: ");
  7.                         string LS = Console.ReadLine();
  8.                         //Console.ReadLine();
  9.                         Console.WriteLine(st.IndexOf(LS));
  10.                         Console.ReadLine();  
  11.  
  12.  
  13.  
Hopefully its enough info.
Thanks in andvance

Greetings, Robba.
Nov 9 '07 #1
4 1353
r035198x
13,262 8TB
I got a little problem, i have to return the number of the array of the word that is filled in.
First there is filled in a sentence, and have to be split.
I think i splitted it but now i cant return the number of the index, i only get the number of the char.
I hope that some1 can help me.
Its written in C# console.
Like this: A line of words.
Then type one of the words like : line .
Than i need to get returned 1.
Expand|Select|Wrap|Line Numbers
  1. Console.WriteLine("Please fill in a line of words:");
  2.             string st = Console.ReadLine(); // type the words
  3.             string[] words = st.Split(new char[] { char.Parse(" ") }); // split up the words
  4.  
  5.  
  6. Console.WriteLine("Fill in a word of the line above: ");
  7.                         string LS = Console.ReadLine();
  8.                         //Console.ReadLine();
  9.                         Console.WriteLine(st.IndexOf(LS));
  10.                         Console.ReadLine();  
  11.  
  12.  
  13.  
Hopefully its enough info.
Thanks in andvance

Greetings, Robba.
Why did you declare the array "words" when you never used it?
If you want to search the position of the entered text from that array then you need a loop (or use ArrayList instead of array).
Nov 9 '07 #2
Robba
3
Why did you declare the array "words" when you never used it?
If you want to search the position of the entered text from that array then you need a loop (or use ArrayList instead of array).
Great thanks, fixed it:P
The "words" was used to count the number of words in the progam, i though it wasnot nesesary to show the rest of the program(witch is a lot more lines).
Nov 9 '07 #3
Plater
7,872 Expert 4TB
Not sure what you were looking for?
Expand|Select|Wrap|Line Numbers
  1. for (int i=0; i<words.Length;i++)
  2. {
  3.    //something with this?
  4.    string currentword=words[i];
  5. }
  6.  
Nov 9 '07 #4
Robba
3
Expand|Select|Wrap|Line Numbers
  1.  
  2. Console.WriteLine("Please fill in a line of words:");
  3.             string st = Console.ReadLine(); // type the words
  4.  
  5.             //string[] words = st.Split(new char[] { char.Parse(" ") }); // split up the words
  6.             string[] words = st.Split(' ');
  7.  
  8.  
  9.             if (words.Length == 1)
  10.             {
  11.                 Console.WriteLine("You did not fill in enough words.");
  12.                 Console.ReadLine();
  13.                 return;
  14.             }
  15.             else
  16.             {
  17.                 Console.WriteLine("there are - " + words.Length + " - words in this line");
  18.             }
  19.  
this is the beginning.

Expand|Select|Wrap|Line Numbers
  1.  
  2. // index
  3. Console.WriteLine("This is the words index: ");
  4.                            for (int i = 0; i < words.Length; i++)
  5.  
  6.                             Console.WriteLine("word number - " + i + ": " + words[i]);
  7.  
  8.  
  9.                                                Console.ReadLine();    
  10.                     }
And i was looking for something like this, to get the index of the words in the array.
Nov 9 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by Andrew Poulos | last post: by
8 posts views Thread by M. Moennigmann | last post: by
23 posts views Thread by Nascimento | last post: by
6 posts views Thread by alternativa | last post: by
1 post views Thread by =?Utf-8?B?RGFya3Jpc2Vy?= | last post: by
1 post views Thread by rainxy | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.