Thread: text processing assignment

  1. #16
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Well, that line appears twice. I'm guessing your array indices are getting off somehow -- if you are using Visual Studio, when you hit F5 and your program stops, you should see the current "state of play" of your program, so you can see what all your variables are. Granted, at the moment I don't see how newwordcount gets past 0, so I don't see how a lot of the code actually gets executed.

  2. #17
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    yea I added a couple of newwordcounter++ in there in my edited post, what do you mean that my array indeces are getting off?

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You're adding words every time they miss, not when they miss everything -- so for instance, why and I don't match, so you add I to the list in the third spot; but then do and I don't match, either, so you add I to the fourth spot too; then the third I and the fourth I match, so you add 1 to the third count, and the fourth I and the fourth I match so you add 1 to the fourth count.

  4. #19
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    I'm not quite sure how to remedy the situation, how exactly can I fix that? I'm assuming I need a break statement somewhere but I don't know where.

  5. #20
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by nellosmomishot View Post
    I'm not quite sure how to remedy the situation, how exactly can I fix that? I'm assuming I need a break statement somewhere but I don't know where.
    That means you can't add words inside that loop. Only after you've tried to match every word so far, and failed on each of them, can you add your word as a new unique word.

  6. #21
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    wow thanks, that fixed a problem, i'm updating my code to show the changes. It doesn't print the words multiple times anymore, but it doesn't add them up still. this is my output now:

    Input the text to be tested:
    strings that are longer than three
    words work now
    that that that
    ^Z

    There are 12 words in the text.
    The shortest word has 3 letters in it.
    The longest word has 7 letters in it.
    The average size of a word is 4 letters.
    There were 3 newline characters used.

    strings 1
    that 1
    are 1
    longer 1
    than 1
    three 1
    words 1
    work 1
    now 1
    that 1
    that 1
    that 1

  7. #22
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    even using strcmp to compare my strings it isn't working. Is there something wrong with my function that creates the two dimensional array? (void createnewarray) I want it to store 1 word on each line terminated by a null statement with no white space. I think that white space is making it into some of the strings in mine right now

  8. #23
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    But saying "int same = 1" declares a new variable named same -- which means once that close curly brace happens, the new variable is gone and you're back to the original int same, which still has a value of 0.

  9. #24
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    I changed it around a little more (the updated code is in my post a couple back). Now it goes into an infinite loop any time that I enter a word more than once

  10. #25
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Considering all you are doing is gathering statistics I find your code a bit convoluted. I mean really you only need to read what is going on inside your buffer, not keep track of the buffer itself. But that is just my two cents.

  11. #26
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    I'm not quite sure what you mean by keep track of the buffer, what I need to do is see how many times every word in the string appears and the only way that I was able to figure out how to attempt to do that was the way it is now

  12. #27
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    okay, i'm wicked close now. It keeps correct count of all the words, outputs them correctly. But now it goes into an infinite loop after it prints out the output. I'm almost positive that the loop is coming from my print statement in main. here is my current code:

    Code:
    #include <stdafx.h>
    #include <stdio.h>
    #include <string.h>
    
    #define MaxData 1000
    #define MaxWords 100
    
    
    int wordcount(char a[MaxData]);
    int shortestword(char a[MaxData]);
    int longestword(char a[MaxData]);
    int averagesize(char a[MaxData], int wordcount);
    int newlinecount(char a[MaxData]);
    void createnewarray(char a[], char b[MaxWords][MaxData]);
    void countarray(char a[][MaxData], char b[][MaxData], int c[MaxData], int count);
    int stringcompare(char a[MaxData], char b[MaxData]);
    
    
    int main(void)
    {
    	char inputarray[MaxData];
    	char newarray[MaxWords][MaxData];
    	char wordsarray[MaxWords][MaxData];
    	int numbersarray[MaxData];
    	int counter1 = 0;
    	int counter2 = 0;
    	int count = 0;
    	int shortest = 0;
    	int longest = 0;
    	int average = 0;
    	int newline = 0;
    
    
    	printf("Input the text to be tested: \n");
    	scanf("%[^\0]", inputarray);
    
    
    	count = wordcount(inputarray);
    	shortest = shortestword(inputarray);
    	longest = longestword(inputarray);
    	average = averagesize(inputarray, count);
    	newline = newlinecount(inputarray);
    
    	createnewarray(inputarray, newarray);
    	countarray(newarray, wordsarray, numbersarray, count);
    
    
    	printf("\nThere are %d words in the text.\n", count);
    	printf("The shortest word has %d letters in it.\n", shortest);
    	printf("The longest word has %d letters in it.\n", longest);
    	printf("The average size of a word is %d letters.\n", average);
    	printf("There were %d newline characters used.\n", newline);
    
    	for (counter1 = 0; counter1 < count; counter1++)
    	{
    		printf("\n");
    		for(counter2 = 0; wordsarray[counter1][counter2] != '\0'; counter2++)
    		{
    			printf("%c", wordsarray[counter1][counter2]);
    		}
    		printf("\t\t%d", numbersarray[counter1]);
    	}
    
    
    
    	return 0;
    }
    
    
    int wordcount(char a[MaxData])
    {
    	int wordcounter = 0;
    	char *counter;
    	for (counter = &a[0]; *counter != '\0'; counter++)
    	{
    		if ((*counter == ' ') || (*counter == '\n'))
    		{
    			wordcounter++;
    		}
    	}
    	return wordcounter;
    }
    
    
    int shortestword(char a[MaxData])
    {
    	int charcounter = 0;
    	int shortestword = 500;
    	char *counter;
    	for (counter = &a[0]; *counter != '\0'; counter++)
    	{
    		charcounter++;
    		if((*counter == ' ') || (*counter == '\n'))
    		{
    			charcounter--;
    			if(charcounter < shortestword)
    			{
    				shortestword = charcounter;
    				charcounter = 0;
    			}
    			else
    				charcounter = 0;
    		}
    	}
    	return shortestword;
    }
    
    
    int longestword(char a[MaxData])
    {
    	int charcounter = 0;
    	int longestword = 0;
    	char *counter;
    	for (counter = &a[0]; *counter != '\0'; counter++)
    	{
    		charcounter++;
    		if((*counter == ' ') || (*counter == '\n'))
    		{
    			charcounter--;
    			if(charcounter > longestword)
    			{
    				longestword = charcounter;
    				charcounter = 0;
    			}
    			else
    				charcounter = 0;
    		}
    	}
    	return longestword;
    }
    
    
    int averagesize(char a[MaxData], int wordcount)
    {
    	int charcounter = 0;
    	int averageword = 0;
    	char *counter;
    	for (counter = &a[0]; *counter != '\0'; counter++)
    	{
    		charcounter++;
    		if((*counter == ' ') || (*counter == '\n'))
    		{
    			charcounter--;
    			averageword += charcounter;
    			charcounter = 0;
    		}
    	}
    	averageword = averageword / wordcount;
    	return averageword;
    }
    
    
    int newlinecount(char a[MaxData])
    {
    	int newlinecounter = 0;
    	char *counter;
    	for (counter = &a[0]; *counter != '\0'; counter++)
    	{
    		if(*counter == '\n')
    		{
    			newlinecounter++;
    		}
    	}
    	return newlinecounter;
    }
    
    
    void createnewarray(char a[], char b[MaxWords][MaxData])
    {
    	char *counter;
    	int bcounter = 0;
    	int words = 0;
    	for (counter = &a[0]; *counter != '\0'; counter++)
    	{
    		if ((*counter == ' ') || (*counter == '\n'))
    		{
    			b[words][bcounter] = '\0';
    			words++;
    			bcounter = 0;
    		}
    		else
    		{
    		b[words][bcounter] = *counter;
    		bcounter++;
    		}
    	}
    }
    
    
    int stringcompare(char a[MaxData], char b[MaxData])
    {
    	for (int counter1 = 0 ; a[counter1] != '\0'; counter1++)
    	{
    		if (a[counter1] == b[counter1])
    		{
    		}
    		else
    		{
    			return 0;
    		}
    	}
    	return 1;
    }
    
    
    void countarray(char a[][MaxData], char b[][MaxData], int c[MaxData], int count)
    {
    	int counter = 0;
    	int newwordcount = 0;
    	int charcounter = 0;
    	int testcounter = 0;
    	int same = 0;
    	int charcount1 = 0;
    	int isthesame = 0;
    	int numbers = 0;
    	char temparray1[MaxData] = "temp array 1";
    	char temparray2[MaxData] = "temp array 2";
    	for (charcounter = 0; a[counter][charcounter] != '\0'; charcounter++)
    	{
    		b[newwordcount][charcounter] = a[counter][charcounter];
    	}
    	b[newwordcount][charcounter] = '\0';
    	c[newwordcount] = 1;
    	newwordcount++;
    	
    	for (counter = 1; counter < count; counter++)
    	{
    		same = 0;
    		for (charcounter = 0; a[counter][charcounter] != '\0'; charcounter++, charcount1++)
    		{
    			temparray1[charcounter] = a[counter][charcounter];
    		}
    		temparray1[charcounter] = '\0';
    		
    		for (testcounter = 0; testcounter < newwordcount; testcounter++)
    		{
    			for (charcounter = 0; b[testcounter][charcounter] != '\0'; charcounter++, charcount1++)
    			{
    				temparray2[charcounter] = b[testcounter][charcounter];
    			}
    			temparray2[charcounter] = '\0';
    			
    			isthesame = stringcompare(temparray1, temparray2);
    			
    			if (isthesame == 0)
    			{
    			}
    			else
    			{
    				numbers = c[testcounter];
    				c[testcounter] = numbers + 1;
    				same = 1;
    			}
    			if (same == 1)
    			{
    				break;
    			}
    		}
    		if (same == 0)
    		{
    			for (charcounter = 0; a[counter][charcounter] != '\0'; charcounter++)
    			{
    				b[newwordcount][charcounter] = a[counter][charcounter];
    			}
    			b[newwordcount][charcounter] = '\0';
    			c[newwordcount] = 1;
    			newwordcount++;
    		}
    	}
    }

  13. #28
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I didn't read the entire specification of what the program needed to do... but here is some quick code for you to play with.

    Example:
    Code:
    #include <stdio.h>
    #include <ctype.h>
    #include <limits.h>
    
    int letter_count(const char *buffer, const char **end)
    {
      const char *p = buffer;
    
      while(*p && !isspace(*p))
        ++p;
    
      if(end)
        *end = p;
    
      return p - buffer;
    }
    
    int space_count(const char *buffer, const char **end)
    {
      const char *p = buffer;
    
      while(*p && isspace(*p))
        ++p;
    
      if(end)
        *end = p;
    
      return p - buffer;
    }
    
    int main(void)
    {
      char buffer[256];
      const char *s;
      int nwords = 0, nchars = 0, min = INT_MAX, max = INT_MIN, last = 0;
    
      while(fgets(buffer, sizeof(buffer), stdin))
      {
        for(s = buffer; *s;)
        {
          last = letter_count(s, &s);
    
          if(last)
          {
            ++nwords;
    
            if(last < min)
              min = last;
    
            if(last > max)
              max = last;
    
            nchars += last;
          }
    
          space_count(s, &s);
        }
      }
    
      printf("The largest word types was &#37;d characters.\n"
        "The shortest word typed was %d characters.\n"
        "%f characters is the average word length.\n", min, max, (float)nchars/(float)nwords);
      
      return 0;
    }
    Or something to that effect, anyway.

  14. #29
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    If you are a little clever its not that hard to even maintain track of what the longest and shortest words actually were, for that matter

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. DirectX | Drawing text
    By gavra in forum Game Programming
    Replies: 4
    Last Post: 06-08-2009, 12:23 AM
  2. Assignment to decrypt a text.
    By hubris in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2009, 12:16 PM
  3. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM
  4. file writing crashes
    By test in forum C Programming
    Replies: 25
    Last Post: 08-13-2002, 08:44 AM
  5. Replies: 1
    Last Post: 07-13-2002, 05:45 PM