I need to write a program that gets content of txt file and display all 4 letter word .
Can someone help ?

The program must be in C .

What have you done so far? If you haven't written any code, what thoughts have you come up for solving the problem? Did you test any ideas with a small test program?

If you haven't done anything, why not? Keep in mind that nobody here will do your homework for you.

    char *str="This is a program that reads a file and all four letter words";


    char *p_str = str;

    int c_len = 0;

    int len_4 = 0;


    while('\0' != *p_str) {
        if((' ' == *p_str) || ('.' == *p_str)){

            if(4 == c_len){
                len_4++;
            }

            c_len = 0;
        } else {

            c_len++;
        }

        p_str++;
    }


    if(4 == c_len){
        len_4++;
    }



    return (len_4);

I came up whit this ,but only finds the number of 4 letter word. I dont know how to print all 4 letter words on the screen.

The output shoud be
All 4 letter words are: This that file four

I came up whit this ,but only finds the number of 4 letter word. I dont know how to print all 4 letter words on the screen.

Normally I'd suggest that you set a pointer to the start of each word as you go, then when the word ends if the letter count is 4, print out the characters starting from that pointer. However, that solution doesn't really fall out of your algorithm, so some changes are needed:

#include <ctype.h>
#include <stddef.h>
#include <stdio.h>

int main()
{
    const char *s = "This is a program that reads a file and all four letter words";
    const char *p = s;

    while (*p) {
        const char *start;
        ptrdiff_t len;

        /* Skip leading whitespace */
        while (*p && isspace(*p))
            ++p;

        start = p; /* Save the start of a word */

        /* Jump over the word */
        while (*p && !isspace(*p))
            ++p;

        /* Get the distance between the two pointers (word length) */
        len = p - start;

        if (len == 4) {
            /* Print characters in the calculated distance */
            printf("%.*s\n", len, start);
        }
    }

    return 0;
}

Thanks!
Now I want to write text to a .txt file and then read the file and display all 4 letter word . But Im doing something wrong .

#include "stdafx.h"
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
int menu();
int entertext();
void four_words(char *s);


int main()
{   
   int i;
    FILE *file_ptr;
    file_ptr=fopen("text.txt","r");
    char c[500];
    fgets(c, 500, file_ptr);


    while(1) 
    {
        i=menu();
          switch (i)
          {
            case 1:entertext();
            break;
            case 2:four_words(c);
            break;   
            case 3:exit(0);
            default: printf("\nInvalid choice\n\n");
            break;
          } 
    }
    return 0;
}





int menu()
{
    int choice;

      printf("**************************************************");
      printf("\n*                      MENU                      *");
      printf("\n*                                                *");
      printf("\n*  1.Enter text                                  *");
      printf("\n*  2.Display all 4 letter words                  *");
      printf("\n*                                                *");
      printf("\n*                                                *");
      printf("\n*  3.Exit                                        *");
      printf("\n*                                                *");
      printf("\n**************************************************");
      printf("\n\nNapravete izbor:");
      scanf ("%d", &choice);
      printf("\n");
    return choice;
}
int entertext()
{
    int c;
    FILE *file_ptr;
    file_ptr = fopen("text.txt","wb");

    printf("Enter text till CTRL/Z):\n");
            while ((c = getchar()) != 0x1a)
                { 
                    putc(c, file_ptr);           
                }
             return  printf("\nText is saved in the file.\n\n");
}
void four_words(char *s)
{

    char *p = s;
    char *start;
    ptrdiff_t len;

    while (*p) {


        while (*p && isspace(*p))       
            ++p;
        start = p; 

        while (*p && !isspace(*p))      
            ++p;

        len = p - start;
        if (len == 4) {

            printf("%.*s\n", len, start);   
        }
    }
}

Sorry for so many posts.Im getting output finally .But the output is weird .To many letter then it should.Nothing to do with what I inputted in the file.

#include "stdafx.h"
#include <ctype.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
int menu();
int entertext();
void four_words();


int main()
{   int words;

   int i;

    while(1) 
    {
        i=menu();
          switch (i)
          {
            case 1:entertext();
            break;
            case 2: four_words();

            break;   
            case 3:exit(0);
            default: printf("\nInvalid choice\n\n");
            break;
          } 
    }

    return 0;
}





int menu()
{
    int choice;

      printf("**************************************************");
      printf("\n*                      MENU                      *");
      printf("\n*                                                *");
      printf("\n*  1.Enter text                                  *");
      printf("\n*  2.Display all 4 letter words                  *");
      printf("\n*                                                *");
      printf("\n*                                                *");
      printf("\n*  3.Exit                                        *");
      printf("\n*                                                *");
      printf("\n**************************************************");
      printf("\n\nNapravete izbor:");
      scanf ("%d", &choice);

    return choice;
}
int entertext()
{
    int c;
    FILE *file_ptr;
    file_ptr = fopen("text.txt","wb");

    printf("Enter text till CTRL/Z):\n");
            while ((c = getchar()) != 0x1a)
                { 
                    putc(c, file_ptr);           
                }
            fclose(file_ptr);
             return  printf("\nText is saved in the file.\n\n");
}
void four_words()
{  char c[500];
    char *s;
    s=c;
   FILE *file_ptr;
   file_ptr = fopen("text.txt","r");
   while((fscanf(file_ptr,"%s",s))!=EOF){
    printf("%s",s);
    char *p = s;
    char *start;
    ptrdiff_t len;

    while (*p) {


        while (*p && isspace(*p))       
            ++p;
        start = p; 

        while (*p && !isspace(*p))      
            ++p;

        len = p - start;
        if (len == 4) {

            printf("%.*s\n", len, start);   
        }
    }
   }
    fclose(file_ptr);
}

Why are you including the header file "stdafx.h"? I'm not familiar with this in C. What about "stddef.h". Why do you need this?

Why are you writing out your words in binary mode, into the file, but reading in file data in text mode? (line 62 and 77)

To debug this:

1) Check that your file data is being put into the file correctly. Be consistent with the file mode you use, always. Any editor can be used, but don't use your program for this part of the debugging.

2) Are you able to read in the words correctly, from the text file? Before you do anything to find the 4 letter words, you have to be sure you're getting all the words OK, from the raw text in the file.

3) If #2 is OK, then concentrate on how you're finding the 4 letter words. Checking for a space first is incorrect - %s will always start with a char, never whitespace.

Work with that and post back. You're close.

Why are you including the header file "stdafx.h"? I'm not familiar with this in C.

It's a Microsoftism. stdafx.h is a precompiled header that contains all of the libraries and stable headers so that you don't have to include other headers, but primarily to speed up the build process.

What about "stddef.h". Why do you need this?

stddef.h is where ptrdiff_t is defined. You'll see ptrdiff_t used in four_words().

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.