10 Replies - 1168 Views - Last Post: 26 February 2011 - 11:29 AM Rate Topic: -----

#1 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

help with logic?

Posted 25 February 2011 - 06:19 PM

Ok, so what I have so far is a textbox and a button. What I'm trying to do is when you type a string into the textbox then hit the button it should take the first letter of each word capitalize them and then remove them and put them into a txt file in a specific folder.

I'm not looking for code, all I want is a little help in understanding the logic so that I can do the program myself. How do I tell the program that I want the first letter of each word? Is there way to detect spaces in the string?

Is This A Good Question/Topic? 0
  • +

Replies To: help with logic?

#2 n8wxs   User is offline

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

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

Re: help with logic?

Posted 25 February 2011 - 06:43 PM

The String class has methods and properties you can use.

See for example String.Chars Property and String.IndexOf Method (Char)

This post has been edited by n8wxs: 25 February 2011 - 06:43 PM

Was This Post Helpful? 0
  • +
  • -

#3 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Re: help with logic?

Posted 25 February 2011 - 06:57 PM

What is the String.Chars property and the String.IndexOf method used for? I'm having trouble understanding the MSDN documentation. They don't explain some stuff as much as they should.
Was This Post Helpful? 0
  • +
  • -

#4 Curtis Rutland   User is offline

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


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: help with logic?

Posted 25 February 2011 - 09:38 PM

Chars allows you to treat the string as a char[]. IndexOf returns the first index of an instance of a character found in a string, or -1 if it's not found in the string.

But I'd suggest neither, actually. I'd use .Split to split the string around the spaces. That'll give you a string[] of each word. Then you can just do .First, or [0] to get the first char in each string returned. Then you can .ToUpper() it.

If you're interested in LINQ, you could actually do this all in one statement.
Was This Post Helpful? 2
  • +
  • -

#5 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Re: help with logic?

Posted 25 February 2011 - 10:45 PM

Here is what I have so far:

            // Changes the first letter of each word in the string to uppercase.
            txt2cypher.Text.ToUpper();

            // Splits the string after each space
            txt2cypher.Text.Split();

            // Takes the first letter of each word from the string
            txt2cypher.Text.First();

            // Writes the output to a text file.
            StreamWriter writer = new StreamWriter(Environment.SpecialFolder.MyDocuments, @"Cypher\Cypher");
            writer.WriteLine(text2cypher.Text);
            writer.Close();



At the moment I have 4 errors. That single line of LINQ is actually starting to sound pretty good right now.
Was This Post Helpful? 0
  • +
  • -

#6 Curtis Rutland   User is offline

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


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: help with logic?

Posted 26 February 2011 - 12:10 AM

First thing you have to know is strings are completely immutable. Once defined, a string cannot be changed. All the methods on string return a string with the changes. Except Split, that returns a string[].

So look up each of these methods on the MSDN. They have useful examples to explain how to use each of these methods.
Was This Post Helpful? 1
  • +
  • -

#7 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Re: help with logic?

Posted 26 February 2011 - 09:36 AM

Ok, so after referring back to the documentation I now have something that partly works. The toUpper method works fine. However, I think my logic may be off for the Split and First methods. Here is what I have.

            // Changes the first letter of each word in the string to uppercase.
            string stringUpper = txt2cypher.Text.ToUpper();
            txt2cypher.Text = stringUpper;

            // Splits the string after each space
            string[] words = txt2cypher.Text.Split(new Char [] {' '});


            // Takes the first letter of each word from the string
            txt2cypher.Text = words.First();



By the way, txt2cypher is my textbox. Any suggestions on what might be going wrong?
Was This Post Helpful? 0
  • +
  • -

#8 Tymon   User is offline

  • New D.I.C Head
  • member icon

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 26-February 11

Re: help with logic?

Posted 26 February 2011 - 10:22 AM

View PostRandomlyKnighted, on 26 February 2011 - 09:36 AM, said:

Ok, so after referring back to the documentation I now have something that partly works. The toUpper method works fine. However, I think my logic may be off for the Split and First methods. Here is what I have.

            // Changes the first letter of each word in the string to uppercase.
            string stringUpper = txt2cypher.Text.ToUpper();
            txt2cypher.Text = stringUpper;

            // Splits the string after each space
            string[] words = txt2cypher.Text.Split(new Char [] {' '});


            // Takes the first letter of each word from the string
            txt2cypher.Text = words.First();



By the way, txt2cypher is my textbox. Any suggestions on what might be going wrong?


Here is a similar console application

        static void Main(string[] args)
        {
            String s = "Hello There";
            String[] words = s.Split(new char[] { ' ' });
            Console.WriteLine(words[0]); //writes first word

            foreach (String word in words) //loops through all words
            {
                Console.WriteLine(word); //writes the current word
                Console.WriteLine(word[0].ToString());//writes the first letter of the current word
            }
            Console.ReadLine();
        }



Since a string is essentially an array of chars, it can be treated as an array more often then not.

This post has been edited by Tymon: 26 February 2011 - 10:25 AM

Was This Post Helpful? 0
  • +
  • -

#9 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Re: help with logic?

Posted 26 February 2011 - 10:32 AM

Ok, so your code concentrates on a single word. Is there way for it to do all the words at once?

For example: The string is "Hello my name is RandomlyKnighted"

Output: H M N I R
Was This Post Helpful? 0
  • +
  • -

#10 Curtis Rutland   User is offline

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


Reputation: 5106
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: help with logic?

Posted 26 February 2011 - 11:06 AM

All right, I'm going to do what we don't usually do here. I'm going to show you how to do this, because sometime's its easier to learn by example than just letting you twist in the wind.

string sentence = "Hello my name is RandomlyKnighted";
string[] words = sentence.Split(' ');
string buffer = "";
foreach (string word in words) {
    string letter = word[0].ToString();
    buffer += letter.ToUpper();
}
Console.WriteLine(buffer);



First, we get the sentence. You can get it from the text box. Then, we split it. One of split's overloads is a params char[], which means you can just put in as many chars as you want separated by commas (look up params if you don't understand). We only want to split on one, the space.

Then we make an empty buffer. If this were going to be big, we'd make a StringBuilder, but for just a few words, it's ok to use a string.

Then we loop through all the words we split out. We take the first character, make it into a string. Then we capitalize it and add it to the buffer.

Outside the loop, we print out the results. Here, you can do whatever you needed to do instead.

Remember that you don't have to keep using your textbox's text string to hold each change we make. Actually, it's preferable that you don't, since it's a UI element.

I really do think that people should learn to program with the console first. UI elements confuse everything.

Oh, and just because I told you it could be done, here's the one liner using LINQ:

var output = sentence.Split(' ').Select(x => x[0].ToString().ToUpper()).Aggregate((w, n) => w + n);


But don't use that until you know exactly what it does.
Was This Post Helpful? 1
  • +
  • -

#11 RandomlyKnighted   User is offline

  • D.I.C Lover
  • member icon

Reputation: 119
  • View blog
  • Posts: 1,384
  • Joined: 14-January 10

Re: help with logic?

Posted 26 February 2011 - 11:29 AM

Thanks for explaining that. I'm sorry that you had to actually show me how to do this.

This post has been edited by RandomlyKnighted: 26 February 2011 - 11:46 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1