Kapcake
Kapcake

Reputation: 37

adding a string to an array

I'm just doing a little project in C# (I'm a beginner), my code is basically asking you "how many words are in this sentence?" and then asks you for every word, once it gets all of them it prints it out with "ba" attached to every word.

I know I'm a real beginner and my code's probably a joke but could you please help me out with this one?

Console.WriteLine("How many words are in this sentence?");

int WordAmount = Convert.ToInt32(Console.ReadLine());
int i = 1;

while (i <= WordAmount)
{
    Console.WriteLine("Enter a word");
    string[] word = new string[] { Console.ReadLine() };
    i++;
}

Console.WriteLine(word + "ba");

Upvotes: 1

Views: 462

Answers (3)

Mikołaj Mularczyk
Mikołaj Mularczyk

Reputation: 969

First of all, consider storing your words in some kind of collection, for example a list.

List<string> words = new List<string>();
while (i <= WordAmount)
{
    Console.WriteLine("Enter a word");
    string word = Console.ReadLine();
    words.Add(word);
    i++;
}

I don't think your code compiles - the reason is you are trying to use the word variable outside of the scope that it is defined in. In my solution I have declared and initialized a list of strings (so list of the words in this case) outside of the scope where user has to input words, it is possible to access it in the inner scope (the area between curly brackets where user enters the words).

To print all the words, you have to iterate over the list and add a "ba" part. Something like this:

foreach(var word in words)
{
    Console.WriteLine(word + "ba");
}

Or more concisely:

words.ForEach(o => Console.WriteLine(o + "ba"));

If you want to print the sentence without using line breaks, you can use LINQ:

var wordsWithBa = words.Select(o => o + "ba ").Aggregate((a, b) => a + b);
Console.WriteLine(wordsWithBa);

Although I would recommend learning LINQ after you are a bit more familiarized with C# :)

You can look here and here to familiarize yourself with the concept of collections and scopes of variables.

You could also use a StringBuilder class to do this task (my LINQ method is not very efficient if it comes to memory, but i believe it is enough for your purpose).

Upvotes: 0

Derviş Kayımbaşıoğlu
Derviş Kayımbaşıoğlu

Reputation: 30665

string[] wordList = new string[WordAmount];
while (i <= WordAmount)
{
    Console.WriteLine("Enter a word");
    wordList[i-1] = Console.ReadLine() ;
    i++;
}

foreach (var item in wordList)
    Console.WriteLine(item + "ba");

Working Fiddle: https://dotnetfiddle.net/7UJKwN

your code has multiple issues. First you need to define your array outside of your while loop, and then fill it one by one.

In order to read/write array of strings (string[]), you need to loop through (iterate) it.

My code actually iterates your wordList. In the first While loop I am iterating to fill the wordList array. then printing it in the second loop

Upvotes: 0

Frontear
Frontear

Reputation: 1261

You're close, you've just got one issue.

string[] word = new string[] { Console.ReadLine() };

You are creating a new array list inside the scope of a while loop. Not only will this disappear every loop, meaning you never save the old words, but you also won't be able to use it outside of the loop, making it useless.

Create a string[] words = new string[WordAmount];. Then iterate through it to add your Console.ReadLine() to it, and finally, iterate through it once more and Console.WriteLine(words[i] + "ba");

Upvotes: 1

Related Questions