Newbie question about arrays 
Author Message
 Newbie question about arrays

using System;

class T1
{

        public static void Main()
        {

                Console.WriteLine("Ask me a question:");
                string sentence = Console.ReadLine();
                string[] sentenceArray = sentence.Split(" ".ToCharArray());
                int arrayLength = sentenceArray.Length;

        }

Quote:
}

As you can see I've taken string input from the console and parsed each
word into a string[].  How do I go about capturing the last element of
my array?  For example, if the user inputs "What kind of question?", I
need to find out what the punctuation is that ends the sentence(the last
element) and return that character to a variable.

*** Sent via Developersdex http://www.*-*-*.com/ ***
Don't just participate in USENET...get rewarded for it!



Sun, 06 Feb 2005 03:56:41 GMT  
 Newbie question about arrays

Quote:
> my array?  For example, if the user inputs "What kind of question?", I
> need to find out what the punctuation is that ends the sentence(the last
> element) and return that character to a variable.

Your code splits the string on a space, so the punctuation will most likely
be with the last word. Given your original example ("What kind of
question?"), and given your code:

Quote:
> string sentence = Console.ReadLine();
> string[] sentenceArray = sentence.Split(" ".ToCharArray());

I'm assuming that sentenceArray[] will contain the following:

    sentenceArray[0] = "What"
    sentenceArray[1] =  "kind"
    sentenceArray[2] = "of"
    sentenceArray[3] = "question?"

You'll have to check the last character in the last array element to find
out about punctuation.
--
Jeff Ferguson
http://staff.magenic.com/jefff/



Sun, 06 Feb 2005 04:07:41 GMT  
 Newbie question about arrays
string sentence = "do you have a question?";
string[] sentenceArray = sentence.Split(" ".ToCharArray());
int l = sentenceArray.Length;
string punct = sentenceArray[l-1].Substring(sentenceArray
[l-1].Length-1, 1);

this will blindly assume that the last character in the
last word is the punctuation.

Quote:
>-----Original Message-----

>using System;

>class T1
>{

>    public static void Main()
>    {

>            Console.WriteLine("Ask me a question:");
>            string sentence = Console.ReadLine();
>            string[] sentenceArray = sentence.Split

(" ".ToCharArray());
Quote:
>            int arrayLength = sentenceArray.Length;

>    }

>}

>As you can see I've taken string input from the console
and parsed each
>word into a string[].  How do I go about capturing the
last element of
>my array?  For example, if the user inputs "What kind of
question?", I
>need to find out what the punctuation is that ends the
sentence(the last
>element) and return that character to a variable.

>*** Sent via Developersdex http://www.developersdex.com
***
>Don't just participate in USENET...get rewarded for it!
>.



Sun, 06 Feb 2005 05:27:09 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Newbie question: Making arrays bigger

2. newbie question about array of pointers

3. Quick newbie question on arrays

4. Newbie question, obtain array of instantiated objects?

5. newbie question re array size limits.

6. Newbie question -- returning arrays

7. newbie question about arrays

8. newbie question on arrays

9. Newbie Question: Arrays

10. Newbie Question: using delete with dynamic 2-d arrays

11. Newbie Question: Array of Points

12. Arrays newbie question

 

 
Powered by phpBB® Forum Software