hi, the programm is supposed to seperate a *text string into string_arrays after this four characters '.' ',' ' ' '\n'
it does it wenn only one of this sign is between the words like "blablic,blablsub,blub,mi ma,maeh".
and this is the code for only this situation.
Code:
#include <stdio.h>
#include <stdlib.h>
#define ARR_MAX 8
int countLetters(char* string)
{
int counter;
int letters = 0 ;
for (counter = 0; string[counter] != '\0'; counter++)
{
letters++;
}
return letters;
}
int main()
{
char *text = "blablic,blablsub,blub,mi ma,maeh";
int array_position = 8;
int array[array_position];
int length = countLetters(text);
int text_counter;
int array_counter;
int position;
char string_array[ARR_MAX][50] = { { 0 } };
int counter;
int i;
for ( position = 0, counter = 0, i = 0, array_counter = 1; counter < 50 && position < ARR_MAX; counter++, i++)
{
if ((text[i] == ',') || (text[i] == '.') || (text[i] == '\n') || (text[i] == ' '))
{
string_array[position][counter] = 0;
counter = -1;
position++;
}
else
{
string_array[position][counter] = text[i];
}
}
for (i = 0; i < ARR_MAX; i++)
{
if (string_array[i][0] == 0) break;
printf("%s\n", string_array[i]);
}
return 0;
}
and this works, but only for one only seperating sign.
what if when ive got a *text which is (char *text = "blablic,. blablsub, blub,mi ma..,maeh"
because of this reason ive decided to build an int array[] into the code which counts the position for the separator signs.
like in the last text example
Code:
array[0] = 8;
array[1] = 9;
array[2] = 10;
array[3] = 19;
array[4] = 20;
....
...
because of that ive written something special for it..
Code:
#include <stdio.h>
#include <stdlib.h>
#define ARR_MAX 8
int countLetters(char* string)
{
int counter;
int letters = 0 ;
for (counter = 0; string[counter] != '\0'; counter++)
{
letters++;
}
return letters;
}
int main()
{
char *text = "blablic,,.blablsub,blub,mi ma,maeh";
int array_position = 8;
int array[array_position];
int length = countLetters(text);
int text_counter;
for(text_counter = 0, array_position = 0; text_counter < length; text_counter++)
{
if(text[text_counter] == ' ' || text[text_counter] == '\n' || text[text_counter] == '.' || text[text_counter] == ',')
{
array[array_position++] = text_counter + 1; //here it gets the position for separator character for the actual array[]
}
else
array[array_position] = 0;
}
int array_counter;
int position;
char string_array[ARR_MAX][50] = { { 0 } };
int counter;
int i;
for ( position = 0, counter = 0, i = 0, array_counter = 1; counter < 50 && position < ARR_MAX; counter++, i++)
{
if ((text[i] == ',') || (text[i] == '.') || (text[i] == '\n') || (text[i] == ' '))
{
if((array[array_counter] == (i+1)) && ((array[array_counter - 1]) == (array[array_counter] - 1))) //if a separator sign follows another
{
array_counter++;
continue; //breakup here and continue to count at the for loop
}
else
{
string_array[position][counter] = 0; // this is for that if a new separator sign comes and last character of string array ist set to 0.
counter = -1;
position++; //this is for the next string_array
}
}
else
{
string_array[position][counter] = text[i]; //otherwise continue to copy from text into the actual string_array
}
}
for (i = 0; i < ARR_MAX; i++)
{
if (string_array[i][0] == 0) break;
printf("%s\n", string_array[i]); //why does it print only the first one?
}
return 0;
}