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);
        }
    }
}
Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
  • 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. – daremachine Jan 12 '19 at 00:26
  • Is it possible you're suppose to use the string as a character array (`char[]`), and not as an array of strings? – Jonathon Chase Jan 12 '19 at 00:28
  • string[] does not contain a definition for "add.. –  Jan 12 '19 at 00:31
  • ah so you need add it with index https://stackoverflow.com/questions/1440265/how-to-add-a-string-to-a-string-array-theres-no-add-function – daremachine Jan 12 '19 at 00:33

3 Answers3

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.

Travis J
  • 81,153
  • 41
  • 202
  • 273
  • how can i do that without using TocharArray() ? –  Jan 12 '19 at 00:33
  • 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.") ?? –  Jan 12 '19 at 00:46
  • @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`. – Lukas G Jan 12 '19 at 00:51
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

T.S.
  • 18,195
  • 11
  • 58
  • 78
  • 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 –  Jan 12 '19 at 00:54
  • I am glad - you have good teacher. If I have time, I add second solution – T.S. Jan 12 '19 at 00:55
  • Thank you i started to understand a little bit, really thank you, you helped me a lot –  Jan 12 '19 at 01:30
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);
Bargros
  • 521
  • 6
  • 18
  • Strings are immutable--the contents of a string object cannot be changed after the object is created. https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/string Let him have an array and modify the values at each index. – Lukas G Jan 12 '19 at 01:01
  • 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? – Bargros Jan 12 '19 at 01:06
  • 2
    @Ghukas not sooo immutable, i give you pointers, unsafe, and reflection ;) – TheGeneral Jan 12 '19 at 01:12
  • @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. – Lukas G Jan 12 '19 at 01:19
  • Yep I see your point, arrays would be a better alternative, I was being a bit lazy there, thanks for the pointer. – Bargros Jan 12 '19 at 01:25