Reputation: 73
CODE HERE: http://ideone.com/AZnXFm
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int main()
{
char *buffer;
size_t bufsize = 32;
size_t characters;
buffer = (char *)malloc(bufsize * sizeof(char));
if( buffer == NULL)
{
perror("Unable to allocate buffer");
exit(1);
}
printf("Type something: ");
characters = getline(&buffer,&bufsize,stdin);
printf("%zu characters were read.\n",characters);
printf("You typed: %s",buffer);
char *end_str,*token2;
char *token = strtok_r(buffer,";",&end_str);
printf("token : %s \n", token);
int count =0,wordcnt=0;
while(token !=NULL)
{
char *end_token;
count++;
printf("outside count ------------------------%d\n", count);
strtok_r(token," ",&end_token);
while(token2!=NULL)
{
wordcnt++;
printf("insdie count %d\n",wordcnt);
printf("%s------------------- \n", token2);
token2 = strtok_r(NULL," ",&end_token);
}
token = strtok_r(NULL, ";",&end_str);
}
return(0);
}
Output is
Type something: rosie is; really good
22 characters were read.
You typed: rosie is; really good
token : rosie is
outside count ------------------------1
insdie count 1
AWAVA��AUATL�% -------------------
insdie count 2
is-------------------
outside count ------------------------2
Upvotes: 0
Views: 219
Reputation: 755104
A number of basic fixes are needed or have been applied in the code below:
getline()
do the memory allocation.getline()
read a line.token2
with inner strtok_r()
call.characters
into ssize_t
to match the return from getline()
.characters
using %zd
. This is marginally controversial; it prints the signed variant of size_t
with the z
-qualified signed decimal format string. It makes sense (to me at least and my compiler — GCC 6.2.0 on macOS Sierra), but I'm not sure where to find the confirmation that it is officially sanctioned by (the POSIX) standards.Leading to:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
char *buffer = 0;
size_t bufsize = 0;
ssize_t characters;
printf("Type something: ");
characters = getline(&buffer, &bufsize, stdin);
printf("%zd characters were read.\n", characters);
if (characters > 0)
{
printf("You typed: %s", buffer);
char *end_str1;
char *token1 = strtok_r(buffer, ";", &end_str1);
printf("token: %s \n", token1);
int count = 0, wordcnt = 0;
while (token1 != NULL)
{
char *end_str2;
count++;
printf("outside count ------------------------%d\n", count);
char *token2 = strtok_r(token1, " ", &end_str2);
while (token2 != NULL)
{
wordcnt++;
printf("inside count %d\n", wordcnt);
printf("%s------------------- \n", token2);
token2 = strtok_r(NULL, " ", &end_str2);
}
token1 = strtok_r(NULL, ";", &end_str1);
}
}
free(buffer);
return(0);
}
Example run:
Type something: rosie is; really good
22 characters were read.
You typed: rosie is; really good
token: rosie is
outside count ------------------------1
inside count 1
rosie-------------------
inside count 2
is-------------------
outside count ------------------------2
inside count 3
really-------------------
inside count 4
good
-------------------
Upvotes: 1