Home > Blockchain >  Is there a way to remove a character from a string in this program?
Is there a way to remove a character from a string in this program?

Time:03-12

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 I must repeat until all characters in the sentence have been counted and I have a number showing how many times each character occurred in the sentence.

The problem I am facing is that the letter does not get removed when counted the first time. The sentence is Jack matches Jill and the number I need to get for the number of times a character occurs is 22211111112 but the number I am getting is 222112121112122. This second number shows a repeat of some characters being counted.

static void Main(string[] args)
        {
            string name1 = "Jack";
            string matches = "matches";
            string name2 = "Jill";
            string fullword = name1   matches   name2;

            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;
        }

    }

CodePudding user response:

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();
        }
    }
}

CodePudding user response:

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()) 
        {
            //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;
    }
  •  Tags:  
  • c#
  • Related