-1

`

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

Matvei
  • 1
  • 1
  • Welcome to Stack Overflow! This is a good opportunity for you to start familiarizing yourself with [using a debugger](https://stackoverflow.com/q/25385173/328193). When you step through the code in a debugger, which operation first produces an unexpected result? What were the values used in that operation? What was the result? What result was expected? Why? To learn more about this community and how we can help you, please start with the [tour] and read [ask] and its linked resources. – David Nov 09 '22 at 15:03
  • `if(s.Contains('A'))` – Magnus Nov 09 '22 at 15:04
  • 2
    At a glance... What is the purpose of that `foreach` loop? Nowhere in the loop is `s1` used, so the loop is just performing the exact same operation over and over. – David Nov 09 '22 at 15:04

6 Answers6

2

You can use LINQ

string s = Console.ReadLine();
var count = s.Split(' ').Count(c=>c.Contains('A'));
Console.WriteLine(count);  
Shinva
  • 1,899
  • 18
  • 25
1

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);
}
YungDeiza
  • 3,128
  • 1
  • 7
  • 32
0

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.

Vivek Nuna
  • 25,472
  • 25
  • 109
  • 197
0

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();
Jan_V
  • 4,244
  • 1
  • 40
  • 64
0

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'));
Dmitry Bychenko
  • 180,369
  • 20
  • 160
  • 215
0

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);
lidqy
  • 1,891
  • 1
  • 9
  • 11