How to to check if all the characters of a string are same in C#

1 Answer

0 votes
using System;
 
class Program
{
    static bool all_characters_are_the_same(string s) { 
        int len = s.Length;
          
        for (int i = 1; i < len; i++) 
            if (s[i] != s[0]) 
                return false; 
                
        return true; 
    } 
    static void Main() {
        string s = "aaaaa"; 
         
        if (all_characters_are_the_same(s)) 
            Console.Write("Yes"); 
        else
            Console.Write("No"); 
    }
}
 
 
 
/*
run:
 
Yes
 
*/

 



answered Dec 31, 2019 by avibootz
edited Dec 31, 2019 by avibootz
...