0

In a text file I have "abbcccdddd". I want to store "abcd" in an array.

Before: tx[0] = a, tx[1] = b, tx[3] = c, tx[6] = d

After: tx[0] = a, tx[1] = b, tx[2] = c, tx[3] = d

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void main() 
{
    FILE *fp = fopen("D:\\C#\\Zip\\Text001.txt", "r");
    char x;
    int size;
    int j, k;
    int i = 0;

    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    fseek(fp, 0, SEEK_SET);
    char tx[size];
    x = fgetc(fp);

    while (x != EOF)
    {
        tx[i] = x;
        printf("%c", tx[i]);
        x = fgetc(fp);
        i++;
    }
}
4
  • 2
    Why is the output abc and not abcd? Commented Nov 30, 2016 at 18:27
  • 1
    Your attempt appears to be no attempt at all. It simply reads in the data from a file. That gets you to the starting point you describe. Commented Nov 30, 2016 at 18:38
  • 2
    It would be easier to de-duplicate as you read the characters from the file, so that the array never has runs of duplicate characters in the first place. You could do that with a small modification to what you already have. Commented Nov 30, 2016 at 18:41
  • 1
    If the input is aabbccaabbcc, do you want the output to be abcabc or abc? The first is a lot easier than the second. Commented Nov 30, 2016 at 21:43

2 Answers 2

1

remove_repeatation() will do this.

void remove_repeatation(char *str)
{
    char flag[256] = {0}; //Assuming sizeof char is 8. So max 256 different characters.
    int  i            = 0;
    int  j            = 0;

    for(i=0; str[i] != '\0';)
    {
        if(0 == flag[str[i]]) //Check if character is already found.
        {
            flag[str[i]] = 1; //If a character is found for the first time, enable corresponding flag.
            i++; //Go to next byte in the array.
        }
        else
        {
            for(j=i; str[j] != '\0'; j++)
                str[j] = str[j+1]; //If repeated character, shift the array entries to 1 byte left.
        }
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

FYI: Someone made a suggestion to simplify your code. See here.
@Aniket Khaire: I don't know why I am not able to approve your edits. Please post it as a separate answer. It is better than my solution.
The review was completed when three reviewers rejected it due to the substantial changes to the code.
0

Edit to above code by MayurK:

char* remove_repeatation(char *str)
{
    char flag[256] = {0}; //Assuming sizeof char is 8. So max 256 different characters.
    int  i            = 0;
    int  j            = 0;

    for(i=0; str[i] != '\0'; i++)
    {
        if(0 == flag[str[i]]) //Check if character is already found.
        {
            flag[str[i]] = 1; //If a character is found for the first time, enable corresponding flag.
            str[j] = str[i];
            j++;
        }
    }
    str[j] = '\0';
    return *str;
}

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.