4 Replies - 832 Views - Last Post: 05 October 2010 - 10:45 PM Rate Topic: -----

#1 oarayax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-August 10

Problem with string array (easy)

Posted 03 October 2010 - 07:18 PM

so i have to ask the user whats your name? and the user type "any_name" i have to tell how many letters are they and the first letter and the last one.

my question is: is there a funcion like the one i found to get the letters (name.length) to convert the name in a string array so i can print the fisrt letter being the first element of the array and the last letter of the name being the last element of my array. how can i convert the name given to an array giving each element of the array a letter of the name given.

here is my code so far in a class i made that does this

 class Class1
    {

        string name;
     
        public void letters() 
        {
            Console.WriteLine("whats your name?");
            name = Console.ReadLine(); 
            string[] array = new string[nombre.Length];
           
            
                Console.WriteLine("your name has  {0} lettters", nombre.Length);  
        }



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with string array (easy)

#2 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Problem with string array (easy)

Posted 03 October 2010 - 08:10 PM

The String class has many capabilities. :)

Have a look at String.Split Method

Instead of:

...
name = Console.ReadLine();
string[] array = new string[nombre.Length];
...



do

...
name = Console.ReadLine();
string[] array = name.Split(); // defaults to splitting on whitespace
...



Array.length will tell you how many pieces name was split into. Each string in the array will also have a length so you can find the first and last characters of each.

This post has been edited by n8wxs: 03 October 2010 - 08:32 PM

Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 444
  • View blog
  • Posts: 1,492
  • Joined: 28-April 09

Re: Problem with string array (easy)

Posted 03 October 2010 - 08:23 PM

just treat the string as a char array and user stringname[] to access each character
like

string name = "alphalpha";
			
for (int i=0; i<name.Length; i++)
{
	Console.WriteLine (name[i]); // writes each letter on a new line
}


Was This Post Helpful? 1
  • +
  • -

#4 oarayax  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 15-August 10

Re: Problem with string array (easy)

Posted 05 October 2010 - 09:33 PM

Thank you both for your help, the problem was resolved here is the code finished .. thankx you again it really help me...


  public void letters() 
        {
            Console.WriteLine("whats your name?");
            name = Console.ReadLine();
            string[] array = name.Split();
            Console.WriteLine("First letter is : {0}", name[0]);
            Console.WriteLine("last letter is  {0}", name[(name.Length)-1]);
            Console.WriteLine("your name has  {0} lettters", name.Length);  
        }





Was This Post Helpful? 0
  • +
  • -

#5 Curtis Rutland  Icon User is online

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 4497
  • View blog
  • Posts: 7,850
  • Joined: 08-June 10

Re: Problem with string array (easy)

Posted 05 October 2010 - 10:45 PM

Well, you can remove line 5 from the code. You're creating the string array, but you're never actually doing anything with it, so it's pointless.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1