'Is there a way to remove a character from a string in this program?
I am tasked with reading the first character in the sentence and count how many times that character occurs. I must then move on to the next character that has not been counted yet and
static void Main(string[] args)
{
char[] array = fullword.ToCharArray();
foreach (char ch in array)
{
Console.WriteLine(GetHowManyTimeOccurenceCharInString(fullword, ch));
}
}
public static int GetHowManyTimeOccurenceCharInString(string text, char c)
{
int count = 0;
foreach (char ch in text)
{
if (ch.Equals(c))
{
count++;
}
}
return count;
}
}
Solution 1:[1]
Ok here it is. Just a little program to show you how to do that. First convert your string into a Char arrar and add that to a List. Then you can remove chars from the list using the RemoveAt(n) method. Finally you change your list into a string again and voila there are "JackandJill" again.
using System;
using System.Collections.Generic;
using System.Text;
namespace RemoveChars
{
internal class Program
{
static void Main(string[] args)
{
string name = "Jack and Jill";
var list = new List<char>();
list.AddRange(name.ToCharArray());
list.RemoveAt(4);
list.RemoveAt(7);
var sb = new StringBuilder();
list.ForEach(x => sb.Append(x));
Console.WriteLine(sb.ToString());
Console.ReadKey();
}
}
}
Solution 2:[2]
I believe you are trying to get the number of character occurrences in a string.
This will give you a dictionary of character and count.
public Dictionary<char, int> GetNoOfOccurenceCharInString(string stringToCheck)
{
//Track the character and count in a Dictionary.
Dictionary<char, int> map = new Dictionary<char, int>();
//For String "Jack and Jill" it will store something like this in the memory
// j =2, a = 2, c = 1, k = 1, " " = 2, n = 1, d = 1, i = 1, l = 2
//Shift to Lower case and Loop through each character
foreach (var c in stringToCheck.ToLower())
{
if (c == ' ') //If the current char is a space do not count;
{
continue;
}
//Check if map already has the char, if yes use the count stored in the memory, else initialize to 0
var count = map.ContainsKey(c) ? map[c] : 0;
map[c] = count + 1; //Increment count by 1
}
return map;
}
To test this:
string name1 = "Jack";
string matches = "matches";
string name2 = "Jill";
string fullword = name1 + matches + name2;
//Call our method
var result = GetNoOfOccurenceCharInString(fullword);
//loop through each key in the Dictionary
foreach (var key in result.Keys)
{
//Print out the Key and the Count
Console.WriteLine($"{key}: {result[key]}");
}
Solution 3:[3]
The best way to do what you need is to create a counting HashMap. I will put this example made in java.
public static void main(String[] args) {
String name1 = "jack";
String matches = "matches";
String name2 = "jill";
String fullWord = name1 + matches + name2;
//Map to get the number of repeated characters
HashMap<Character, Integer> counts = new HashMap<>();
//New string without repeated characters
StringBuilder noRepeatingCharacter = new StringBuilder();
// Text string with the number of repeated characters
StringBuilder literalCounts = new StringBuilder();
for (int i = 0; i < fullWord.length(); i++) {
//We get the character to establish if it is repeated or not.
char charAr = fullWord.charAt(i);
//We verify if in our map the character exists
if (counts.containsKey(charAr)) {
// Update counts
counts.replace(charAr, counts.get(charAr) + 1);
} else {
// we add to our new string of characters only the non-repeated ones
noRepeatingCharacter.append(charAr);
// If the character does not exist in our map, we add it
counts.put(charAr, 1);
}
}
// So that we can print the count in order, we assign to our variable literalCounts
for (int i = 0; i < noRepeatingCharacter.length(); i++) {
char charAr = noRepeatingCharacter.charAt(i);
literalCounts.append(counts.get(charAr));
}
// Print counts
System.out.println("Counts: " + literalCounts);
}
Output Output
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Paul Sinnema |
| Solution 2 | |
| Solution 3 | C_the_best |
