-4

i am having some problems with a buffer. Short story, i have to iterate over the lines in a text file, in which each line has information separated by an empty space, the problem is, the informartion can have an space in it so i wrote a code that check all the empty spaces of a string and checks if its a sperator, and if it is, ut replaces it by a ";".The problem: I write this to another var in where i use malloc to allocate its space, but it ends printing garbage, can somebody point me what's wrong in the code?

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


int main(){
    int i;
    char *destination;
    char* str = "S:15 B:20 B A:15",letra;
    destination = (char *)malloc(strlen(str)*sizeof(char));
    for (i=0;i<strlen(str);i++){
            printf("%c \n",str[i]);
            letra = str[i];
            if (i == 0){
                destination[i] = letra;
            }
            else if (letra != ' '){
                destination[i] = letra;
            }
            else if (letra == ' ' ){
                if (isdigit(str[i-1])){
                    destination[i] = ";";
                }
                else{
                    destination[i] = letra;
                }
            }
    }
    printf("%s",destination);
    return 0;
}
NyxTheShield
  • 69
  • 1
  • 9
  • You got the amount of memory needed to store a string of length `strlen(str)` wrong. – Jongware Jul 08 '14 at 21:06
  • Also, `i == 0` should *not* be a special case. (You might have gotten confused there over the use of `\0` inside a string.) – Jongware Jul 08 '14 at 21:07
  • 1
    First character might be an empty space, it would check for str[-1] if i dont add that special case. What would be the correct size of the string? – NyxTheShield Jul 08 '14 at 21:09
  • Add 1 for the terminating zero (which you also need to add!). I think I'd prefer a double check `if (i && letra == ' ')` for the second q. Unless, of course, you are sure your input format is consistent like this. – Jongware Jul 08 '14 at 21:13
  • Please Read: [Don't cast the result of malloc (and friends)](http://stackoverflow.com/questions/605845) Also, `sizeof(char)` is a bad sign, `char` is always exactly one `char` big, no more nor less. – Deduplicator Jul 08 '14 at 21:16
  • Okay, this is almost fixed, my problem is that i doesnt append the char consitently, it adds a random separator :S (I say destination[i] = '_', and it ends adding a [ ) – NyxTheShield Jul 08 '14 at 21:22
  • A-ha, you are using `destination[i] = ";"` -- double quotes. Single characters use single quotes. – Jongware Jul 08 '14 at 21:24
  • Oh god i can't belieive it is not the same hahahaha, thanks, it works perfectly now. – NyxTheShield Jul 08 '14 at 21:32

1 Answers1

0

Here is how I would do it -- a simple, one-directional loop copying just the characters needed, discarding spaces and inserting a ; where necessary.

The space to store a string of length x in (according to strlen) is x+1 -- one extra position for the additional ending zero.

The check of when to insert a ; is easy: after initially skipping spaces (if your input string starts with those) but before any valid text is copied, d will still be 0. If it is not and you encountered a space, then insert a ;.

Only one scenario is not checked here: if the input string ends with one or more spaces, then the final character in destination will also be a ;. It can be trivially discarded by checking just before the Copy loop #3, or just before ending at #4 (and you need to test if d != 0 as well).

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

int main (void)
{
    int i, d;
    char *destination;
    char *str = "S:15 B:20 B A:15";

    destination = malloc(strlen(str)+1);

    i = 0;
    d = 0;

    while (str[i])
    {
    /* 1. Skip spaces at the start */
        while (isspace (str[i]))
            i++;
    /* 2. Do we need to add a ';' here? */
        if (d)
        {
            destination[d] = ';';
            d++;
        }
    /* 3. Copy while not space or -zero- */
        while (str[i] && !isspace (str[i]))
        {
            destination[d] = str[i];
            d++;
            i++;
        }
    }
    /* 4. Close off. */
    destination[d] = 0;

    printf ("%s\n", destination);

    return 0;
}
Jongware
  • 22,200
  • 8
  • 54
  • 100
  • 1
    Thanks, i ended using my code but this works like a charm too (and its a great guidance too), thanks for everything ^^ – NyxTheShield Jul 08 '14 at 21:41