Jump to content

C# Analyze a string

Doughnutnator

Hi! I'm a newbie in C# that can hardly make a program with if, do, and while commands. In class our teacher (that sometimes ask me for help, so that's f*cked up) told us that he wants a program in C# (console application) with these functions:

 

- Ask user input for his/her name, no more than 20 characters.

- See how much vowels the name has and write the number of vowels in the screen.

- Ask if you want to do it again (i think that here we use a do while loop)

 

The teacher went a little retard, because he doesn't even know how to correctly make a do/while loop (I had to help him) so now all my classroom don't know what to do (also because he didn't even gave us Visual Studio Keys for our PC's)

 

It's just curiosity, I don't have to do it, he made 2 different activities, I had the luck and I got the easy one with a do/while loop, but I want to know how to do the other one because I know that my teacher isn't going to explain me well.

 

Thank you guys, and sorry for extremely newbie questions.

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to use visual studio the Express version is free. I don't know C# so much but C++. I'll look into it and come back if I get something before someone else does.

Link to comment
Share on other sites

Link to post
Share on other sites

If you want to use visual studio the Express version is free. I don't know C# so much but C++. I'll look into it and come back if I get something before someone else does.

I know that it's free, I have the Express 2013 Version. I know how to get software, my classmates don't :P I'm just trying to help them, since it seems that a friend and me are the only ones that kinda knows the basics of C#

Link to comment
Share on other sites

Link to post
Share on other sites

I got the C# code. I can either give it to you or help you figure it out. Also I'm using a for loop because you know how many character you have.

Link to comment
Share on other sites

Link to post
Share on other sites

I got the C# code. I can either give it to you or help you figure it out. Also I'm using a for loop because you know how many character you have.

I found one, but I would really like to figure it out, so I can learn. :) Thank you.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not sure how I feel about your situation. On one hand, an intro to programming is great to have in school. On the other hand, the teacher should know this stuff lol.

 

Also, sounds like you don't really want any help. Maybe you just needed to vent a bit :P

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not sure how I feel about your situation. On one hand, an intro to programming is great to have in school. On the other hand, the teacher should know this stuff lol.

Also, sounds like you don't really want any help. Maybe you just needed to vent a bit :P

I need help that's why i'm asking :( i don't know c#. I told that in the last comment because I want to learn how to do it instead of just copy/paste.

Maybe I didn't expressed it well, English isn't my native language.

Sorry :(

Link to comment
Share on other sites

Link to post
Share on other sites

Ok, I misunderstood then. Sorry.

// Item 1: Ask user input for his/her name, no more than 20 characters.// Look into theseConsole.WriteLine() // Print a message asking themConsole.ReadLine() // Request their inputstringVariable.Length // Gets the length of a string// Item 2: See how much vowels the name has and write the number of vowels in the screen.// Loop through the string variable and increment a count variable when you see a vowel// Item 3: Ask if you want to do it again (i think that here we use a do while loop)// Yes do everything inside a loop

Hmm, I may have misunderstood what you mean by item 2.

 

If you just need to know that the name contains 1 or more of each vowel, but don't need to count how many vowels there are, then Contains will work.

ex) Doughnutnator contains 3 of the 5 vowels 'o', 'u', and 'a'.

 

If you need to know how many vowels there are in total, then it wont.

ex) Doughnutnator contains 5 vowels in total 2 'o', 2 'u', and 1 'a'.

Link to comment
Share on other sites

Link to post
Share on other sites

- Ask user input for his/her name, no more than 20 characters.

if you want to truncate excess:

string temp = Console.ReadLine(20);

string input;

if (temp.Length < 20) input = temp;

else input = temp.substring(0,20);

 

if you want to ask again when length is in excess:

string input = null

while (input == null)

{

string temp = Console.ReadLine();

if (temp.Length < 20)  input = temp;

}

 

- See how much vowels the name has and write the number of vowels in the screen.

using string input:

int vowels = 0;

for (int i = 0; i < input.Length; i++)

{

if ("aeiouAEIOU".IndexOf(input) >= 0) i++; edit: wait that should have been vowels++;

}

 

