finding longest word
-
This is my logic to find the longest word in C. void find_long(char *a) { int l,i,l1=0,j=0,l2=0; char b[50],c[50]; l=strlen(a); for(i=0;il1){ strcpy(c,b); l1=strlen(b); j=0; } else{ printf("\nEOF");} } } printf("\nlongest is \n"); printf(c); But it is not taking the last word I am giving. What is wrong here?
-
This is my logic to find the longest word in C. void find_long(char *a) { int l,i,l1=0,j=0,l2=0; char b[50],c[50]; l=strlen(a); for(i=0;il1){ strcpy(c,b); l1=strlen(b); j=0; } else{ printf("\nEOF");} } } printf("\nlongest is \n"); printf(c); But it is not taking the last word I am giving. What is wrong here?
You only check the length of each word when you encounter a space character. So if the last character is not a space then when the loop ends you still have an unchecked word in array b. Your code would be clearer if you formatted it properly and used <pre> tags round it. And also used meaningful names for your variables rather than single letters.
-
You only check the length of each word when you encounter a space character. So if the last character is not a space then when the loop ends you still have an unchecked word in array b. Your code would be clearer if you formatted it properly and used <pre> tags round it. And also used meaningful names for your variables rather than single letters.
what condition I can add here to avoid it?
-
This is my logic to find the longest word in C. void find_long(char *a) { int l,i,l1=0,j=0,l2=0; char b[50],c[50]; l=strlen(a); for(i=0;il1){ strcpy(c,b); l1=strlen(b); j=0; } else{ printf("\nEOF");} } } printf("\nlongest is \n"); printf(c); But it is not taking the last word I am giving. What is wrong here?
It is essential to "see" the code properly indented and formatted. Then you can recognise - besides the problems mentioned by Richard - that there is a missing closing parentheses (which should also issue a compiler error if not present behind the posted code):
void find_long(char *a)
{
int l,i,l1=0,j=0,l2=0;
char b[50],c[50];
l=strlen(a);
for (i = 0; i < l; i++) {
if (a[i] != ' ') {
b[j] = a[i];
printf("%c ",b[j]);
j++;
}
else if (a[i] == ' ') {
b[j] = '\0';
if (strlen(b) > l1) {
strcpy(c,b);
l1 = strlen(b);
// Inserted missing parentheses here
}
j = 0;
}
else {
printf("\nEOF");}
}
}
printf("\nlongest is \n");
printf(c);
// Added missing parentheses here
} -
what condition I can add here to avoid it?
-
It worked Thanks
-
This is my logic to find the longest word in C. void find_long(char *a) { int l,i,l1=0,j=0,l2=0; char b[50],c[50]; l=strlen(a); for(i=0;il1){ strcpy(c,b); l1=strlen(b); j=0; } else{ printf("\nEOF");} } } printf("\nlongest is \n"); printf(c); But it is not taking the last word I am giving. What is wrong here?
Try
#include enum State
{
INSIDE_BLANKS,
INSIDE_WORD
};void find_longest_word( const char * a, const char ** pps, const char ** ppe);
int main()
{
const char * foo = "alpha beta gamma delta epsilon ";const char *ps, *pe;
find_longest_word( foo, &ps, &pe);
if ( pe-ps > 0)
{
printf("longest word length = %ld\n", (pe-ps));
while (ps != pe)
{
printf("%c", *ps);
++ps;
}
printf("\n");
}
return 0;
}void find_longest_word( const char * a, const char ** pps, const char **ppe)
{
const char * ps = a;
const char * pe = a;
*pps = *ppe = a;enum State state = INSIDE_BLANKS;
while ( *a != '\0')
{
if ( state == INSIDE_BLANKS)
{
if ( *a != ' ')
{
state = INSIDE_WORD;
ps = a;
}
}
else // inside word
{
if ( *a == ' ')
{
pe = a;
if ( pe - ps > *ppe - *pps)
{
*pps = ps;
*ppe = pe;
}
state = INSIDE_BLANKS;
}
}
++a;
}
// special handling of (possible) last word
if ( state == INSIDE_WORD)
{
if ( pe - ps > *pps - *pps)
{
*pps = ps;
*ppe = pe;
}
}
}