Need help working with Strings in C

This is a discussion on Need help working with Strings in C within the C Programming forums, part of the General Programming Boards category; I have been trying to understand how to accomplish the tasks needed for this project I'm working on which goes ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    21

    Need help working with Strings in C

    I have been trying to understand how to accomplish the tasks needed for this project I'm working on which goes in and returns number of sentences, etc, but I'm pretty sure I'm either leaving out a necessary part, or not comparing the strings appropriately. If a kind person would help me understand what I'm not doing right, and how to fix it, I would greatly appreciate it.

    Here is the assignment:

    This assignment requires you to use strings, loops, conditionals, and file redirection. So, the main goal of this assignment is for you to show that you have mastered the syntax of strings, string-related functions, and the general application of loops and conditionals, and to show the techniques used to perform some common text-analysis operations.Write a C program that will read a series of lines of input text (each input text line will not exceed 80 characters in length) and determine the number of each unique "word"s found in the input. While finding the words, several statistics about the words and text are to be accumulated.
    A "word" is defined for this assignment as a series of non-white-space characters with leading and trailing punctuation characters removed. You are to consider punctuation characters to be all printing characters except letters, digits, and the space. C can automatically identify punctuation characters using theispunct() function. Note that punctuation characters inside a word (such as teacher's) are considered part of the word, and sequences of numbers (like 1234) would also be considered valid words, based on the assignment's definition of a word.
    The program should initially display on the screen the problem number, the course number, your name, your email address, and the name of the csp machine on which you ran your program.
    The program should determine each word found in a series of input lines, and accumulate the following characteristics about the words:
    • The number of short-length words (words with a length less than 6).
    • The number of medium-length words (words with a length greater than 5 but less than 12).
    • The number of long-length words (words with a length greater than 11).
    • The number of capitalized words.
    • The number of common words ("the", "of", "a", "is", "that", "are"), without worrying about case. Treat the words case-insensitively. Accumulate one total for all of the common words listed.
    • The number of sentences (a sentence is considered to be (for this assignment) to be indicated by a word that has either a period, exclamation point, or a question mark someone in its trailing punctuation).
    After all of the words have been determined, display a summary report of each of the characteristics described above.

    Here is what I have come up with so far:

    Code:
    // CSE 1030 Program Three - Alexander Hollis - email - csp_03
    
    
    
    #include <stdio.h>
    #include <string.h>
    #define PUNC ispunct
    #define LAST strlen(str)-1
    
    
    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 - Alexander Hollis - email - csp_03\n\n");
        
        while(scanf("%s",str) != EOF) {
        
            while(ispunct(LAST))
            {
                if(str[LAST] == '?' || str[LAST] == '.' || str[LAST] == '!')
                {
                    sentences++;
                }
                str[LAST] = '\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;
    
    
    }
    Here is the data.dat file that I give to unix as the input for the scanf:

    The teacher announced: "This is a simple example
    showing several sentences of text." The students
    where instantaneously amazed!


    With surprise, the teacher's dog stood up and barked
    "Ruff! Ruff!! Ruff!!!" The students were surprised
    and burst into applause, much to the teacher's chagrin.


    That is all, folks! [86 and out].

    (The spaces are intentional for the input)


    Here's what the output should look like:

    Sample output:
    %cat data.dat
    The teacher announced: "This is a simple exampleshowing several sentences of text." The studentswhere instantaneously amazed!With surprise, the teacher's dog stood up and barked"Ruff! Ruff!! Ruff!!!" The students were surprisedand burst into applause, much to the teacher's chagrin.That is all, folks! [86 and out].
    %./a.out < data.dat

    CSE 1030 Program Three - your name - your email address - csp_machine
    Summary of Results:
    32 short length words
    17 medium length words
    1 long length words
    9 capitalized words
    10 common words
    8 sentences
    Any feedback is greatly appreciated.

    - Alex

  2. #2
    Registered User
    Join Date
    Nov 2011
    Location
    Seguin, Texas, United States
    Posts
    6
    my program looks pretty much the exact same, everything is coming back as 0. is that the same problem youre getting?

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    21
    yes, it appears that the checks aren't working, or formatted right, which is what is making none of the counts increase.

  4. #4
    Registered User
    Join Date
    Nov 2011
    Location
    Seguin, Texas, United States
    Posts
    6
    did you get yours working then?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Seguin, Texas, United States
    Posts
    6
    i cant find where the error even is really, i dont see whats wrong with the checks...

  6. #6
    Registered User
    Join Date
    May 2009
    Posts
    2,771
    This line DOES NOT do what you think it does; read about strcmp.
    strcmp - C++ Reference
    Code:
    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")
    These two defines are NOT a good idea.
    Code:
    #define PUNC ispunct
    #define LAST strlen(str)-1
    Tim S.
    Last edited by stahta01; 11-06-2011 at 07:32 PM.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Seguin, Texas, United States
    Posts
    6
    i dont really understand how strcmp would help here

  8. #8
    Registered User
    Join Date
    Aug 2008
    Location
    Belgrade, Serbia
    Posts
    163
    Because it compares strings?
    Vanity of vanities, saith the Preacher, vanity of vanities; all is vanity.
    What profit hath a man of all his labour which he taketh under the sun?
    All the rivers run into the sea; yet the sea is not full; unto the place from whence the rivers come, thither they return again.
    For in much wisdom is much grief: and he that increaseth knowledge increaseth sorrow.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by B.Strouhal View Post
    i dont really understand how strcmp would help here
    You are working in a programming language that has no native string type... zero awareness of text.

    In C we use pseudo-strings which are arrays of characters with a trailing 0 added at the end.

    C cannot compare arrays with == < > <= >= ! != etc. it can only compare either numeric variables or pointers and since strings are arrays, it also cannot compare strings this way.

    (Yeah, I know ... WTF??? ... but that's how it is.)


    You need the C Library functions in string.h (and others) to manipulate text.

    You cannot do this...
    Code:
    char str1[100] = "Hello world";
    char str2[100] = "Hello world";
    
    if ( str1 == str2 )  
      puts("It worked");
    ... because all you are really doing is comparing the pointers, which will never be the same.

    So you use strcmp() like this...
    Code:
    char str1[100] = "Hello world";
    char str2[100] = "Hello world";
    
    if ( strcmp(str1, str2) == 0 )  // returns 0 if same
      puts("It worked");
    If you are going to do any serious programming at all you need to get a good grasp of this glaring omission in the language and the library functions used to compensate for it.

  10. #10
    Registered User
    Join Date
    Nov 2011
    Posts
    8
    have you figured out the solution to this program? if so i would be interested in seeing how you did because i too am not sure how you would fix this problem.

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,851
    > have you figured out the solution to this program? if so i would be interested in seeing how you did because i too am not sure how you would fix this problem.
    Start your own thread, post your own code - then we can help you with your specific issues.
    Simply hijacking someone else's thread with "gimmetehcodez" isn't going to teach you a thing.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Location
    Seguin, Texas, United States
    Posts
    6
    can strcmp be used for more than 2 strings or does it have to be run for each string individually?

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by B.Strouhal View Post
    can strcmp be used for more than 2 strings or does it have to be run for each string individually?
    Nope it can only compare 1 string with 1 string...

    There are several things you can do to aleviate the case sensitivity issue...
    1) use stricmp() if your library has it (hint: look it up)
    2) convert both strings to uppercase (using struper() and compare, if your library has it)
    3) rework your menu system to take single characters not strings and use toupper()

    There are other options as well... but these are the easiest to set up.

  14. #14
    Registered User
    Join Date
    Nov 2011
    Location
    Seguin, Texas, United States
    Posts
    6
    is there a way to make it all lowercase instead, thatd eliminate that 3rd step..? and then just compare each word to str?

  15. #15
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by B.Strouhal View Post
    is there a way to make it all lowercase instead, thatd eliminate that 3rd step..? and then just compare each word to str?
    At this point the teacher would tell you to ... "go look it up!"

    You will need to learn to read the library documentation sometime, now's as good a time as any.

    (And the answer is... yes there is.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Working with strings [using scanf()]
    By Luponius in forum C Programming
    Replies: 4
    Last Post: 04-26-2011, 11:27 AM
  2. Working with strings!
    By mass in forum C Programming
    Replies: 7
    Last Post: 06-03-2006, 12:15 AM
  3. Working with Strings and Numbers
    By jamez05 in forum C++ Programming
    Replies: 8
    Last Post: 10-19-2005, 01:39 PM
  4. compare strings not working
    By gtriarhos in forum C Programming
    Replies: 7
    Last Post: 09-29-2005, 01:51 PM
  5. working with strings
    By ygfperson in forum C++ Programming
    Replies: 5
    Last Post: 02-19-2002, 12:46 AM

Tags for this Thread


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