开发者

Char array sorting and removing duplicates

开发者 https://www.devze.com 2023-02-19 14:11 出处:网络
I am trying to do some array manipulations. I am doing char array sorting and duplicates removal here.

I am trying to do some array manipulations. I am doing char array sorting and duplicates removal here. Your comments are welcome. Havent done much testing and error handling here though.

#include<stdafx.h>
#include<stdlib.h>
#include<stdio.h>
#include<string>
using namespace std;

void sort(char *& arr)
{
    char temp;
    for(int i=0;i<strlen(arr);i++)
    {
        for(int j=i+1;j<strlen(arr);j++)
        {
            if(arr[i] > arr[j])
            {
            temp = arr[i];
            arr[i] = arr[j];
            arr[j] = temp;
            }
        }
    }

}
bool ispresent(char *uniqueArr, char * arr)
{
    bool isfound = false;
    for(int i=0;i<strlen(arr);i++)
    {
    for(int j=0;j<=strlen(uniqueArr);j++)
    {
        if(arr[i]== uniqueArr[j])
        {
        isfound = true;
        return isfound;
        }
        else
        isfound = false;
    }
    }

    return isfound;
}

char * removeduplicates(char *&arr)
{
    char * uniqqueArr = strdup(""); // To make this char array modifiable
    int index = 0;
    bool dup = false;
    while(*arr!=NULL)
    {       
     dup = ispresent(uniqqueArr, arr);
     if(dup == true)
     {}//do nothing
     else// copy the char to new char array.
     {
           uniqqueArr[index] = *arr;    
     index++;
     }
    arr++;
    }
    return uniqqueArr;
}
int main()
{
    char *arr = strdup("saaangeetha"); 
    // if strdup() is not used , access v开发者_JS百科iolation writing to 
          //location occurs at arr[i] = arr[j]. 
    //This makes the constant string modifiable
    sort(arr);
    char * uniqueArr = removeduplicates(arr);   

}


If you use std::string, your code (which is actually C-Style) can be written in C++ Style in just these lines:

#include <iostream>
#include <string>
#include <algorithm>

int main() {
        std::string s= "saaangeetha";
        std::sort(s.begin(), s.end());
        std::string::iterator it = std::unique (s.begin(), s.end()); 
        s.resize( it - s.begin());
        std::cout << s ;
        return 0;
}

Output: (all duplicates removed)

aeghnst

Demo : http://ideone.com/pHpPh

If you want char* at the end, then you can do this:

   const char *uniqueChars = s.c_str(); //after removing the duplicates!


If I were doing it, I think I'd do the job quite a bit differently. If you can afford to ignore IBM mainframes, I'd do something like this:

unsigned long bitset = 0;

char *arr = "saaangeetha";
char *pos;

for (pos=arr; *pos; ++pos) 
    if (isalpha(*pos))
        bitset |= 1 << (tolower(*pos)-'a');

This associates one bit in bitset with each possible letter. It then walks through the string and for each letter in the string, sets the associated bit in bitset. To print out the letters once you're done, you'd walk through bitset and print out the associated letter if that bit was set.

If you do care about IBM mainframes, you can add a small lookup table:

static char const *letters = "abcdefghijklkmnopqrstuvwxyz";

and use strchr to find the correct position for each letter.

Edit: If you're using C++ rather than C (as the tag said when I wrote what's above), you can simplify the code a bit at the expense of using some extra storage (and probably being minutely slower):

std::string arr = "saaangeetha";

std::set<char> letters((arr.begin()), arr.end());

std::copy(letters.begin(), letters.end(), std::ostream_iterator<char>(std::cout, " "));

Note, however, that while these appear the same for the test input, they can behave differently -- the previous version screens out anything but letters (and converts them all to lower case), but this distinguishes upper from lower case, and shows all non-alphabetic characters in the output as well.


char *arr = "saangeetha";

arr is pointing to read only section where string literal saangeetha is stored. So, it cannot be modified and is the reason for access violation error. Instead you need to do -

char arr[] = "sangeetha"; // Now, the string literal can be modified because a copy is made.
0

精彩评论

暂无评论...
验证码 换一张
取 消