-4

The program I'm making is supposed to print strings out that only end in 'ed'. This is what I have so far, but I don't understand why it doesn't work. Am I misunderstanding the logic behind the for loop with the if statement or am i just going about this wrong completely?

#include <stdio.h>
#include <string.h>

int main () 
{ 
    char string[10][100]; 
    int i; 

    for ( i =0; i<=10; i++)
    { 
       printf(" Please enter a string for the array \n"); 
       fgets( string[i], 100, stdin); 
    } 

    int length = strlen(string[i]); 

    for ( i=0; i<=10; i++)
    { 
       if( string[i][length] == 'd' && string[i][length -1] == 'e' )
       { 
          printf(" Index %d: %s \n", i,  string[i]); 
       }  
    } 
    return 0; 
}

thanks for all the input guys! However, the program is still not outputting strings that end in'ed' when i enter them through the keyboard, it compiles just fine. Here's what I have modified:

#include <stdio.h>
#include <string.h>

int main () 
{ 
  char string[10][100]; 
  int i;
  int length;

  for ( i =0; i < 10; i++)
  { 
     printf(" Please enter a string for the array \n"); 
     fgets( string[i], 100, stdin); 
  } 

  for ( i=0; i< 10; i++)
  { 
     length = strlen(string[i]); 

     if( length > 1 && (string[i][length - 1] == 'd' && string[i][length -2] == 'e') )
     { 
        printf(" Index %d: %s \n", i,  string[i]); 
     }  
  } 

  return 0;     
}
5
  • 1
    Ask yourself this question: "What is the value of length for each iteration of the loop?"
    – Gabe
    Jul 6, 2014 at 22:58
  • 1
    This is a poor title and description. Instead of "doesn't work", explain what is actually happening, and what you expected to happen instead. Make the title reflect this.
    – M.M
    Jul 6, 2014 at 23:20
  • You need to read stackoverflow.com/help/mcve and then read ericlippert.com/2014/03/05/how-to-debug-small-programs. It will help you more than us just giving you the solution. Jul 6, 2014 at 23:38
  • As for your edit, carefully read: linux.die.net/man/3/fgets Jul 6, 2014 at 23:41
  • Just figured it out. I was misunderstanding the index of each string. Thanks for the help!
    – Armbrust
    Jul 6, 2014 at 23:48

1 Answer 1

3

Your indices are not correct. In C, indices start with 0. The first loop should be:

for ( i = 0; i < 10; i++ )

The indices in second loop are also not correct. They should be:

int length = strlen ( string[i] );
if( length > 1 && ( string[i][length-1] == 'd' && string[i][length -2] == 'e' ) )

In the above statement, since the indices start at 0, the last element will be at length-1. If your string is smiled, the character at index 0 is s and the last character is at index 5 (d) while the length of string is 6.

9
  • 2
    And it should also be checked that length >= 2 to avoid pointing out of the array.
    – DrV
    Jul 6, 2014 at 23:00
  • and please do not declare a variable in loops, you should declare the length variable outside the loop (even if the latest compilers allow it, it's still a bad practice)
    – Lectem
    Jul 6, 2014 at 23:04
  • Though your answer is correct I find it difficult to read (too concise). It will be perfect if you elaborate a little more. Do keep in mind that he seems to be new to C. Jul 6, 2014 at 23:11
  • I tried modifying it like you said, however i'm still unable to output strings that end in 'ed'. The program compiles just fine but does not output anything.
    – Armbrust
    Jul 6, 2014 at 23:32
  • That is because fgets will add the newline character at the end of the string. So, you either have to remove that, or look for length-2 and length-3.
    – unxnut
    Jul 6, 2014 at 23:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.