I can't figure out what causes this problem... appreciate any help! I've tried a lot of codes for strlen but this one was the only one that I could implement with only 1 error. With this code, I'm trying to read a string from a file, break it in words separated by space, determinate the length and then print the word and the length to the user.
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
FILE*arquivo;
char nuks[80];
char frase[80];
typedef struct node {
char palavra;
struct node* esquerda;
struct node* direita;
int altura;
} No;
size_t strlen(char *nstr)
{
int total=0;
char str[80];
strcpy(str, nstr);
total = strlen(str);
printf("|%s| is |%d|", str, total);
}
int main()
{
No* raiz = NULL;
arquivo=fopen("README.txt","r");
fgets(nuks, 79, arquivo);
printf("%s\n",nuks);
char *parte;
// Get the first word
parte = (char*)strtok(nuks, " ");
// Get the other words
while(parte != NULL){
strlen(parte);
printf("%s\n", parte);
parte = (char*)strtok(NULL, " ");
}
printf("\n\n");
system("pause");
return 0;
}
You are calling a function named strlen() into a function also named strlen(), which makes it recursive and, what is worse, infinitely recursive! Besides that, you don't need to have a local copy of nstr into str just for determining its length. Finally, is there any reason for not using the standard strlen() function declared in string.h?