string processing: my program won't work on sentences that begin with a single char

This is a discussion on string processing: my program won't work on sentences that begin with a single char within the C Programming forums, part of the General Programming Boards category; Here is my problem. I run the program it analyzes sentences and outputs a percentage on how much a kind ...

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    229

    string processing: my program won't work on sentences that begin with a single char

    Here is my problem. I run the program it analyzes sentences and outputs a percentage on how much a kind of word was used.
    It has a problem analyzing single letter words like 'A' or 'I' if they are the first word in a sentence.
    The program will ignore those lines.

    Here is my code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define LINES 4096
      
    char red;
    char* reverse;
     
    /* The function to split the input into a sentence per line */
    int split_by_sentence(void)
    {
    	int character, file_character=0;
    	char buffer[1024];
    	FILE *book=fopen("readtext1.txt", "r");
    	FILE *book2=fopen("readtext.txt", "a+");
    	if(!book) {printf("Error: unable to open input file!\n"); return 1;}
    	if(!book2) {printf("Error: unable to open output file!\n"); return 1;}
    
    	while(file_character!=EOF)
    	{
    		buffer[0]='\0';
             for(character=0;character<sizeof(buffer);character++) 
    	              {
    					  file_character=fgetc(book);
    				      if(file_character==EOF)
    						  break;
    
    				      if(file_character=='.')
    					  {
    							  buffer[character]='\0';  
    							  break;
    					  }
    
    					  if(file_character=='?')
    					  {
    							  buffer[character]='\0';  
    							  break;
    					  }
    
    					  if(file_character=='!')
    					  {
    							  buffer[character]='\0';  
    							  break;
    					  }
    
    				      buffer[character]=file_character;
                      }
    		 if(file_character==EOF)
    			 break;
             fprintf(book2, "%s.\n", buffer);
    	}
       fclose(book);
       fclose(book2);
       putchar('\n');
    
       return 0;
    }
    
    /* The function to count the number of characters in a string */
      
    int countchar (char list[])
    {
        int i, count = 0;
        for (i = 0; list[i] != '\0'; i++)
            count++;
        return (count);
    }
     
    /* The function to find the first letter in the word */
    char find_letter (char* a, char* b)
    {
        char string = strlen(b);
        strncpy (a,b,1);
        a[1]='\0';
        return 0;
    }
    
    /* The function to reverse the characters in a string */
    char* rev(char* str)
    {
      int end= strlen(str)-1;
      int start = 0;
       
      while( start<end )
      {
        str[start] ^= str[end];
        str[end] ^=   str[start];
        str[start]^= str[end];
       
        ++start;
        --end;
      }
       
      return str;
    }
    
    /* The function for percentage calculation */
    void percentage_calculation_numbers(char* a_pch, int a_numchar, char* a_b)
    {
        FILE *sp;
        FILE *fp;
        char *filedata = malloc(300);
        char str2[7];
        char vowels[] = "aeiouyAEIOUY";
        char letters[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
        char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        float one;
        float two;
        float three;
        float four;
        float total;
        float five;
        float six;
        float seven;
        float eight;
        float total_2;
        float percentage_1;
        float percentage_2;
        float percentage_3;
        float percentage_4;
         
        one = 0;
        two = 0;
        three = 0;
        four = 0;
        total = 0;
        five = 0;
        six = 0;
        seven = 0;
        eight = 0;
        total_2 = 0;
        percentage_1 = 0;
        percentage_2 = 0;
        percentage_3 = 0;
        percentage_4 = 0;
         
        /* open text file or report error */
       sp = fopen("readlist.txt", "r");
       fp = fopen("writelist.txt", "a+");
      
        if(!sp)
        {
        perror("Error: file readlist.txt was not found or opened");
        exit(1);
        }
        if((fp = fopen("writelist.txt", "a+"))==NULL) {
        printf("Cannot open file.\n");
        exit(1);
        }
        printf("%s", a_pch);
        /* The while loop gives value to five, six, seven, eight, which is used for the percentage calculation */
      while(a_pch != NULL)
        {
         /* text file comparison begin */
            while(fgets(filedata, 300, sp))
            {
                if(_memicmp(a_pch, filedata, strlen(a_pch)) == 0
                    && (filedata[strlen(a_pch)] == ' '
                    || filedata[strlen(a_pch)] == '\n'))
                {
                    /* adding numchar to filedata erases the word and leaves the grammar intact */
                    printf("%s\n", filedata);
                    /* write the text file comparison result to file */
                    fprintf(fp, "%s\n", filedata);
                }
            }
            /* text file comparison end */
              
            /* identify the first and last letter in the word begin */
              
            red = find_letter(str2, a_pch);
            if(strpbrk(str2, letters))
            {
            one++;
            }
            if(strpbrk(str2, vowels))
            {
            two++;
            }
      
            reverse = rev(a_pch);
      
            red = find_letter(str2, a_pch);
            if(strpbrk(str2, letters))
            {
            three++;
            }
            if(strpbrk(str2, vowels))
            {
            four++;
            }
            /* identify the first and last letter in the word end */
            /* The math to see what kind of word it is start */
              
            if(total = (one && three))
            {
            five++;
            }
            else if(total = (one && four))
            {
            six++;
            }
            else if(total = (two && three))
            {
            seven++;
            }
            else if(total = (two && four))
            {
            eight++;
            }
            /* The math to see what kind of word it is end */
      
            /* Reset the pointers */
            one = 0;
            two = 0;
            three = 0;
            four = 0;
            total = 0;
            rewind(sp);
            a_pch = strtok (NULL, " .");
      }
     
      /* pch is NULL, so I find alphabet in b and pass that into pch */
      a_pch = strpbrk(a_b, alphabet);
      /* pch is backwards, so I use rev to reverse it back */
      /*printf("%s uncomment to test to see if the string is backwards", pch);*/
      reverse = rev(a_pch);
      /* count the pointers */
      total_2 = (five + six + seven + eight);
      percentage_1 = ((five)/ total_2);
      percentage_2 = ((six)/ total_2);
      percentage_3 = ((seven)/ total_2);
      percentage_4 = ((eight)/ total_2);
      printf("\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n\n", percentage_1, percentage_2, percentage_3, percentage_4);
      /* write the pointers result to file */
      fprintf(fp, "\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n\n", percentage_1, percentage_2, percentage_3, percentage_4);
      fclose(fp);
      fclose(sp);
    }
     
    /* function so I don't have to type getch all over the place */
    void MyExit(void) { system("pause"); }
       
    /* the main program */
    int numchar;
     
    int main ()
    {   
         /* declaring and initiaizing variables */
         FILE *book;
         char * pch = malloc(300);
    	 char alphabet[] = "abcdefghijklmnopqrstuvwxyz";
         char buffer[LINES];
         atexit(MyExit);    
    
    	 /* create text file */
          split_by_sentence();
    
         /* open text file or report error */
         book = fopen("readtext.txt", "r");
          
         if(!book)   
         {   
              perror("Error: file readtext.txt was not found or opened");   
              return 0;   
         }
          
         /* read from file */
                  while(fgets(buffer, sizeof(buffer), book)!=NULL)
                  { 
                                      /* I tokenize the input string into individual words */
    				  pch = strtok (buffer, " ~!@#$%^&*()_+`1234567890-=[]\'\"/;/|,./{}:<>?");
                                      
    									  if(strpbrk(pch, alphabet))
    									  {
                                      /* numchar counts the numbers of characters in the pch string */
                                      numchar = countchar (pch);
                                       
                                      /* I calculate the percentage */
                                      percentage_calculation_numbers(pch, numchar, buffer);
    									  }
    									  else
    									  {
    										  continue;
    									  }
                  }
                   
                  fclose(book);
         return 0;
    }
    Here is my text files:

    readtext1.txt:

    Code:
    A car.
    An newspaper.
    These text files are blank when you run the program:
    writelist.txt
    readtext.txt
    readlist.txt

    Basically the problem is in the while loop in the percentage function.
    I need to make code there so the single letter words don't get ignored.
    I'm hoping you can look at my percentage function and maybe compile my program and make a recommendation or solve my problem and post the answer.

    I worked on the problem I was having before this problem and it was because in the percentage function I listed only lower case letters in the variable declaration. I solved that problem by including upper case in the variable declaration. Then in my test I found the problem I'm describing to you now.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Have you tried adding some logic to work with 1 letter words:

    Code:
    if(strlen(word) == 1) {
       //your code to handle it here
    }

    Seems like it should go before any other line of code that handles the longer strings. Get it handled first, and out of the way.

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    229
    That code you mentioned isn't necessary to get the program working.
    The problem was the alphabet variable in the main function was all lower case.
    This code below works.

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define LINES 4096
      
    char red;
    char* reverse;
     
    /* The function to split the input into a sentence per line */
    int split_by_sentence(void)
    {
    	int character, file_character=0;
    	char buffer[1024];
    	FILE *book=fopen("readtext1.txt", "r");
    	FILE *book2=fopen("readtext.txt", "a+");
    	if(!book) {printf("Error: unable to open input file!\n"); return 1;}
    	if(!book2) {printf("Error: unable to open output file!\n"); return 1;}
    
    	while(file_character!=EOF)
    	{
    		buffer[0]='\0';
             for(character=0;character<sizeof(buffer);character++) 
    	              {
    					  file_character=fgetc(book);
    				      if(file_character==EOF)
    						  break;
    
    				      if(file_character=='.')
    					  {
    							  buffer[character]='\0';  
    							  break;
    					  }
    
    					  if(file_character=='?')
    					  {
    							  buffer[character]='\0';  
    							  break;
    					  }
    
    					  if(file_character=='!')
    					  {
    							  buffer[character]='\0';  
    							  break;
    					  }
    
    				      buffer[character]=file_character;
                      }
    		 if(file_character==EOF)
    			 break;
             fprintf(book2, "%s.\n", buffer);
    	}
       fclose(book);
       fclose(book2);
       putchar('\n');
    
       return 0;
    }
     
    /* The function to find the first letter in the word */
    char find_letter (char* a, char* b)
    {
        char string = strlen(b);
        strncpy (a,b,1);
        a[1]='\0';
        return 0;
    }
    
    /* The function to reverse the characters in a string */
    char* rev(char* str)
    {
      int end= strlen(str)-1;
      int start = 0;
       
      while( start<end )
      {
        char temp = str[ start ];
    	str[ start ] = str[ end ];
    	str[ end ] = temp;
       
        ++start;
        --end;
      }
       
      return str;
    }
    
    /* The function for percentage calculation */
    void percentage_calculation_numbers(char* a_pch, char* a_b)
    {
        FILE *sp;
        FILE *fp;
        char *filedata = malloc(300);
        char str2[7];
        char vowels[] = "aeiouyAEIOUY";
        char letters[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
        char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        float one;
        float two;
        float three;
        float four;
        float total;
        float five;
        float six;
        float seven;
        float eight;
        float total_2;
        float percentage_1;
        float percentage_2;
        float percentage_3;
        float percentage_4;
         
        one = 0;
        two = 0;
        three = 0;
        four = 0;
        total = 0;
        five = 0;
        six = 0;
        seven = 0;
        eight = 0;
        total_2 = 0;
        percentage_1 = 0;
        percentage_2 = 0;
        percentage_3 = 0;
        percentage_4 = 0;
         
        /* open text file or report error */
       sp = fopen("readlist.txt", "r");
       fp = fopen("writelist.txt", "a+");
      
        if(!sp)
        {
        perror("Error: file readlist.txt was not found or opened");
        exit(1);
        }
        if((fp = fopen("writelist.txt", "a+"))==NULL) {
        printf("Cannot open file.\n");
        exit(1);
        }
        printf("%s", a_pch);
        /* The while loop gives value to five, six, seven, eight, which is used for the percentage calculation */
      while(a_pch != NULL)
        {
         /* text file comparing word to grammar list begin */
            while(fgets(filedata, 300, sp))
            {
                if(_memicmp(a_pch, filedata, strlen(a_pch)) == 0
                    && (filedata[strlen(a_pch)] == ' '
                    || filedata[strlen(a_pch)] == '\n'))
                {
                    /* write the text file comparison result to file */
                    printf("%s\n", filedata);
                    fprintf(fp, "%s\n", filedata);
                }
            }
            /* text file comparing word to grammar list end */
              
            /* identify the first letter in the word begin */
              
            red = find_letter(str2, a_pch);
            if(strpbrk(str2, letters))
            {
            one++;
            }
            if(strpbrk(str2, vowels))
            {
            two++;
            }
            /* identify the first letter in the word end */
    		/* identify the last letter in the word begin */
            reverse = rev(a_pch);
      
            red = find_letter(str2, a_pch);
            if(strpbrk(str2, letters))
            {
            three++;
            }
            if(strpbrk(str2, vowels))
            {
            four++;
            }
            /* identify the last letter in the word end */
    
            /* The math using the first and last letter begin */
              
            if(total = (one && three))
            {
            five++;
            }
            else if(total = (one && four))
            {
            six++;
            }
            else if(total = (two && three))
            {
            seven++;
            }
            else if(total = (two && four))
            {
            eight++;
            }
            /* The math using the first and last letter end */
      
            /* Reset the pointers */
            one = 0;
            two = 0;
            three = 0;
            four = 0;
            total = 0;
            rewind(sp);
            a_pch = strtok (NULL, " .");
      }
     
      /* pch is NULL, so I find alphabet in b and pass that into pch */
      a_pch = strpbrk(a_b, alphabet);
      /* pch is backwards, so I use rev to reverse it back */
      /*printf("%s uncomment to test to see if the string is backwards", pch);*/
      reverse = rev(a_pch);
      /* count the pointers */
      total_2 = (five + six + seven + eight);
      percentage_1 = ((five)/ total_2);
      percentage_2 = ((six)/ total_2);
      percentage_3 = ((seven)/ total_2);
      percentage_4 = ((eight)/ total_2);
      printf("\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n\n", percentage_1, percentage_2, percentage_3, percentage_4);
      /* write the pointers result to file */
      fprintf(fp, "\nletter vowel letter %f\nletter vowel vowel %f\nvowel vowel letter %f\nvowel vowel vowel %f\n\n", percentage_1, percentage_2, percentage_3, percentage_4);
      fclose(fp);
      fclose(sp);
    }
     
    /* function so I don't have to type getch all over the place */
    void MyExit(void) { system("pause"); }
       
    /* the main program */
     
    int main ()
    {   
         /* declaring and initiaizing variables */
         FILE *book;
         char * pch = malloc(300);
    	 char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
         char buffer[LINES];
         atexit(MyExit);    
    
    	 /* create text file */
          split_by_sentence();
    
         /* open text file or report error */
         book = fopen("readtext.txt", "r");
          
         if(!book)   
         {   
              perror("Error: file readtext.txt was not found or opened");   
              return 0;   
         }
          
         /* read from file */
                  while(fgets(buffer, sizeof(buffer), book)!=NULL)
                  { 
                                      /* I tokenize the input string into individual words */
    				  pch = strtok (buffer, " ~!@#$%^&*()_+`1234567890-=[]\'\"/;/|,./{}:<>?");
                                      
    									  if(strpbrk(pch, alphabet))
    									  {
                                       
                                      /* I calculate the percentage */
                                      percentage_calculation_numbers(pch, buffer);
    									  }
    									  else
    									  {
    										  continue;
    									  }
                  }
                   
                  fclose(book);
         return 0;
    }
    Thanks for the reply though.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. processing a char pointer/string
    By l2u in forum C Programming
    Replies: 3
    Last Post: 05-29-2006, 03:05 PM
  2. Removing single char from string
    By cboard_member in forum C Programming
    Replies: 17
    Last Post: 09-28-2005, 04:17 AM
  3. single-string to char
    By FoodDude in forum C++ Programming
    Replies: 6
    Last Post: 09-01-2005, 04:23 PM
  4. String to a single char
    By Thumper333 in forum C Programming
    Replies: 15
    Last Post: 11-15-2004, 10:54 PM
  5. Replies: 1
    Last Post: 03-02-2003, 08:42 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21