Need help with the program

+1 Luka Lopotar · February 9, 2015
Hello everyone!

I need to make an encoding program, but not the usual type of encoding. This is how it needs to work:

The user inputs a string which can only contain lower and/or upper case letters, and/or numbers. (without any kind of whitespaces, commas etc...)
Now, the program needs to count the same letters/numbers in a row and modify the string like in this example:

First example:
INPUT: ssppzrrrr
OUTPUT: 2s2p1z4r

Second example:
INPUT: ddddUUUU777777
OUTPUT: 4d4U67

Also, there are some restrictions:
- The program needs to use a function which prototype is: 
      void encode(char* s)
- I'm not allowed to make any other strings which can help me make the program, but I can make other functions if necessary


I had to make this program for my programming course one month ago, but I failed. This is bugging me ever since then.
I don't ask you to solve it for me (if you want you can), but I need some ideas on how to make this.

Post a Reply

Replies

Oldest  Newest  Rating
0 c student · February 10, 2015
i've seen many cases where people are restricted to their tools, even in my case when i took my first c course, however, the course i (and most probably the majority of the people) took was under the faculty of computer science and engineering where tool restriction allowed students to expand their mind and think of different methods to solve a particular solution.  the fact that this was also a first course meant that solutions wouldn't be the most clean and efficient but if it worked, you were on the right track, also meaning that it was a challenge to make nice code and once these restrictions were lifted, you would start to think about different methods an issue could be solved instead of typing down the first thing you knew and only knew.
0 c student · February 10, 2015
just to clarify, my code does meet the requirements - note the comment sections.  i've merely unlocked its potential.  :angel:
0 Lazar Bulic · February 10, 2015
Ok these solutions work but not for the requirements that were given. 


Luka wrote: 


Now, the program needs to count the same letters/numbers in a row and modify the string like in this example

- I'm not allowed to make any other strings which can help me make the program, but I can make other functions if necessary

So the solutions should edit the string that was passed. This complicates the things even more :) 
0 c student · February 9, 2015
here is my code, although it is incredibly messy and unclean

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

#define DEFAULT_SIZE 100
#define INCREMENT 2

char *readString (void);
void encode (char *);

int main (void) {

    encode (readString());
    
    return EXIT_SUCCESS;
    
}

// to be able to read in huge strings/text files/etc...
// if you want to read in files, remove the following:
/*
*  && c != '\n'
*/
// this is just the input reading, you can ignore this
// and replace it with fgets()
// DISCLAIMER: NOT TESTED
char *readString (void) {
    size_t length = 0;
    int c;
    char *input = malloc ((DEFAULT_SIZE * sizeof (char))+1);
    *input = '\0';
    // check for proper execution
    if (input == NULL) {
        fprintf (stderr, "Input buffer failed\n");
        exit (EXIT_FAILURE);
    }
    // read until user stops input
    while ((c = getchar()) != EOF && c != '\n') {
        // check and resize input buffer
        if (length >= sizeof (input)) {
            input = realloc (input, (INCREMENT * length * sizeof (char))+1);
            // check for proper execution
            if (input == NULL) {
                fprintf (stderr, "Input buffer failed\n");
                exit (EXIT_FAILURE);
            }
        }
        input[length++] = c;
    }
    // terminate input buffer
    input[length] = '\0';
    
    return input;
}

void encode (char *input) {
    int c, counter = 0, index = 0, pos = 0;
    char encoded[INCREMENT*strlen (input)+1];
    memset (encoded, 0, sizeof (encoded));
    
    // check entire input until end
    while (input[index] != '\0') {
       /*
       * disabled to read in everything
        // check alphanumeric
       while (!isalnum(input[index])) {
            index++;
        }
       */
       // read character
        c = input[index++];
        counter++;
        // if next letter is also the same
        if (input[index] == c) {
            // continue reading and counting
            while (input[index] == c) {
                counter++;
                index++;
            }
        }
        // put number into encoded array; avoid if able
        sprintf (encoded+pos, "%d", counter);
        // fix for Mathias's issue
        int digit = 0;
        while (counter != 0) {
            counter /= 10;
            digit++;
        }
        pos += digit;
        // put character after
        encoded[pos++] = c;
        // reset counter and repeat
        counter = 0;
    }
    
    // terminate array
    encoded [pos] = '\0';
    
    // free malloc
    free (input);
    
    puts (encoded);
    
}

