
- C# Basic Tutorial
- C# - Home
- C# - Overview
- C# - Environment
- C# - Program Structure
- C# - Basic Syntax
- C# - Data Types
- C# - Type Conversion
- C# - Variables
- C# - Constants
- C# - Operators
- C# - Decision Making
- C# - Loops
- C# - Encapsulation
- C# - Methods
- C# - Nullables
- C# - Arrays
- C# - Strings
- C# - Structure
- C# - Enums
- C# - Classes
- C# - Inheritance
- C# - Polymorphism
- C# - Operator Overloading
- C# - Interfaces
- C# - Namespaces
- C# - Preprocessor Directives
- C# - Regular Expressions
- C# - Exception Handling
- C# - File I/O
- C# Advanced Tutorial
- C# - Attributes
- C# - Reflection
- C# - Properties
- C# - Indexers
- C# - Delegates
- C# - Events
- C# - Collections
- C# - Generics
- C# - Anonymous Methods
- C# - Unsafe Codes
- C# - Multithreading
- C# Useful Resources
- C# - Questions and Answers
- C# - Quick Guide
- C# - Useful Resources
- C# - Discussion
Check if both halves of the string have same set of characters in C#
Firstly, set the string to be checked.
string s = "timetime";
Now set two counters for two halves of the string.
int []one = new int[MAX_CHAR]; int []two = new int[MAX_CHAR];
Check for both the halves of the string.
for (int i = 0, j = l - 1; i < j; i++, j--) { one[str[i] - 'a']++; two[str[j] - 'a']++; }
The following is the complete code to check whether both the halves of the string have same set of characters or not in C#.
Example
using System; class Demo { static int MAX_CHAR = 26; static bool findSameCharacters(string str) { int []one = new int[MAX_CHAR]; int []two = new int[MAX_CHAR]; int l = str.Length; if (l == 1) return true; for (int i = 0, j = l - 1; i < j; i++, j--) { one[str[i] - 'a']++; two[str[j] - 'a']++; } for (int i = 0; i < MAX_CHAR; i++) if (one[i] != two[i]) return false; return true; } public static void Main() { string str = "timetime"; if (findSameCharacters(str)) Console.Write("Yes: Two halves are same!"); else Console.Write("No! Two halves are not same!"); } }
Output
Yes: Two halves are same!
- Related Articles
- Check if both halves of the string have same set of characters in Python
- Check if both halves of the string have the same set of characters in Python
- Python program to check if both halves of the string have same set of characters.
- Check if both halves of the string have at least one different character in Python
- Check if two String objects have the same value in C#
- Java Program to check if the String contains any character in the given set of characters
- Create a new string by alternately combining the characters of two halves of the string in reverse in C++ Program
- A football and a stone has same mass:I. Both have same inertia II. Both have same momentum III. Both have different inertia IV. Both have different momentum
- Check if string contains special characters in Swift
- Check if the characters of a given string are in alphabetical order in Python
- Python - Check if frequencies of all characters of a string are different
- Check if a string has all characters with same frequency with one variation allowed in Python
- Check if frequency of all characters can become same by one removal in Python
- Find if a string begins with a specific set of characters in Arduino
- Check if a string is entirely made of the same substring JavaScript

Advertisements