简体   繁体   中英

C#: How to use arrays, chars and strings? Clear up needed

I am new to programming and I have a project in my Algorithm class. What we have to do is decide on a problem and solve it. We haven't learnt much more than string, char and WriteLine. We did add a couple of things as you will see soon!

I decided that what I want to solve this: The user inserts a word, no matter how long and the program will automatically make the first letter a capital letter. So far this is what I have:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
    static void Main(string[] args)
    {
    start:
        Console.WriteLine("Please enter a word below:");
        Console.WriteLine("");
        string str = Console.ReadLine();
        char char1;
        if (str[0] >= 97)
        {
            char1 = (char)(str[0] - 32);
        }
        else
        {
            char1 = (char)(str[0] + 32);
        }

        char char2 = (char)(str[1]);
        char char3 = (char)(str[2]);
        char char4 = (char)(str[3]);
        char char5 = (char)(str[4]);
        Console.WriteLine("");
        Console.Write(char1);
        Console.Write(char2);
        Console.Write(char3);
        Console.Write(char4);
        Console.WriteLine(char5);
        goto start;
}
}
}

The problem with that code is that any word with less than 5 letters will make the program crash. Anything with more than 5 letters will just be cut at the fifth letter... I was told that using arrays should solve this problem. Seeing as I am a total newbie at this, I would need this to be broken down and be as simply told as possible!

Any help getting this to work would be very appreciated.

Thanks :)

Console.WriteLine("Enter a word:");
string str = Console.ReadLine();
Console.WriteLine(str[0].ToString().ToUpper() + str.Substring(1));

This will work.

Or... if you need to go through the entire string and find the first actual alphabetical character, you can do the following:

Console.WriteLine("Please enter a word:");
string s = Console.ReadLine();
bool found = false;
char[] chars = new char[s.Length];
for (int i = 0; i < s.Length; i++)
{
    if (Char.IsLetter(s[i]) && !found)
    {
         chars[i] = s[i].ToString().ToUpper()[0];
         found = true;
    }
    else
    {
        chars[i] = s[i];
    }
}
s = new String(chars);
Console.WriteLine(s);

Use a for loop like this after writing char1 to the Console:

if (str.Length > 1)
{
    for (int i = 1; i < str.Length; i++)// Start at 1 to skip char1
    {
        Console.Write(str[i]);
    }
}

There are some methods you can call on string that will be helpful:

  • Substring
  • ToUpper

In fact, you don't need to worry about characters; this problem can be solved using only strings.

Also take care to check that your code handles the case where the string is empty (using an if statement), which will happen if the user just presses Enter without typing anything.

You're taking an algorithms class and they make you choose a problem to solve? Sounds dangerous for someone learning.

Console.WriteLine("Please enter a word below:");
Console.WriteLine("");
string inputString = Console.ReadLine();    // try to use meaningful variable names

// shorthand for the if ... else block:
// type variableName = (true/false condition) ? "is true" : "is false";
char firstChar = inputString[0] >= 97 ? (char)(inputString[0] - 32) : (char)(inputString[0] + 32);

Console.WriteLine("");
Console.Write(firstChar);

for (int i = 1; i < inputString.Length; i++)    // skip firstChar
{
    Console.Write(inputString[i]);
}

As others have mentioned, you need to use a loop for this if you want anything resembling a general solution.

Also, you'll want to avoid using goto statements. There are many reasons, one being that they (in my opinion) make code difficult to read and maintain.

Additionally, if your code had worked as written, it would never end. Your program, as written would execute, then begin again, never stopping. If you want this sort of behavior, then you should wrap your code in an infinite loop which exits upon some condition. This might look something like:

bool keepRunning = true;
while(keepRunning){
    //code here
    Console.Write("go again? (y/n) ");
    keepRunning = (string)(Console.ReadLine()).equals("y") ? false : true;  
}

On that last statement, I forget if you need to cast the output of ReadLine to a string before calling the .equals method... I don't have my IDE up. i think you get the idea.

edit: I saw another response that was just posed about using the .ToUpper method. I thought of this but assumed maybe you needed to use the char type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM