Question get the number of special characters?

programadoro

New member
Joined
Apr 25, 2020
Messages
4
Programming Experience
5-10
how do i get the number of special characters my phrase has, and show them what they are?
how do i show my sentence without the special characters on a single line?
as i read all four notes on a single line, because i'm great at c language, and i know i can use a single scanf for that.

C#:
using System;
namespace ConsoleApp1
{
    class Program
    {
        static void Main(string[] args)
        {
            //especificando caracteres exclusivos
            string[] separatingStrings = { "<<", "...", "<" };
            //escrevendo uma frase qualquer
            string text = "one<<two......three<four";
            //imprimindo a frase
            System.Console.WriteLine($"Original text: '{text}'");
            //separando texto dos caracteres exclusivos
            string[] words = text.Split(separatingStrings, System.StringSplitOptions.RemoveEmptyEntries);
            //quantidade de palavras no texto sem os caracteres exclusivos
            System.Console.WriteLine($"{words.Length} Palavras no texto:");
            //imprimindo texto sem os caracteres
            foreach (var word in words)
            {
                System.Console.WriteLine(word);
            }
        }
    }
}

C#:
using System;

namespace ConsoleSharp
{
    class Program
    {
        static void Main()
        {
            int nota1, nota2, nota3, nota4, media;
            Console.WriteLine("Entre com 4 notas !");
            //ReadLine() retorna uma String, por isso, é preciso converter.
            nota1 = Convert.ToInt32(Console.ReadLine());
            nota2 = Convert.ToInt32(Console.ReadLine());
            nota3 = Convert.ToInt32(Console.ReadLine());
            nota4 = Convert.ToInt32(Console.ReadLine());
            media = (nota1 + nota2 + nota3 + nota4) / 4;
            Console.WriteLine("MEDIA:" + media);
        }
    }
}
 
Last edited by a moderator:
Hello, and welcome to the forums.

Please use code tags when posting to the forums. I've edited your post and added them for you this time.

I'd imagine someone with 5-10 years experience would have known the answer to your questions. Do you program in other languages?
 
What's the problem with the first block of code?

Can you explain what the problem with the code you have is, and what you are expecting to happen, and what is actually happening? Often what people write in code is not what they want to do.

In regards this : System.Console.WriteLine($"{words.Length} Palavras no texto:"); - Are you trying to get how many chars in total or how many words?
If trying to get how many chars, you need to add each index up from each position in the string[] by calling .Length on the index [position index here].Length.
show my sentence without the special characters on a single line?
Create a variable outside for each statement and use String.Concat(); to combine them with the variable in your foreach statement from inside the foreach statement.
 
This puts them all on one line. You are only missing some string merging by calling string.Concat().
C#:
            string result = string.Empty;
            foreach (var word in words)
            {
                result = string.Concat(result, " ", word);
                Console.WriteLine(result);
            }
And If I am understanding your question properly, this will tell you how many chars are in each position in the string array :
C#:
Console.WriteLine($"Length { separatingStrings[0].Length + separatingStrings[1].Length + separatingStrings[2].Length }");
To also show each one, write out their positions : separatingStrings[0] If this isn't quite what you're asking, let me know and I will check in later when I will have more time. ;)

Gotta rush out, stuff to do.
 
how do i show my sentence without the special characters on a single line?
You can also use Write method instead of WriteLine.
how do i get the number of special characters my phrase has,
With your splitted string the count should be one less than the number of strings in array.
 
What's the problem with the first block of code?
the problem is that after saying in the code which characters will be special in a sentence. I can't tell you how many special characters the phrase has and what they are.
Create a variable outside for each statement and use String.Concat(); to combine them with the variable in your foreach statement from inside the foreach statement.




could you show me an example?


yes i'm from brazil and i want to learn this programming language because it makes a lot of money
 
I just did on post #4

With your splitted string the count should be one less than the number of strings in array.
While the question is a bit muddled, I think mostly because of language translation. Doing as you suggested would only count 3 times as this will count how many items are in the separatingStrings array. Or am I just misunderstanding you again?

I thought @programadoro wanted to know how many special chars are in the string array. But based on his last reply, it sounds like it's going to get more complicated as OP doesn't know how many special chars there will be. Regex perhaps @JohnH?
 
C#:
string result = string.Empty;
foreach (var word in words)
{
result = string.Concat(result, " ", word);
Console.WriteLine(result);
}
And If I am understanding your question properly, this will tell you how many chars are in each position in the string array :
C#:
Console.WriteLine($"Length { separatingStrings[0].Length + separatingStrings[1].Length + separatingStrings[2].Length }");
thankyou
 
Or am I just misunderstanding you again?
I don't know (Spanish), just guessing :rolleyes: I try to make it a habit myself to use English in my own code, both in element names and code comments, all too often I mix my native language and English when quickly throwing together something, result is mess to read even for me, so I have to go over it again renaming and translating.

Then there is the Join method:
C#:
Console.WriteLine($"{words.Length} Palavras no texto: {String.Join(",", words)}");
 
That's much neater than looping while there is no difference in performance, I prefer string.Join to the foreach iterator.

There is another issue @JohnH. He doesn't know which special chars may be in the string[] -- I was thinking it might be best using regex for checking for a specific pattern of chars which he knows will be in the string array. Yes he could use Linq also, but I think this would be a job best for Regex? Your thoughts on this, as my brain in not in the right gear atm.

And @programadoro you're welcome.
 
What defines a special character? Is it any character that is not alphanumeric, and is not a whitespace? Or is there a specific set of characters that are considered special characters?

Right now, based on the code from the original post, it looks like it is actually special strings separatingStrings, not special characters that he is concerned about.
 
Back
Top Bottom