Reputation: 49
I have another problem concerning C :( what I want to do with this function is check for numbers in a token and if there are none I put that token in a string, which I later print to file. My function looks like this: const char *tarpas = " ";
{
const char *space = " ";
char *token, B[255];
int i, length, found = 0, x = 0;
token = strtok(A, space);
while(token != NULL){
for(i = 0; i < strlen(token); i++){
if((token[i] >= '0') && (token[i] <= '9')) found++; //If found a number
} //increase found
if(found == 0){ //if found = 0 means the word is clean
for(i = 0; i < strlen(token); i++){
B[x] = token[i];
x++;
}
B[x] = ' '; //adds a space after the token is in string
x++;
}
rado = 0;
token = strtok(NULL, tarpas); // get another token
}
print(B);
memset(B, 0, strlen(B)); //clear B string
}
My data file:
ta5iip r345ytas suraitytas o rytoj gimimo rytas
asdasdasd
asdasd
My result file:
asdasd \
rytoj gimimo rytas
(
What it should be:
suraitytas o rytoj gimimo rytas
asdasdasd
asdasd
Thank you for any kind of input!!!
Upvotes: 1
Views: 59
Reputation: 310980
You have to reset found
in each iteration of the while loop.
Also you have to exit loop
for(i = 0; i < strlen(token); i++){
if((token[i] >= '0') && (token[i] <= '9')) found++; //If found a number
}
if a digit was found in the string.
This loop could be rewritten the following way
size_t n = strlen(token);
i = 0;
while ( i < n && ( token[i] < '0' || token[i] > '9' ) ++i;
found = i != n;
It seems also that you read strings with function fgets
that includes the new line character. You should remove this character from the string.
ANd place statement
memset(B, 0, strlen(B));
before the while loop or initially initialize array B to zeroes
Upvotes: 1
Reputation: 37
You forgot to initialize found variable inside the while loop. Also as @BLUEPIXY mentioned, B array needs to be ended with '\0'. So the code would be as following
{
const char *space = " ";
char *token, B[255];
int i, length, found = 0, x = 0;
token = strtok(A, space);
while(token != NULL){
found = 0;
for(i = 0; i < strlen(token); i++){
if((token[i] >= '0') && (token[i] <= '9')) found++; //If found a number
} //increase found
if(found == 0){ //if found = 0 means the word is clean
for(i = 0; i < strlen(token); i++){
B[x] = token[i];
x++;
}
B[x] = ' '; //adds a space after the token is in string
x++;
}
rado = 0;
token = strtok(NULL, tarpas); // get another token
}
B[x] = '\0';
print(B);
memset(B, 0, strlen(B)); //clear B string
}
Upvotes: 1
Reputation: 66371
You never reset found
to zero inside the loop.
Since you find a digit in the first token, that means that you never execute the if(found == 0)
code.
This, in turn, means that when you print B
, it's still uninitialised and you're printing some random data.
You should initialise B
:
char B[255] = {0};
and add
found = 0;
as the first line of the loop.
Or, since you have no use of found
outside the loop, move it inside the loop.
while(token != NULL){
int found = 0;
/* ... */
Upvotes: 1