#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0

    Find and display all 4 letter words C prgram


    My project is nearly finish but,
    Im stuck whit finding and display all 4 letter words , i have no idea how to write the code . Can someone help? Please!
    This is my project so far whitout the finding and display all 4 letter words function.
    Code:
    #include "stdafx.h"
    #include <stdlib.h>
    #include <string.h>
    
    #define TEXT "text.txt"
    #define	TEXT1 "text1.txt"
    
    int menu();
    int entertext();
    int nword();
    int lword();
    void copytext();
    int display();
    
    int _tmain(int argc, _TCHAR* argv[])
    {	
    	int i;
    	FILE *file_ptr;
    	while(1) 
    	{
    		i=menu();
    		  switch (i)
    		  {
    			case 1:entertext();
    			break;
    			case 2:nword();lword();
    			break;   
    			case 3:copytext();
    			break; 
    			case 4:display();
    			break;  
    			case 5:exit(0);
    			default: printf("\nNevaliden izbor\n\n");break;
    		  } 
    	}
      	return 0;
    }
    
    
    int menu()
    {
    	int i,choice;
    
    	  printf("**************************************************");
    	  printf("\n*                      MENU                      *");
    	  printf("\n*                                                *");
    	  printf("\n* 1.Input text                               *");
    	  printf("\n* 2.find number of word , longest word and all 4 letter word           *");
    	  printf("\n* 3. Copy content from text.txt to text 1.txt (every word on a new line)          *");
    	  printf("\n*                              *");
    	  printf("\n* 4.Display content of text1.txtt       *");
    	  printf("\n* 5.EXIT                *");
    	  printf("\n*                                                *");
    	  printf("\n**************************************************");
    	  printf("\n\Choice:");
    	  scanf ("%d", &choice);
    	  printf("\n");
    	return choice;
    }
    int entertext(){
    	int c;
    	FILE *file_ptr;
    	file_ptr = fopen(TEXT,"wb");
    
    	printf("Vavedete teksta(do vavejdaneto na CTRL/Z).\nDumite otdeliate sas interval ili '.':\n");
    			while ((c = getchar()) != 0x1a)
    				{ 
    					putc(c, file_ptr);			 
    				}								
    		
    	fclose(file_ptr);
    
    	return  printf("\nTeksta e zapazen vav fail text.txt\n\n");
    }
    int nword()
    {  
    	int c,nw,state;
    	FILE *file_ptr;
        file_ptr=fopen(TEXT,"r");  
    	
            
    		state =0;
    		 nw =  0;
    		while ((c = fgetc(file_ptr))!= EOF) {
    
    			if (c == ' ' || c == '.' )
    			state = 0;
    			else if (state ==0) {
    			state = 1;
    			++nw;
    			}  
    		}
    		
         fclose(file_ptr);  
    
    	return printf("Broia dumi e %d.\n\n",nw);
    }
    int lword()
    {	char a[500],b[200],c[200];
    	int i,j=0,l=0;
    
    	FILE *file_ptr;
        file_ptr=fopen(TEXT,"r");
    
    	while(fgets(a,500,file_ptr)!=NULL){
    		for(i=0;i<=strlen(a);i++)
    		{
    			if(a[i]!=' ' && a[i]!='.' && a[i]!='\0')
    				b[j++]=a[i];
    			else{
    				b[j]='\0';
    			
    				if(strlen(b)>l)
    				 {
    					strcpy(c,b);
    					l=strlen(b);
    				 }
    				 j=0;
    			}
    		}
    	}
    
    	fclose(file_ptr);
    
    	return printf("Nai-dalgata duma e %s.\n\n",c) ;
    }
    void copytext()
    {	int c=0;
    	FILE *file_ptr;
    	FILE *file_ptr1;
    	file_ptr=fopen(TEXT,"r");
    	file_ptr1=fopen(TEXT1,"w");
    	       
    	while ((c = fgetc(file_ptr))!= EOF) {
    
    		if(c==' ' || c=='.' )
    			fputc('\n',file_ptr1);
    		else
    			 fputc(c,file_ptr1); 
        }
    		
         fclose(file_ptr);    
    
         fclose(file_ptr1);
    
    	 printf ("Sadarjanieto na text.txt e zapisano v text1.txt.\n");
    }
    int display()
    { 
    	int c;
    	FILE *file_ptr1;
    	file_ptr1 = fopen( TEXT1 , "r");
    
    	printf("Sadarjanieto na text1.txt e:\n");
    
        while((c=getc(file_ptr1))!=EOF)
    		printf("%c",c);
        fclose(file_ptr1);
    	
    	return printf("\n\n");
    }"
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    It seems that all you need is
    Code:
    if ( strlen(word) == 4 ) {
        printf("A 4 letter word is %s\n", word );
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0
    Originally Posted by salem
    It seems that all you need is
    Code:
    if ( strlen(word) == 4 ) {
        printf("A 4 letter word is %s\n", word );
    }
    where do i put it ?
  6. #4
  7. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > where do i put it ?
    Anywhere you like.

    Some places will be less effective than others, but all will provide you with a learning experience.

    Even a modicum of thought will quickly reveal only a few likely locations to try, all of which would take less time than waiting several hours for someone to just give you the answer.

    It makes me wonder how you managed to get this far - are you sure it's all your own code ?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0
    Im you207 in that forum see the last post.
  10. #6
  11. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    So you've got the program working then.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0
    Kind of ... for the function four_words Im getting weird output .To many letter then it should.Nothing to do with what I inputted in the file.
    Code:
    #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);
    }
  14. #8
  15. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    > while((fscanf(file_ptr,"%s",s))!=EOF){
    > printf("%s",s);
    What is s pointing to?

    You have a garbage pointer, pointing who knows where.
    So you randomly write some strings over someone else's memory, with chaotic results.

    Try something like
    char s[100];
    instead.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    10
    Rep Power
    0
    That was a very dumb mistake!Thank you very much ! Happy holidays! :)

IMN logo majestic logo threadwatch logo seochat tools logo