Home > database >  How to change one string to another with different sizes
How to change one string to another with different sizes

Time:11-01

I have a task to do. I have to work with strings. I will show you the input and output, because I think that will be clear what the task is.

Input: "aaa bbuvvvvo"

Output: "a$3 b$2uv$4o"

If there is the same symbols, I have to leave that symbol and then put dollar sign '$' and an integer of how many same signs was there. I am stuck on the spot, where I have to change string without losing any information.

I will leave my code here, it might help.

#include <stdio.h>
#include <string.h>
#define CAPACITY 255
#define MAX_NUMBER 10

void Output(char readFile[], char outputFile[], char string[]);
void changeString(char string[], char newString[]);

int main() {

    char readFile[CAPACITY];
    char outputFile[CAPACITY];
    char string[CAPACITY];

    // Input file's name
    printf("Iveskite teksto failo varda: ");
    scanf("%s", &readFile);

    // Output file's name
    printf("Iveskite teksto faila i kuri bus isvedamas atsakymas: ");
    scanf("%s", &outputFile);


    Output(readFile, outputFile, string);
    return 0;
}

// Scanning file
void Output(char readFile[], char outputFile[], char string[])
{
    char newString[CAPACITY];
    FILE *input, *output;
    input = fopen(readFile, "r");

    while(fscanf(input, "%s", string) != EOF)
    {
        changeString(string, newString);
        printf("%s\n", newString);
    }
}

// Changing string to wanted string
void changeString(char string[], char newString[])
{
    char number[MAX_NUMBER];
    int symbols = 0;
    int j;

    for(int i = 0; string[i] != '\0';   i)
    {
        int temp = i;
        newString[i] = string[i];
        if(newString[i] == string[i   1])
        {
            j = i;
            while(string[j] == string[i])
            {
                  symbols;
                  j;
            }
            // Changing int to char
            sprintf(number, "%d", symbols);
            newString[i   1] = '$';
            i  = 2;
            newString[i] = number[0];
            symbols = 0;
        }
    }
}

I have tried to do that with function called changeString, but I get the wrong output all the time. Also the input I am getting is from .txt file.

EDIT: When I compiling this program right now, I get a$3 b$2v$4vo that output.

CodePudding user response:

For starters this declaration in main

char string[CAPACITY];

does not make sense.

You should declare variables in scopes where they are used.

The variable string is used in the function Output where it is should be declared.

The function changeString should be declared like

void changeString( const char string[], char newString[]);

because the source string is not changed within the function.

Your function has several bugs.

For example it does not build a string in the array newString because it does not append the stored sequence in the array with the terminating zero character '\0'.

Secondly this increasing of the variable i

i  = 2;

in general is invalid. You need to add to the variable i the number of repeated characters in the source string.

Or the number of repeated characters change greater than or equal to 10. In this case this statement

newString[i] = number[0];

will not produce correct result.

The function can be defined the following way as shown in the demonstration program below.

#include <stdio.h>

#define CAPACITY 255

void changeString( const char string[], char newString[] )
{
    while ( *string )
    {
        *newString   = *string;

        size_t n = 1;
        while (*  string == *( newString - 1 ) )   n;

        if (n != 1)
        {
            *newString   = '$';
            int count = sprintf( newString, "%zu", n );
            newString  = count;
        }
    }

    *newString = '\0';
}

int main( void )
{
    char string[CAPACITY] = "aaa bbuvvvvo";
    char newString[CAPACITY];

    changeString( string, newString );

    puts( newString );
}

The program output is

a$3 b$2uv$4o
  • Related