4 Replies - 1177 Views - Last Post: 23 May 2012 - 03:00 PM Rate Topic: -----

#1 inerstrength  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-May 12

Problem with code trying to count number of words etc, in asked String

Posted 22 May 2012 - 08:54 PM

I am trying to find the Number of words, Upper/Lowercase Letters and number of SPACES in a Entered string. The output shows that there is 0 Words,Upper/Lowercase Letters, or Spaces.


using System;
using System.IO;
namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter a String");
            Console.ReadLine();

            char ch;
            int x;

            int space = 0;
            int Lowercase = 0;
            int Uppercase = 0;
            int words = 0;

            do
            {
                x = Console.Read();
                ch = Convert.ToChar(x);
                if (ch.Equals(' '))
                {
                    space++;
                }
                else if (Char.IsLower(ch))
                {
                    Lowercase++;
                }
                else if (Char.IsUpper(ch))
                {
                    Uppercase++;
                }
                if (ch.Equals(x))
                {
                    words++;
                }

            }
            while (ch != '\r');

            Console.WriteLine();

            Console.WriteLine("# of spaces: {0}", space);
            Console.WriteLine("# of Lowercase Letters: {0}", Lowercase);
            Console.WriteLine("# of Uppercase Letters: {0}", Uppercase);
            Console.WriteLine("# of Words: {0}", words);
            Console.ReadLine();
            Console.ReadLine();
        }
    }
}


Is This A Good Question/Topic? 0
  • +

Replies To: Problem with code trying to count number of words etc, in asked String

#2 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,374
  • Joined: 02-June 10

Re: Problem with code trying to count number of words etc, in asked String

Posted 23 May 2012 - 06:41 AM

View Postinerstrength, on 22 May 2012 - 09:54 PM, said:

I am trying to find the Number of words, Upper/Lowercase Letters and number of SPACES in a Entered string. The output shows that there is 0 Words,Upper/Lowercase Letters, or Spaces.


Is there a question in there somewhere?

inerstrength: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # 2. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated May 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,374
  • Joined: 02-June 10

Re: Problem with code trying to count number of words etc, in asked String

Posted 23 May 2012 - 06:46 AM

10 Console.ReadLine();

Don't you want to use a variable here? The user inputs a line to be scanned through... You need to save that line so you can scan through it.


22 x = Console.Read();
Why is this in the do..while loop? If the user entered their string earlier why are they entering it again why letter at a time.

I think you need to get some sleep. The read through this code again with a clear head and think about what you are doing.
Was This Post Helpful? 1
  • +
  • -

#4 inerstrength  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 22-May 12

Re: Problem with code trying to count number of words etc, in asked String

Posted 23 May 2012 - 01:03 PM

10 Console.ReadLine();
I am trying to save it as how anyone would enter a string. since I am trying to make a code so that I would prompt "user" to enter a string, and that the code would count how many words, Upper and Lowercase letters and Count how many spaces are in the string.

I don't know how to use breakpoints, we never learned that in class, so are there any different simpler methods? What my main problem is that when I hit F5 in Microsoft Studios 2011 it prompts to enter a string yes and then I have to hit enter twice to be given all that I asked for the code to count as 0.

Thanks for helping!
Would like to have more advice ><
Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6507
  • View blog
  • Posts: 14,374
  • Joined: 02-June 10

Re: Problem with code trying to count number of words etc, in asked String

Posted 23 May 2012 - 03:00 PM

View Postinerstrength, on 23 May 2012 - 02:03 PM, said:

What my main problem is that when I hit F5 in Microsoft Studios 2011 it prompts to enter a string yes and then I have to hit enter twice to be given all that I asked for the code to count as 0.


Right... Because on line 10 you have the user enter it but you don't save that to a variable to use later.

Then you have a letter by letter input in your do...while loop. On line 22.
I've already pointed this out to you.

So yeah, you enter it twice - because that's what you wrote.

Or did you write it? Is this code your code, or just some stuff you scavenged from 'net and tried to fit together? if you don't understand what you plagiarized then there is little we can do for you.

We are not going to re-write your code for you. You have not shown any updated code based on the very specific advice you've been given. Make an effort to use the advice you've been given.

inerstrength: What this shows us is that you aren't familiar with breakpoints and how to debug your own code. Learning to debug one's own code is an essential skill. Sadly, one that apparently few college courses teach. Silly if you ask me.

Placing breakpoints and walking through the code line by line allows you to actually WATCH it execute.

Visualizing what your code does will let you see why it behaves the way it does.

It would be well worth your time to do the tutorials on FAQ 2. A couple hours learning this skill will save you hundreds of hours of confusion in one project alone.


See FAQ # 2. (Click the SHOW button below)


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Class1/Form1 talk to Class2/Form2


FAQ (Frequently Asked Questions - Updated May 2012
Spoiler



Was This Post Helpful? 0
  • +
  • -

Page 1 of 1