https://stackoverflow.com/questions/17764680/check-if-a-character-is-a-vowel-or-consonant

 

- Ask if you want to do it again (i think that here we use a do while loop)

create bool Again = true;

put all the code in a while (Again == true) loop

at the end of the loop do if (Console.ReadLine().ToLower() == "no") Again = false;

then it will exit 

Link to comment
Share on other sites

Link to post
Share on other sites

static void Main(string[] args)        {            // set the vairiables we'll be using.            bool doagain = false;            string name = "";            string vowels = "aeiou";            int vowelcount = 0;            do // whole program do/while            {                do // name input do/while                {                    Console.WriteLine("Enter your name, no more than 20 characters.");                    name = Console.ReadLine().ToLower(); // grab the name abd convert it to lower case                } while (name.Length > 20); // make sure the name is less than 20 characters                for (int i = 0; i < name.Length; i++) // loop through the name and count the vowels                {                    if (vowels.IndexOf(name[i]) >= 0)                        vowelcount++;                }                Console.WriteLine("There are " + vowelcount + " vowel(s) in the name " + name + "."); // display the vowel count                Console.WriteLine("Would you like to do this again? [Y/N]"); // ask the user to try again                if (Console.ReadLine().ToLower().Equals("y")) // if the user wants to try again, we run it again.                    doagain = true;            } while (doagain);        }

There's my attempt at it.

I might be wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

if you want to truncate excess:

string temp = Console.ReadLine(20);

string input;

if (temp.Length < 20) input = temp;

else input = temp.substring(0,20);

if you want to ask again when length is in excess:

string input = null

while (input == null)

{

string temp = Console.ReadLine();

if (temp.Length < 20) input = temp;

}

using string input:

int vowels = 0;

for (int i = 0; i < input.Length; i++)

{

if ("aeiouAEIOU".IndexOf(input) >= 0) i++;

}

https://stackoverflow.com/questions/17764680/check-if-a-character-is-a-vowel-or-consonant

create bool Again = true;

put all the code in a while (Again == true) loop

at the end of the loop do if (Console.ReadLine().ToLower() == "no") Again = false;

then it will exit

Thank you very much, this is very well explained method. I really appreciate your help, I now understand how to do it. Thanks :D

Link to comment
Share on other sites

Link to post
Share on other sites

static void Main(string[] args)        {            // set the vairiables we'll be using.            bool doagain = false;            string name = "";            string vowels = "aeiou";            int vowelcount = 0;            do // whole program do/while            {                do // name input do/while                {                    Console.WriteLine("Enter your name, no more than 20 characters.");                    name = Console.ReadLine().ToLower(); // grab the name abd convert it to lower case                } while (name.Length > 20); // make sure the name is less than 20 characters                for (int i = 0; i < name.Length; i++) // loop through the name and count the vowels                {                    if (vowels.IndexOf(name[i]) >= 0)                        vowelcount++;                }                Console.WriteLine("There are " + vowelcount + " vowel(s) in the name " + name + "."); // display the vowel count                Console.WriteLine("Would you like to do this again? [Y/N]"); // ask the user to try again                if (Console.ReadLine().ToLower().Equals("y")) // if the user wants to try again, we run it again.                    doagain = true;            } while (doagain);        }
There's my attempt at it.

Thank you! I owe you bacon!

I didn't knew that I could add the condition in the same line (cosole.readline().ToLower().Equals("y"))

This is going to help me a lot because it saves a lot of time. I like how you used a boolean for the do/while, I never knew how to use them correctly, and with your example now I see that they are pretty useful.

Thank you very much for your help.

Link to comment
Share on other sites

Link to post
Share on other sites

Thank you! I owe you bacon!

I didn't knew that I could add the condition in the same line (cosole.readline().ToLower().Equals("y"))

This is going to help me a lot because it saves a lot of time. I like how you used a boolean for the do/while, I never knew how to use them correctly, and with your example now I see that they are pretty useful.

Thank you very much for your help.

Sure thing :D I'm glad it helped. Feel free to PM me with any questions or problems, I love to help.

I might be wrong.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×