Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
C# Program to Read a String and Find the Sum of all Digits
C# is a popular object-oriented programming language used to develop Windows applications, web applications, and games. In this article, we will discuss how to write a C# program to read a string and find the sum of all digits present in the string.
Approach
To find the sum of digits in a string, we need to iterate through each character, check if it's a digit using char.IsDigit(), and convert it to an integer to add to our running sum.
Using char.IsDigit() Method
The char.IsDigit() method determines whether a character represents a decimal digit. Here's the complete program
using System;
class Program {
static void Main(string[] args) {
string inputString = "Hello123World456";
int sum = 0;
Console.WriteLine("Input string: " + inputString);
foreach (char c in inputString) {
if (char.IsDigit(c)) {
sum += int.Parse(c.ToString());
Console.WriteLine("Found digit: " + c);
}
}
Console.WriteLine("The sum of digits in the string is: " + sum);
}
}
The output of the above code is
Input string: Hello123World456 Found digit: 1 Found digit: 2 Found digit: 3 Found digit: 4 Found digit: 5 Found digit: 6 The sum of digits in the string is: 21
Using Character Arithmetic
An alternative approach is to use character arithmetic by subtracting '0' from the digit character
using System;
class Program {
static void Main(string[] args) {
string inputString = "ABC789XYZ";
int sum = 0;
Console.WriteLine("Input string: " + inputString);
foreach (char c in inputString) {
if (char.IsDigit(c)) {
sum += c - '0'; // Convert char to int using ASCII arithmetic
}
}
Console.WriteLine("Sum of all digits: " + sum);
}
}
The output of the above code is
Input string: ABC789XYZ Sum of all digits: 24
Using LINQ Approach
For a more concise solution, we can use LINQ to filter digits and calculate the sum
using System;
using System.Linq;
class Program {
static void Main(string[] args) {
string inputString = "Test123String456End";
int sum = inputString
.Where(char.IsDigit)
.Sum(c => c - '0');
Console.WriteLine("Input string: " + inputString);
Console.WriteLine("Sum of digits: " + sum);
}
}
The output of the above code is
Input string: Test123String456End Sum of digits: 21
Comparison of Approaches
| Approach | Readability | Performance | Description |
|---|---|---|---|
| char.IsDigit() + int.Parse() | Good | Slower | Most readable but involves string conversion |
| char.IsDigit() + ASCII arithmetic | Good | Fast | Direct conversion using character values |
| LINQ | Excellent | Moderate | Most concise but requires System.Linq |
Conclusion
In this article, we explored multiple ways to find the sum of digits in a C# string. The char.IsDigit() method effectively identifies digit characters, while character arithmetic (c - '0') provides the fastest conversion from char to int. Choose the approach that best fits your needs based on readability and performance requirements.
