0

I'm doing an exercise that says I have to enter a phrase and put it in array, then I have to delete all the repeated characters and show the new phrase. I did it like this but I don't know how to get char from string array and put it into another string array.

PS: I must only use the basics of C# and arrays :(

namespace Exercice3
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Entrez une phrase SVP!!");
            string phrase = Console.ReadLine();
            string[] phraseArray = new string[]{ phrase };
            string[] newPhrase = new string[phrase.Length];

            for (int i = 0; i <= phrase.Length - 1; i++)
            {
                for (int j = 1; j <= phrase.Length - 1; j++)
                {
                    if (phraseArray[i] != phraseArray[j])
                        newPhrase = phraseArray[i]; //Probleme here 
                }
            }
            Console.WriteLine(newPhrase);
        }
    }
}
4
  • newPhrase.Add(pharseArray[i]) this add new item into array. = only set new array into newParse and phraseArray[i] is not an array i guess. Commented Jan 12, 2019 at 0:26
  • Is it possible you're suppose to use the string as a character array (char[]), and not as an array of strings? Commented Jan 12, 2019 at 0:28
  • string[] does not contain a definition for "add.. Commented Jan 12, 2019 at 0:31
  • ah so you need add it with index stackoverflow.com/questions/1440265/… Commented Jan 12, 2019 at 0:33

3 Answers 3

0

something like this would do it. You don't have to do it this way...

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        var newColl = new List<char>();

        foreach(char c in "aaabbbccc".ToCharArray())
        {
            if (!newColl.Contains(c))
                newColl.Add(c);
        }
        Console.WriteLine(new string(newColl.ToArray()));
    }
}

Output:

abc

Array-only method

using System;
using System.Collections.Generic;

public class Program
{
    public static void Main()
    {
        const string orig = "aaabbbcccddd";
        int origLen = orig.Length;
        char[] newArr = new char[origLen]; // get max possible spots in arr
        int newCount = 0;


        for(int i = 0; i < origLen; i++)
        {
            bool yes = false;

            for(int j = 0; j < newCount + 1; j++)
            {

                if (newArr[j] == orig[i])
                {    
                    yes = true;
                    break;
                }

            }

            if (!yes)
            {
                newArr[newCount] = orig[i];
                newCount++;
            }
        }
        Console.WriteLine(new string(newArr));
    }
}

Output:

abcd

Sign up to request clarification or add additional context in comments.

3 Comments

i know but my teacher wil not accept that because he says that we have just use the basics of c# like if and else statement, loops and arrays
I am glad - you have good teacher. If I have time, I add second solution
Thank you i started to understand a little bit, really thank you, you helped me a lot
0

The main problem here is that you are using arrays of strings. That is inappropriate because you are trying to iterate the characters of the string.

You need to construct your array of characters like this:

char[] phraseArray = phrase.ToCharArray();

This should allow you the ability to iterate the set of characters, check for duplicates, and form the new character array.

3 Comments

how can i do that without using TocharArray() ?
can you show me how can i do that ("This should allow you the ability to iterate the set of characters, check for duplicates, and form the new character array.") ??
@RedaTaha You can simply access chars of string by index: char myChar = phrase[indexNumber];. Doing so for each of the chars and inserting it into phraseArray if the char has not already been inserted will give you what is needed. Arrays are fixed-length, but since you are removing duplicate chars, the new array will be of shorter or equal size than original one, so just make it the same size as phrase.
0

The reason why you're getting an IndexOutOfRangeException is because if you look at the two arrays:

string[] phraseArray = new string[]{ phrase };

And

string[] newPhrase = new string[phrase.Length];

The length of both arrays are completely different, phraseArray has a length of 1 and newPhrase will be set to the length of your phrase, so if you insert a world of more than 1 character both arrays will not match and then either:

if (phraseArray[i] != phraseArray[j]) 

Or

newPhrase = phraseArray[i]; 

Will fail, specially newPhrase = phraseArray[i];, because here you're trying to replace newPhrase an array, for a character phrase[i]. Basically your solution won't work. So what you can do is do what @Travis suggested, basically change your arrays from String[]to char[], then:

char[] phraseArray = phrase.ToCharArray();

Or Instead of using arrays you can just use another string to filter your phrase. The first string being your phrase and the second would be the string you'd add your non repeated characters to. basically this:

Console.WriteLine("Entrez une phrase SVP!!");
string phrase = Console.ReadLine();
string newPhrase = ""; 

// loop through each character in phrase        
for (int i = 0; i < phrase.Length; i++) {
   // We check if the character of phrease is within our newPhrase, if it isn't we add it, otherwise do nothing
   if (newPhrase.IndexOf(phrase[i]) == -1) 
        newPhrase += phrase[i]; // here we add it to newPhrase
}

Console.WriteLine(newPhrase);

Don't forget to read the comments in the code.

Edit:

Based on the comments and suggestions given I implemented a similar solution:

Console.WriteLine("Entrez une phrase SVP!!");
char[] phrase = Console.ReadLine().ToCharArray();
char[] newPhrase = new char[phrase.Length]; 
int index = 0;

// loop through each character in phrase        
for (int i = 0; i < phrase.Length; i++) {
  // We check if the character of phrease is within our newPhrase, if it isn't we add it, otherwise do nothing
  if (Array.IndexOf(newPhrase, phrase[i]) == -1) 
      newPhrase[index++] = phrase[i]; // here we add it to newPhrase
}

Console.WriteLine(newPhrase);

5 Comments

Strings are immutable--the contents of a string object cannot be changed after the object is created. learn.microsoft.com/en-us/dotnet/csharp/language-reference/… Let him have an array and modify the values at each index.
Nvm I see your point. But don't you think using two strings is the same as using two char arrays, literally. So in theory wouldn't it be the same thing?
@Ghukas not sooo immutable, i give you pointers, unsafe, and reflection ;)
@Bargros It's not 2 strings, it's as many strings as the number of times the if statement's condition is satisfied. In case of array the "waste" is phrase.Length - number_of_unique_chars chars. Also I'd not use .IndexOf() but it doesn't really matter.
Yep I see your point, arrays would be a better alternative, I was being a bit lazy there, thanks for the pointer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.