I have written a program to find the longest word and to print it.
My code is:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
int MaxWord(char text[],char[]);
int main (void){
char text[1000];
char word[1000];
int max;
printf("geben Sie den Text bitte : ");
gets(text);
max=MaxWord(text,word);
printf("ist mit %d Zeichen das laengste wort im Text\n\n\n",max);
return 0;
}
int MaxWord(char text[], char word[])
{
char i;
int ctr=0;
int max=0;
int len;
char begin=0;
len=strlen(text);
for(i=0;i<len+1;i++)
{
if(isalpha(text[i]))
{
if(ctr==0)
{
begin=i;
}
ctr++;
}
else
{
if(ctr>max)
{
max=ctr;
}
ctr=0;
}
}
strncpy(word,begin,max);
printf("%s ",word);
return max;
}
and the error is:
error #2140: Type error in argument 2 to 'strncpy'; expected 'const char * restrict' but found 'char'.
How can I fix this?