-2

I want to split an input array into three different ones based on a / as a delimiter.

I have tried the (probably the naive) approach of storing an input string into different arrays by using getchar and while to read in the characters into an array and using a counter to count how many times a / appears.

Based on this number I would use:

if (slashcounter == 0) {
      destinationarray[i++] = c;
}

to store it into the proper array. full implementation below.

please note that I try to do this using only stdio.h

#include <stdio.h>

char c; 
char replace[80], toBeReplaced[80], input[80], testInput[80];
int i = 0;
int slashcounter = 0;

int main(){

    puts("Enter a line of text: ");

    while (( c = getchar()) != '\n'){

        if (c == '/') {
            slashcounter++;
        }

        if (slashcounter == 0) {
            replace[i++] = c;
        }

        else if (slashcounter == 1) {
            toBeReplaced[i++] = c;
        }

        else if (slashcounter == 2) {
            input[i++] = c;
        }


    }
    //debug purpose
    puts("The arrays have the following content\n");
    puts("replace[]:\n");
    puts(replace);
    puts("\n");
    puts("toBeReplaced[]:\n");
    puts(toBeReplaced);
    puts("\n");
    puts("input[]:\n");
    puts(input);
    printf("Slashcounter = %d\n",slashcounter);



    return 0;

Unfortunately, what happens is: that the first word i.e. the word before the first slash is stored correctly but the other two are empty.

What have I done wrong here

the current output with the input this/test/fails

Enter a line of text: 
this/test/fails
The arrays have the following content

replace[]:

this


toBeReplaced[]:




input[]:


Slashcounter = 2
Program ended with exit code: 0

p.s. I would also like to ensure that the /s are not in the output array.

Thank you for your help.

xxxvodnikxxx
  • 1,270
  • 2
  • 18
  • 37
Koos
  • 117
  • 4
  • 15
  • 1
    You might be interested in the [`strtok`](https://en.cppreference.com/w/c/string/byte/strtok) function. – Some programmer dude Apr 15 '19 at 10:18
  • As for your current program, I recommend that you [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Pay close attention to the variable `i` and its values. And don't forget that `char` strings are really called ***null-terminated** byte strings*. – Some programmer dude Apr 15 '19 at 10:20
  • I managed it with strtok but this is found in `string.h` and I am only allowed to use stdio.h and the functions getchar(), puts() and putchar() – Koos Apr 15 '19 at 10:21
  • [strtok](https://stackoverflow.com/questions/2091815/how-to-split-a-string-into-tokens-in-c) should be user, but its from `string.h`, do handle that manually, you will need `to read input string by char` and `found each position of delimiter` and then `split to tokens` (from 0 index to first occur, from 'firstOccur+1' index to second, etc.. – xxxvodnikxxx Apr 15 '19 at 10:26
  • 1
    `char c;` needs to be `int`. `getchar()` unintuitively returns an `int`, not a `char`. – Lundin Apr 15 '19 at 10:31

1 Answers1

2

You have two immediate problems in your code, first you miss to add a null character to end each sub string, second you never reset the index to 0 when you read a /

Other problems are you do not check if you will write out of the arrays, and you do not not manages the EOF

You also test the value of slashcounter all the time, this is quite expensive for nothing, you can have 3 loops or use a pointer to point to the array to fill etc

There is also no reason to use global variables, all of them can be local in main

Example with minimal changes :

#include <stdio.h>

int main(){
  int c;
  char replace[80], toBeReplaced[80], input[80];
  int i = 0;
  int slashcounter = 0;

  puts("Enter a line of text: ");

  while (( c = getchar()) != '\n') {
    if (c == EOF) {
      fprintf(stderr, "unexpected EOF");
      return -1;
    }

    if (c == '/') {
      if (slashcounter == 0) {
        replace[i] = 0;
      }
      else if (slashcounter == 1) {
        toBeReplaced[i] = 0;
      }
      else if (slashcounter == 2) {
        input[i] = c;
      }
      i = 0;
      slashcounter++;
    }
    else if (slashcounter == 0) {
      if (i != (sizeof(replace) - 2))
        replace[i++] = c;
    }
    else if (slashcounter == 1) {
      if (i != (sizeof(toBeReplaced) - 2))
        toBeReplaced[i++] = c;
    }
    else if (slashcounter == 2) {
      if (i != (sizeof(input) - 2))
        input[i++] = c;
    }
  }

  if (slashcounter == 0) {
    replace[i] = 0;
    toBeReplaced[0] = 0;
    input[0] = 0;
  }
  else if (slashcounter == 1) {
    toBeReplaced[i] = 0;
    input[0] = 0;
  }
  else if (slashcounter == 2) {
    input[i] = 0;
  }

  //debug purpose
  puts("The arrays have the following content\n");
  puts("replace[]:\n");
  puts(replace);
  puts("\n");
  puts("toBeReplaced[]:\n");
  puts(toBeReplaced);
  puts("\n");
  puts("input[]:\n");
  puts(input);
  printf("Slashcounter = %d\n",slashcounter);

  return 0;
}

Note I use an int for c to handle EOF and I removed the useless array testInput

Compilation and execution :

pi@raspberrypi:/tmp $ gcc -pedantic -Wall -Wextra s.c
pi@raspberrypi:/tmp $ ./a.out
Enter a line of text: 
this/test/fails
The arrays have the following content

replace[]:

this


toBeReplaced[]:

test


input[]:

fails
Slashcounter = 2
bruno
  • 32,421
  • 7
  • 25
  • 37