here is my c source code encoded with your algorithm:


1#1i1n1c1l1u1d1e1 1<1s1t1d1i1o1.1h1>1
1#1i1n1c1l1u1d1e1 1<1s1t1d1l1i1b1.1h1>1
1#1i1n1c1l1u1d1e1 1<1s1t1r1i1n1g1.1h1>1
2/1#1i1n1c1l1u1d1e1 1<1c1t1y1p1e1.1h1>2
1#1d1e1f1i1n1e1 1D1E1F1A1U1L1T1_1S1I1Z1E1 11201
1#1d1e1f1i1n1e1 1I1N1C1R1E1M1E1N1T1 122
1c1h1a1r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n1c1o1d1e1d1 1a2r1a1y1;1 1a1v1o1i1d1 1i1f1 1a1b1l1e1
8 1s1p1r1i1n1t1f1 1(1e1n1c1o1d1e1d1+1p1o1s1,1 1"1%1d1"1,1 1c1o1u1n1t1e1r1)1;1
8 2/1 1f1i1x1 1f1o1r1 1M1a1t1h1i1a1s1'1s1 1i2s1u1e1
8 1i1n1t1 1d1i1g1i1t1 1=1 101;1
8 1w1h1i1l1e1 1(1c1o1u1n1t1e1r1 1!1=1 101)1 1{1
12 1c1o1u1n1t1e1r1 1/1=1 11101;1
12 1d1i1g1i1t2+1;1
8 1}1
8 1p1o1s1 1+1=1 1d1i1g1i1t1;1
8 2/1 1p1u1t1 1c1h1a1r1a1c1t1e1r1 1a1f1t1e1r1
8 1e1n1c1o1d1e1d1[1p1o1s2+1]1 1=1 1c1;1
8 2/1 1r1e1s1e1t1 1c1o1u1n1t1e1r1 1a1n1d1 1r1e1p1e1a1t1
8 1c1o1u1n1t1e1r1 1=1 101;1
4 1}1
4 1
4 2/1 1t1e1r1m1i1n1a1t1e1 1a2r1a1y1
4 1e1n1c1o1d1e1d1 1[1p1o1s1]1 1=1 1'1\101'1;1
4 1
4 2/1 1f1r2e1 1m1a2l1o1c1
4 1f1r2e1 1(1i1n1p1u1t1)1;1
4 1
4 1p1u1t1s1 1(1e1n1c1o1d1e1d1)1;1
4 1
1}1


so cryptic... :sick:  and colourful(?)! :silly:
0 Lazar Bulic · February 9, 2015
Luka can you provide the code that you made. To see where are u stuck.
0 Mathias Frits Rørvik · February 9, 2015
I made a solution, but it won't work if there is more than 9 consecutive characters in a row.

sorry for double post, forum is bugged.
0 Mathias Frits Rørvik · February 9, 2015

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

#define SIZE 255 //size of ascii

void encode(char *s);

int table[SIZE];

int main(void)
{
char input[100];
printf("INPUT: ");
scanf("%s", input);

encode(input);

printf("OUTPUT: %s\n", input);
return 0;
}

void encode(char *s)
{
int len = strlen(s); //get length of string
int i;


for (i = 0; i < len; i++) { //we can treat chars with their decimal value
//to index them in our table
table[s]++;
}

int pos = 0;
char c = ' ';
for (i = 0; i < len; i++) {
if (s != c) {
c = s;
s[pos++] = (char) (((int)'0')+ table);
s[pos++] = c;
}
}
s[pos] = '\0';
}
  • 1

C

107,270 followers
About

One of the most popular languages of all time.

Links
Moderators
Bucky Roberts Administrator