-2
string s = "This is an ExAmPlE of sTrInG";

Such words in this string: 2.

I understand how to convert a phrase to uppercase and lowercase words but i dont know how to find such words in string and count it

mousetail
  • 7,009
  • 4
  • 25
  • 45
Solar1888
  • 7
  • 2
  • What type of words exactly are you trying to count? – mousetail Mar 22 '21 at 10:32
  • Words in which lowercase and uppercase alternate – Solar1888 Mar 22 '21 at 10:33
  • 2
    Should they alternate exactly or just select words that contain both upper and lower case letters? – mousetail Mar 22 '21 at 10:34
  • 1
    Just go over the words in a loop and check for each character? – Max Play Mar 22 '21 at 10:35
  • why is `ExAmPLE` alternating while `This` is not? Would `Example` be alternating? – MakePeaceGreatAgain Mar 22 '21 at 10:35
  • yes. they should alternate. Max, is there a way to do this without using a loop? – Solar1888 Mar 22 '21 at 10:36
  • @HimBromBeere it's `ExAmPlE`, not `ExAmPLE` (alternating meaning 1up/1low/1up/1low...) – Rafalon Mar 22 '21 at 10:38
  • how do you iterate through a string without a loop? unless the string always has a fixed length – phuclv Mar 22 '21 at 10:38
  • HimBromBeere, in my question " ExAmPlE" and "sTrInG". in these words capital letters alternate with lowercase – Solar1888 Mar 22 '21 at 10:39
  • Do you have any requirement regarding what you consider to be "Upper" and "Lower"? Depending on your requirements, you could use `char.IsUpper(c1) != char.IsUpper(c2)` to check that two chars don't have the same case. Or you could use `char.IsLower(c1) != char.IsLower(c2)`. Or you have to use `char.IsLower(c1) && char.IsUpper(c2) || char.IsUpper(c1) && char.IsLower(c2)`. Edge cases include having digits or non-alphanumeric characters – Rafalon Mar 22 '21 at 10:57

4 Answers4

0

This should do the trick:

First you need to split the string in words and than you can check each word if is alternating

    public void Counter(string phrase)
    {
        var qty = 0;
        var words = phrase.Split(' ');
        for (var i = 0; i < words.Length; ++i)
        {
            if (IsUpperLowerAltenated(words[i].Trim()))
                ++qty;
        }


    }


    public bool IsUpperLowerAltenated(string word)
    {
        for (var i = 1; i < word.Length; ++i)
        {
            if(char.IsLower(word[i-1]) == char.IsLower(word[i]))
               return false;
        }
        return true;
    }
Rafalon
  • 4,450
  • 2
  • 16
  • 30
Stefano Cavion
  • 641
  • 8
  • 16
-1

String is mixed case if it's not either lower or upper

bool IsUpper(string str) => str.Upper() == str;
bool IsLower(string str) => str.Lower() == str;

bool IsMixedCase(string str) => !IsUpper(str) && !IsLower(str);

string s = "This is an ExAmPlE of sTrInG";
foreach (var str in s.Split().Where(IsMixedCase))
{
    Console.WriteLine(str);
}

Update

Solution without explicit loop. String is alternating if each pair of characters in different cases

public static IEnumerable<(T, T)> Pairwise<T>(this IEnumerable<T> input) 
    => input.Zip(input.Skip(1), (a, b) => (a, b));

public static bool OfDifferentCase(char c1, char c2) 
    => char.IsUpper(c1) && char.IsLower(c2) || char.IsLower(c1) && char.IsUpper(c2);

public static bool IsAlternating(string str) 
    => str.Pairwise().All(p => OfDifferentCase(p.Item1, p.Item2));

string s = "This is an ExAmPlE of sTrInG";
foreach (var str in s.Split().Where(IsAlternating))
{
    Console.WriteLine(str);
}
JL0PD
  • 3,698
  • 2
  • 15
  • 23
  • 1
    OP doesn't seem to be looking for "mixed case" so it's not *that simple*, but I think that using a function that determines what you want is a good strategy (`bool IsAlternatingCase(string str)`) then all OP has to do is implement this – Rafalon Mar 22 '21 at 10:41
-1

You can find count of upper and lower chars like this.

public int IsUpperCount(string myString)
{   int upperC=0;
    var charArray = myString.ToCharArray();
    foreach (var item in charArray)
    {
     if(item.toString()==item.toString().ToUpper())
     { upperC++;}
 return upperC;
}

public int IsLowerCount(string myString)
{   int lowerC=0;
    var charArray = myString.ToCharArray();
    foreach (var item in charArray)
    {
     if(item.toString()==item.toString().ToUpper())
     { lowerC++;}
return lowerC;
}
Berk
  • 59
  • 4
  • 1
    `ExAmPlE` is alternating and does not have same count of upper and lower chars (4 vs 3). Reversely, `EXAmpl` have the same count and isn't alternating. I don't see how this answer can help determining if a word is alternating or not – Rafalon Mar 22 '21 at 11:00
-1
string s = "This is an ExAmPlE of sTrInG";

var words = Regex.Matches(s, @"\b((\p{Ll}\p{Lu})+\p{Ll}?|(\p{Lu}\p{Ll})+\p{Lu}?)\b");

foreach (var w in words)
{
    Console.WriteLine(w);
}

Reference:
Regex pattern search for alternating character case
RegEx for matching alternating case letters
Make regular expression not match empty string?
regex - match pattern of alternating characters
Supported Unicode general categories

GSerg
  • 76,472
  • 17
  • 159
  • 346