Thread: c string help

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    8

    c string help

    Ok first off i have been trying to fix this problem the past several days and have looked up and read a ton of things and still cannot get this to work so DO NOT link me to another website at this point i just need someone to show me how to fix the part of my code that is wrong. I know what is wrong but whenever i try and fix it i get more errors so the best way for me to learn is seeing how the code looks when written correctly. Please do not comment on this if you are not going to help im sick of the useless comments and this program is due at midnight tonight so im already stressed and need this done. I never understood people when they said showing you the correct code wont help you learn because for me that is the best way to learn because i can look at the correct code and study it for future reference.Now that im done with that rant here is my code :

    Code:
    
    #include <stdio.h>
    #include <string.h>
    
    
    
    int main() {
        
        char str[80+1];
        int sentences, sWords, MWords, LWords, length, common;
        sentences=sWords=MWords=LWords=length=common=0;
        int capital=0;
        char test[80+1];
        
        printf("\nCSE 1030 Program Three - email - csp_03\n\n");
        
        while(scanf("%s",str) != EOF) {
        
            while(ispunct(strlen(str)-1))
            {
                if(str[strlen(str)-1] == '?' || str[strlen(str)-1] == '.' || str[strlen(str)-1] == '!')
                {
                    sentences++;
                }
                str[strlen(str)-1] = '\0';
    
    
                while(ispunct(str[0]))
                {
                    strcat(str,str+1);
                }
                length = strlen(str) +1;
    
    
                if(length < 6)
                {
                    if ( length < 4 )
                    {
                        if(str == "the" || str == "The" || str == "a" || str == "A" || str == "of" || str == "Of" || str == "is" || str == "Is" || str== "that" || str=="That" || str == "are" || str=="Are")
                        {
                            common++;
                            sWords++;
                        }
                        else sWords++;
                    }
                }
                if(length >=6 && length < 12 ) MWords++;
    
    
                if(length >= 12) LWords++;
                
                strcpy(test,str);
    
    
                test[0] = toupper(test[0]);
                
                if(strcmp(test,str)==0) capital++;
            }
            
        
        
        
        
        
        
        
        
        }
    
    
        printf("Summary of Results: \n\n");
        printf("%d short length words\n",sWords);
        printf("%d medium length words\n",MWords);
        printf("%d long length words\n",LWords);
        printf("%d capitalized words\n",capital);
        printf("%d common words\n",common);
        printf("%d sentences\n",sentences);
        
        
    
    
        return 0;
    
    }
    Ok so what this prgram does is it takes a text file and says how many short,medium,long, etc.. words the paragraph has. So i know that i mseed up when i used the == sign for the string and believe i am supposed to use strcmp but whenever i try using strcmp i get a bunch of errors and get even more confused. Please show me the correct way to code that portion. Also, it is not printing out any of the printf statements at the end for some reason.
    Last edited by hallo; 11-10-2011 at 06:18 PM.

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    Well, here is what your program does:

    1. It never opens a file
    2. It waits for input from the user
    3. It reads 1 word at a time
    4. It discards any word that doesn't end with a non-alphanumeric graphic character.

    Look at your code blocking.

    If you want to gobble up 80 characters use is fgets() and not scanf. Which is probably not what you want to do at this point.

    The whole strcpy, toupper, strcmp is tedious when all you need is isupper().

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Here's code to read from a text file a line at a time:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #define LINES 4096
    
    /* function so I don't have to type getch all over the place */
    void MyExit(void) { system("pause"); }
      
    int main ()
    {    
         /* declaring and initiaizing variables */
         FILE *book;
         char * pch = malloc(300);
         char buffer[LINES];
         atexit(MyExit);     
         
         /* 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) 
                  {  
                                      printf("%s\n", buffer);
                  } 
                  
                  fclose(book);
         return 0;
         }
    Here is the text file, both files are in the same folder:

    Code:
    car ran.
    car ran. 
    jeremy car ran ran.
    And here's the results:

    Code:
    car ran.
    
    car ran.
    
    jeremy car ran ran.
    Press any key to continue . . .
    Put your code where I put printf. Try compiling my code then fiddle with your code so i works.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    > if(str == "the" || str == "The" || str == "a" || str == "A" || str == "of" || str == "Of" ||
    > str == "is" || str == "Is" || str== "that" || str=="That" || str == "are" || str=="Are")
    You were told about this in your last thread, but it seems it didn't sink in very well.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 22
    Last Post: 07-28-2011, 01:26 PM
  2. Replies: 7
    Last Post: 06-16-2011, 06:21 PM
  3. Replies: 5
    Last Post: 05-09-2010, 10:58 AM
  4. Replies: 1
    Last Post: 10-31-2005, 11:36 AM
  5. Problem comparing string from text file with string constant
    By XenoCodex Admin in forum C++ Programming
    Replies: 3
    Last Post: 07-25-2002, 10:17 AM