// Программа делит текст находящийся в файле infile1.txt

// на отдельные слова и

// помещает все слова в новый файл

// Не работает с кириллицей.

// Каждое слово выводит с новой строки

 

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <ctype.h>

 

int get_lines( char *oldname,char *newname,unsigned int kolbukv);

int GetWord(char* theString, char* word, int* wordOffset);

int find(char* goodChar,char* word);

 

char goodChar[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";

 

int main( void )

{

 unsigned int kolBukv;

//  int kolBukv;

 

 

//массивы для имен файла источника о файла получателя

 

    char source[80];

    char destination[]="outfile1.txt";

 

 /* Введем имена файла источника и файла получателя. */

 

    printf("\nEnter source file: ");

    gets(source);

 //   printf("\nEnter destination file: ");

 //   gets(destination);

 

    puts("\nEnter number of character: ");

    scanf("%u",&kolBukv);

 

 

    get_lines(source,destination,kolBukv);

 

 

   return(0);

}

 

 

 

 

int get_lines( char *oldname,char *newname,unsigned int kolbukv)

{

 

   char buffer[256];  /* Temporary storage for each line. */

   char word[256];

   char word1[256]= "\n";

 

    FILE *fold, *fnew;

 

        int *q;

      int wordOffset;              // start at the beginning

        wordOffset = 0;

        q = &wordOffset;

 

 /* Откроем файл-источник для чтения в символьном режиме */

 

    if ( ( fold = fopen( oldname, "r" ) ) == NULL )

        return -1;

 

 /* Откроем файл получатель для записи в символьном режиме. */

 

    if ( ( fnew = fopen( newname, "w" ) ) == NULL  )

    {

        fclose ( fold );

        return -1;

    }

 

 

    while (!feof( fold ))

    {

       fgets (buffer,80,fold);

 

      while (-1!=GetWord(buffer,word,q))

      {   

           if(strlen(word)==kolbukv)

           {

 

                 if(-1!= find(goodChar,word))

                 {

                  fputs( word, fnew );

                  fputs( word1, fnew );

                 }

           }     

 

          }

       

        wordOffset = 0;

        q = &wordOffset;

 

    }

 

    fclose ( fold );

    fclose ( fnew);

    return 0;

}

 

 

////////////////////////////////////////////////////////

 

// function to parse words from a string.

int GetWord(char* theString, char* word, int* wordOffset)

{

        int* u ;

      char *p1, *p2;

         int i;

         int len;

         int j;

         u =  wordOffset;

      if (!theString[*u])  // end of string?

      {

            return -1;

      }

      p1 = p2 = (theString+(*u));  // point to the next word

 

      // eat leading spaces

      for ( i = 0; i<(int)strlen(p1) && !isalnum(p1[0]); i++)

            p1++;

 

      // see if you have a word

      if (!isalnum(p1[0]))

            return -1;

 

      // p1 now points to start of next word

      // point p2 there as well

      p2 = p1;

 

      // march p2 to end of word

      while (isalnum(p2[0]))

            p2++;

 

      // p2 is now at end of word

      // p1 is at beginning of word

      // length of word is the difference

       len =  (p2 - p1);

 

      // copy the word into the buffer

      strncpy (word,p1,len);

 

      // null terminate it

      word[len]='\0';

 

      // now find the beginning of the next word

      for ( j = (p2-theString); j<(int)strlen(theString)

            && !isalnum(p2[0]); j++)

      {

            p2++;

      }

 

      (*u) = (p2-theString);

 

      return 0;

}

//end of getWord()

 

 int find(char* goodChar,char* word)

{

    unsigned int m ;

   unsigned int i,k;

 

  for( i=0;i<strlen(word);i++)

  {

     m = 0;

   for( k=0;k<strlen(goodChar);k++)

    {

      if(goodChar[k] == word[i])

     break;

       m++;

    }

     if(m == strlen(goodChar))

     break;

  }

     if(m == strlen(goodChar))

    {

      return -1;

    }

 

    else

    {

      return 0;

    }

}

//end of find()

Hosted by uCoz