Printing out unique members of a char array.

  • (2 Pages)
  • +
  • 1
  • 2

26 Replies - 2092 Views - Last Post: 25 November 2012 - 01:25 PM Rate Topic: -----

#1 GloomyDawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-November 12

Printing out unique members of a char array.

Posted 25 November 2012 - 03:30 AM

Hello everyone. I'm a begginer at c++ and I'm completely lost. Basically I've got a char array with letters and I need to print out only unique letters. For example I've got an array
 char array[] = "ABABCABC"; 
and I need to print out only unique letters (in this case only ABC). I've been trying to write different codes but none worked at all so theres no point to show them here. Any help or hints would be greatly appreciated.

Is This A Good Question/Topic? 0
  • +

Replies To: Printing out unique members of a char array.

#2 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 03:51 AM

Some of the most basic ideas would be :
- Create a new array. Then, iterate through your original character and for every character not in the new array, add it to the new array. If it already exists in new array, ignore it. Then print the new array.
- Before printing any character, check if it has occurred in the array before. If it has, then skip over. If it has not, then print it. You can run a loop from array[0] to array[i-1] to check if array[i] has occurred before in the array or not.
Was This Post Helpful? 1
  • +
  • -

#3 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 03:52 AM

You can use strstr(), also you can try strpbrk() which is a bit different...

Well you can use functions from string.h...

Hope this Helps!
Was This Post Helpful? 1
  • +
  • -

#4 GloomyDawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-November 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 04:20 AM

View PostAKMafia001, on 25 November 2012 - 03:52 AM, said:

You can use strstr(), also you can try strpbrk() which is a bit different...

Well you can use functions from string.h...

Hope this Helps!


As far as I understand these functions help me find the characters that I declare. I need to print out unique characters from a random array of characters. I kinda figured it out with the aditionnal array, but I want to do it as simply as possible, hopefully without using other arrays. Anyways. Thanks for the help. I'll keep trying to make it work.
Was This Post Helpful? 0
  • +
  • -

#5 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 04:57 AM

The C++ Standard Library way of doing this task is to use the methods available in <algorithm> header like unique or unique_copy.

Just note that the container must be sorted first.

This post has been edited by Anarion: 25 November 2012 - 04:57 AM

Was This Post Helpful? 1
  • +
  • -

#6 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 06:32 AM

Well! It is pretty simple to use the second array...

All you need to do is:
Char Array of original string
Char Array of string you want to search

Loop {
If OriginalArray[indexI] == SearchArray[IndexJ]
    IndexFound = indexI;
}



If a first match is found, increment the indexes to check if the next element also matches... If it doesn't, reset the Index of the SearchArray and search further in the OriginalArray. Remember to reset the IndexFound too... Once a match is found and IndexJ is equal to the size of the search string, you can break out of the loop and have the exact FoundIndex...

If you are confused, you can ask...

Hope this Helps!
Was This Post Helpful? 1
  • +
  • -

#7 GloomyDawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-November 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 08:22 AM

View PostAKMafia001, on 25 November 2012 - 06:32 AM, said:

Well! It is pretty simple to use the second array...

All you need to do is:
Char Array of original string
Char Array of string you want to search

Loop {
If OriginalArray[indexI] == SearchArray[IndexJ]
    IndexFound = indexI;
}



If a first match is found, increment the indexes to check if the next element also matches... If it doesn't, reset the Index of the SearchArray and search further in the OriginalArray. Remember to reset the IndexFound too... Once a match is found and IndexJ is equal to the size of the search string, you can break out of the loop and have the exact FoundIndex...

If you are confused, you can ask...

Hope this Helps!


Well first of all thank you for replying, but
I'm sorry maybe I asked wrong. I think that I understand what you're telling me and its finding elements that you declare in the second array, right? What I need to do is to print out elements of an array only once. As I've said before if I have elements A B B B A C I need to print out A B C ignoring duplicated. My program isn't supposed to know what elements are repeating. It reads the data from a text file and then prints out all the capital letters but I need to print them out only once.
Was This Post Helpful? 0
  • +
  • -

#8 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 10:25 AM

Well! You are right, I got something else...

I think, you want to identify different elements. If something is repeating then it is ignored, and if something is new, insert it into the list...

