`
namespace Program
{
class Program
{
static void Main(string[] args)
{
Console.Write("s: ");
string s = Console.ReadLine();
int count = 0;
string[] sp = s.Split(new Char[] {' '}, StringSplitOptions.RemoveEmptyEntries);
foreach (string s1 in sp)
{
if (s[s.Length-1] == 'А')
count ;
}
Console.WriteLine(count);
}
}
}
`
My code works, but only if the first character is "A" I need it to work even if the first character is not A. Help
CodePudding user response:
You can use LINQ
string s = Console.ReadLine();
var count = s.Split(' ').Count(c=>c.Contains('A'));
Console.WriteLine(count);
CodePudding user response:
You keep comparing the same thing over and over again. You need to check each string you iterate over for the character. Using ToUpper()
means that the count will increase for both upper case and lower case 'a's in the input.
Solution:
public static void Main()
{
Console.Write("s: ");
string s = Console.ReadLine();
int count = 0;
string[] sp = s.Split(new Char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
foreach (string s1 in sp)
{
if (s1.ToUpper().Contains('A'))
count ;
}
Console.WriteLine(count);
}
CodePudding user response:
you have to do like this.
string[] arr = new string[] { "Abc", "axy", "Abd"};
int count = 0;
foreach (string s1 in arr)
{
if (s1.Contains('A'))
count ;
}
if you want to run a for
loop not foreach
, do it like this.
for (int i = 0; i <arr.Length; i )
{
if (arr[i].Contains('A'))
count ;
}
Follow this answer if you are looking for case insensitive Contains.
CodePudding user response:
Let's query the text: first, let's match the words; let word be
Non empty sequence of letters
then we can Count
(with a help of Linq) words which Contains
char 'A'
:
using System.Linq;
using System.Text.RegularExpressions;
...
Console.Write("s: ");
int count = Regex
.Matches(Console.ReadLine(), @"\p{L} ")
.Cast<Match>()
.Count(match => match.Value.Contains('A'));
Console.WriteLine(count);
If you want case insensitive condition (i.e. count words which contain either 'a'
or 'A'
) you can put the last .Count
as
.Count(match => match.Value.Contains('A') || match.Value.Contains('a'));
CodePudding user response:
I think this sounds like a good use case for a regular expression.
The following code will populate numberOfMatches
with the number 3
, which is what I'm expecting.
var text = "This is a text which contains words with the letter A";
var matches = Regex.Matches(text, "\\w*a \\w*", RegexOptions.IgnoreCase);
var numberOfMatches = matches.Count();
CodePudding user response:
Not using Linq, Split, Contains, IndexOf, new tmp arrays, or what ever builtin helper, these few lines will also get that count of words containing whatever char ....
// 1 2 3 4 5 6 7
var str = "That fat fox started to accumulate marias fancy cheddar";
char f = 'A';
var cnt = 0;
var srch = f;
foreach (var c in str) {
if (char.ToUpperInvariant(c) == char.ToUpperInvariant(srch)) {
if (srch == f) {
cnt ;
srch = ' ';
}
else {
srch = f;
}
}
}
Console.WriteLine("{0} words containing '{1}' in '{2}' (ignoring casing)", cnt, f, str);