vincegeratorix

cantidad de palabras y buscar una palabra en archivo

Dec 30th, 2011
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. /*
  2.  * lee palabras.c
  3.  *
  4.  * Copyright 2011 Erick Merino Menares <[email protected]>
  5.  *
  6.  * This program is free software; you can redistribute it and/or modify
  7.  * it under the terms of the GNU General Public License as published by
  8.  * the Free Software Foundation; either version 2 of the License, or
  9.  * (at your option) any later version.
  10.  *
  11.  * This program is distributed in the hope that it will be useful,
  12.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.  * GNU General Public License for more details.
  15.  *
  16.  * You should have received a copy of the GNU General Public License
  17.  * along with this program; if not, write to the Free Software
  18.  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19.  * MA 02110-1301, USA.
  20.  *
  21.  *
  22.  */
  23.  
  24.  
  25. #include <stdio.h>
  26.  
  27. int main()
  28. {
  29.     int i, cont;
  30.     int j=0;
  31.     int ch;
  32.     char Palabra[100][50];
  33.     char nombre[100];
  34.     char buscar[100];
  35.     int rebusq[100];
  36.     int cont2=0;
  37.     int drebusq[100];
  38.     FILE *f1;
  39.     printf("Ingrese el nombre del archivo: ");
  40.     scanf("%s", nombre);
  41.     f1=fopen(nombre, "r");
  42.     if(f1==NULL)
  43.     {
  44.         printf("Error, no existe el archivo");
  45.         exit(0);
  46.     }
  47.     else
  48.     {
  49.         cont=0;
  50.         while((ch=getc(f1))!=EOF)
  51.         {
  52.             i=ch;
  53.             if(((i>=65) && (i<=90)) || ((i>=97) && (i<=122))) //65 al 90 mayusculas y 97 al 122 minusculas no acepta acentos (no los lee).
  54.             {
  55.                 Palabra[cont][j]=(char)(ch);
  56.                 j++;
  57.             }
  58.             else
  59.             {
  60.                 if(ch==((int)(' ')))
  61.                 {
  62.                     cont++;
  63.                     j=0;
  64.                 }
  65.             }
  66.         }
  67.     }
  68.     printf("La cantidad de palabras son: %d\n", cont+1);
  69.     for(i=0;i<=cont;i++)
  70.     {
  71.         for(j=0;j<50;j++)
  72.         {
  73.             printf("%c", Palabra[i][j]);
  74.         }
  75.         printf(" ");
  76.     }
  77.     printf("\nIngrese la palabra a buscar: ");
  78.     scanf("%s", buscar);
  79.     for(i=0;i<=cont;i++)
  80.     {
  81.         for(j=0;j<50;j++)
  82.         {
  83.             if(buscar[j]!=Palabra[i][j])
  84.             {
  85.                 rebusq[i]=-1;
  86.             }
  87.         }
  88.     }
  89.     for(i=0;i<=cont;i++)
  90.     {
  91.         if(rebusq[i]!=-1)
  92.         {
  93.             drebusq[cont2]=i;
  94.             cont2++;
  95.         }
  96.     }
  97.     if(cont2==(cont+1))
  98.     {
  99.         printf("La palabra no está\n");
  100.     }
  101.     else
  102.     {
  103.         printf("La palabra está en las posiciones:");
  104.         for(i=0;i<cont2;i++)
  105.         {
  106.             printf(" %d", drebusq[i]+1);
  107.         }
  108.         printf("\n");
  109.     }
  110.     fclose(f1);
  111.     return 0;
  112. }
Advertisement
Add Comment
Please, Sign In to add comment