How can I make it so that text.Split(' ')[0] increments? I would like it to do text.Split(' ')[ ] but putting that in there doesn't work. The goal is to have the code count the "search" words. Sorry, new to c#.
using System;
namespace TESTER
{
class Program
{
static void Main(string[] args)
{
int wordCount = 0;
int index = 0;
string text = "I ate a donut on national donut day and it tasted like a donut";
string search = "donut";
// skip whitespace until first word
while (index < text.Length)
{
if (search == text.Split(' ')[0])
{
wordCount ;
}
}
Console.WriteLine(wordCount);
}
}
}
CodePudding user response:
You could just do this:
string text = "I ate a donut on national donut day and it tasted like a donut";
string search = "donut";
int wordCount = text.Split(' ').Count(x => x == search);
Console.WriteLine(wordCount);
That gives 3
.
CodePudding user response:
Try doing this.
using System;
namespace TESTER
{
class Program
{
static void Main(string[] args)
{
int wordCount = 0;
int index = 0;
string text = "I ate a donut on national donut day and it tasted like a donut";
string search = "donut";
// skip whitespace until first word
string[] wordArray = text.Split(' ');
while (index < wordArray.Length)
{
if (search == wordArray[index])
{
wordCount ;
}
index ;
}
Console.WriteLine(wordCount);
}
}
}
CodePudding user response:
The answer of Enigmativity is the right one. That's how you should do what you want.
But you're learning and using LINQ won't make it easier.
Your variable text is of string. When you use the member function Split(...)
of the type string
(or String
, which is the same), it will return an array of string
. This is string[]
. To use the []
you can declare such an object.
string[] words;
Then you assign the result of your text.Split(' ')
to it.
words = text.Split(' ');
This gives you access to all entries through the variable words
.
string str = words[0];
To count without LINQ you can iterate through the array. Think this was you intention with the [ ]
. You have two options.
Use a for-loop or a foreach.
int wordCount = 0;
for( int i = 0; i < words.Count )
{
if( words[i] == search)
wordCount;
}
or the foreach-loop
// let pretend it's a real program here and
// reset the wordCount rather then declaring it
wordCount = 0;
foreach( string str in words )
{
if( words[i] == search)
wordCount;
}
Increamentation with the
sign, or it's oposite --
:
These need a number. For instance an int
.
int number = 0;
This you can increament with:
number ;
Now number
will have the value of 1
.
You can use it in the indexer of an array. But you do need an integer.
Consider this code:
int index = 0;
while(index < words.Length)
{
Console.WriteLine( words[ index ] );
}
Here you have the array words
. In the indexer you request entry of what number holds as value. Then index
will be increamented by 1
and due to the while-loop index
will be 14 upon exiting the while-loop. 14 is the number of words in your initial string.
CodePudding user response:
You can use this:
using System;
using System.Linq;
namespace TESTER
{
class Program
{
static void Main(string[] args)
{
string text = "I ate a donut on national donut day and it tasted like a donut";
string search = "donut";
var wordCount = text.Split(' ', StringSplitOptions.RemoveEmptyEntries)
.Count(x => x == search);
Console.WriteLine(wordCount);
}
}
}
If you want a case-insensitive search use:
var wordCount = text.Split(' ', StringSplitOptions.RemoveEmptyEntries).Count(
x => string.Equals(x, search, StringComparison.InvariantCultureIgnoreCase)
);