Is that what you mean?
Was This Post Helpful? 1
  • +
  • -

#9 GloomyDawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-November 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 10:33 AM

View PostAKMafia001, on 25 November 2012 - 10:25 AM, said:

Well! You are right, I got something else...

I think, you want to identify different elements. If something is repeating then it is ignored, and if something is new, insert it into the list...

Is that what you mean?


I want to print out individual elements only once even if there are many of them. My program should read capital letters from a text file and then print out the letters only once.
Was This Post Helpful? 0
  • +
  • -

#10 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 10:48 AM

Take this then: You have to use a data structure to hold the unique elements so that you can check the array elements against it.
You haven't given any more information about the situation, but I'm assuming this is for an assignment and you may be forbidden from using STL algorithms? What about data structures? Can you use a Vector?
Was This Post Helpful? 1
  • +
  • -

#11 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 10:54 AM

Well! Then you have to keep track of the letters you have read... And when you read the next letter you have to check if it has already been read.

It would be easier if you maintain a std::vector and insert every new element into it... Whenever you read a new element, just search it in the std::vector if it already exists... If the vector contains less items, then you can use STL algorithms like find and find_if. But if you have many elements in your vector, then for better performance you can sort it and use a search algorithm like binary search or any other...

PS: Vectors can expand automatically, so you don't have to worry about the size...

Hope this Helps!
Was This Post Helpful? 1
  • +
  • -

#12 Anarion  Icon User is offline

  • The Persian Coder
  • member icon

Reputation: 282
  • View blog
  • Posts: 1,456
  • Joined: 16-May 09

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 11:01 AM

View PostAKMafia001, on 25 November 2012 - 09:24 PM, said:

Well! Then you have to keep track of the letters you have read... And when you read the next letter you have to check if it has already been read.

It would be easier if you maintain a std::vector and insert every new element into it... Whenever you read a new element, just search it in the std::vector if it already exists... If the vector contains less items, then you can use STL algorithms like find and find_if. But if you have many elements in your vector, then for better performance you can sort it and use a search algorithm like binary search or any other...

PS: Vectors can expand automatically, so you don't have to worry about the size...

Hope this Helps!

If any of the STL algorithms are to be used, then using unique (and ofcourse sort before it) seems better.
Was This Post Helpful? 0
  • +
  • -

#13 GloomyDawn  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 25-November 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 11:10 AM

View PostAnarion, on 25 November 2012 - 10:48 AM, said:

Take this then: You have to use a data structure to hold the unique elements so that you can check the array elements against it.
You haven't given any more information about the situation, but I'm assuming this is for an assignment and you may be forbidden from using STL algorithms? What about data structures? Can you use a Vector?


You are correct. This in university homework in a course called "Programming basics". We haven't learned vectors yet nor data structures. :\
Was This Post Helpful? 0
  • +
  • -

#14 AKMafia001  Icon User is offline

  • </code.in.dream>

Reputation: 187
  • View blog
  • Posts: 624
  • Joined: 11-June 11

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 11:24 AM

I assume you have some knowledge of fixed size arrays...

You can create a char array of large size, maybe char chArray[256];
If a new item is found, insert it to the array and keep track of the current index being used...
Read new item, now you have to search, the simplest way is:
for( i = 0; i < currentIndex; i++) {
    if(chArray[i] == item)
        return false; 
}
chArray[currentIndex+1] = item;
currentIndex++;
// or simply, chArray[currentIndex++] = item;

return true;



I think that's all, in the end chArray is filled up with what you need...

Hope this Helps!

Edit: I have modified the snippet, and now is considered to be a part of function which is supposed to return bool, if item already exists it returns false, else true... You can change it as you want...

This post has been edited by AKMafia001: 25 November 2012 - 11:31 AM

Was This Post Helpful? 1
  • +
  • -

#15 aresh  Icon User is offline

  • It's a 16-Bit World!
  • member icon

Reputation: 273
  • View blog
  • Posts: 4,163
  • Joined: 08-January 12

Re: Printing out unique members of a char array.

Posted 25 November 2012 - 11:29 AM

In your solution, the starting value of currentIndex must be -1. Won't it be better to simply use chArray[currentIndex] = item;?
Was This Post Helpful? 2
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2