text processing assignment

This is a discussion on text processing assignment within the C Programming forums, part of the General Programming Boards category; The goal of this assignment is to count the number of words in a user entered string, count the number ...

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    20

    text processing assignment

    The goal of this assignment is to count the number of words in a user entered string, count the number of letters in the shortest and longest word, the average number of letters used, the number of newline characters used, and the number of times each word appears in the text. I have the first 5 parts done and cannot get the last part to work (the part that counts how many times each word appears).

    my professor gave us the following function prototype and description:
    void addword(char wordarray[][MAXWORD], char *word, int wordcount[], int *numwords);
    if the word is present in the word array increment the words count. if it isn't add the word to the array and set the appropriate element in word count to 1.
    Parameters:
    wordarray - array of words in text
    word - word to be processed
    wordcount - array of wordcount
    numwords - counter for total number of words in the array

    I have no idea how to begin to utilize this seeing as i only have a one dimensional input array.



    This is my code (I included the whole program because it basically encompasses everything I've learned so far, just to give you all an idea of my programming abilities):

    Code:
    #include <stdafx.h>
    #include <stdio.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]);
    
    
    int main(void)
    {
    	char inputarray[MaxData];
    	char newarray[MaxWords][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("&#37;[^\0]", inputarray);
    
    
    	count = wordcount(inputarray);
    	shortest = shortestword(inputarray);
    	longest = longestword(inputarray);
    	average = averagesize(inputarray, count);
    	newline = newlinecount(inputarray);
    
    	createnewarray(inputarray, newarray);
    
    
    	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; newarray[counter1][counter2] != '\0'; counter2++)
    		{
    			printf("%c", newarray[counter1][counter2]);
    		}
    	}
    
    
    	for(;;);
    	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++;
    		}
    	}
    
    }
    Last edited by nellosmomishot; 11-23-2008 at 02:46 PM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Code:
    void addword(char wordarray[][MAXWORD], char *word, int wordcount[], int *numwords);
    He's clearly showing a wordarray of two dimensions, here. You'll need two D's to get words lined up in "rows" and "columns". One dimension won't do.

  3. #3
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    ok, i tried converting my single dimension array to a two dimensional array with one word stored on each row of the array.

    i added the following to main:

    Code:
    	char newarray[MaxWords][MaxData];
    	int counter1 = 0;
    	int counter2 = 0;
    
    
    	createnewarray(inputarray, newarray);
    
    
    	for (counter1 = 0; counter1 != count; counter1++)
    	{
    		for (counter2 = 0; counter2 != '\0'; counter2++)
    		{
    			printf("&#37;c", newarray[counter1][counter2]);
    		}
    		printf("\n");
    	}
    and I created the following function:

    Code:
    void createnewarray(char a[], char b[MaxWords][MaxData])
    {
    	char *counter = 0;
    	int counter1 = 0;
    	int word = 0;
    	for (counter = &a[0]; *counter != '\0'; counter++)
    	{
    		b[word][counter1] = *counter;
    		counter1++;
    		if (*counter == ' ')
    		{
    			b[word][counter1] = '\0';
    			word++;
    			counter1 = 0;
    		}
    	}	
    }
    I have the nested for loops to check if my function is working properly but it's not. I get an ouput of just blank lines, it does drop down one line for each word in there but it doesn't print out any of the words.

    edit:
    I figured out the problem lies in the function, for some reason nothing is being stored in the new array.
    Last edited by nellosmomishot; 11-22-2008 at 09:11 PM. Reason: more to say

  4. #4
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    I still can't get this to work, I tried creating a one dimensional array of pointers but i'm still having the same problem, nothing is written to the new array.

    this is what i added to main:

    Code:
    	char *newarray[MaxWords];
    	int counter1 = 0;
    
    	createnewarray(inputarray, newarray);
    
    	for (counter1 = 0; counter1 != count; counter1++)
    	{
    		printf("&#37;c\n", *newarray[counter1]);
    	}
    and this is the new function:

    Code:
    void createnewarray(char a[], char *b[MaxWords])
    {
    	int counter = 0;
    	int counter1 = 0;
    	for (counter = 0; a[counter] != EOF; counter++)
    	{
    		if ((a[counter] == ' ') || (a[counter] == '\n'))
    		{
    			a[counter] = '\0';
    		}
    	}
    	*b[counter1] = a[0];
    	counter1++;
    	for (counter = 0; a[counter] != EOF; counter++)
    	{
    		if( a[counter] = '\0')
    		{
    			counter++;
    			*b[counter1] = a[counter];
    			counter1++;
    		}
    	}
    }
    Last edited by nellosmomishot; 11-23-2008 at 01:28 PM. Reason: forgot the tags

  5. #5
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    char *newarray[MaxWords];

    this is array of not initialized pointers. Do you have somewhere initialization routine?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  6. #6
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    no I do not, everything in my program is either in the first post, or the third post. Wouldn't an uninitialized pointer work like an uninitialized variable? As long as I set it equal to something before I perform an action on it I should be fine right? I'm not too good with pointers, I read the two chapters in the book on pointers a few times but I guess they're not my strong suit.

  7. #7
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    As long as I set it equal to something before I perform an action on it I should be fine right
    yes... but you do not do it before using it...
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  8. #8
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    that whole new function is how i'm trying to initialize each pointer in the array. I want each one to point to a word, but only one word, in the input. any suggestions on how to make that function do what i'm trying to have it do?

  9. #9
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    ok I finally got my two dimensional array of words, now I still have no clue how to check how many times each word appears, I updated my code in the first post to show what i currently have.

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,655
    The very general steps are:

    Sort the word set.
    Start with the first word and compare the next word. If it is the same, increment a count and compare the next. If it isn't, compare the current word with the word after that and increment some count.
    Display the results.

    You will have to fill in the other operations depending on where you are confused and what you understand. For example, since you'll likely implement this with loops, you may have to display results before you move on to counting up the number of instances of the next word (that is, whatever caused a mismatch).

    Write down a clear set of operations first and then implement.

  11. #11
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    ok I wrote something that I believe will count the words in the function the way I want it too, but now I have two errors:

    text processing error LNK2019: unresolved external symbol "void __cdecl countarray(char (* const)[1000],char (* const)[1000],int * const)" (?countarray@@YAXQAY0DOI@D0QAH@Z) referenced in function _main

    text processing fatal error LNK1120: 1 unresolved externals

    This assignment is due at 6pm tonight so any help at all is much appreciated.

    this is what my program looks like now:

    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("&#37;[^\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]);
    	}
    
    
    
    	for(;;);
    	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[newwordcount];
    				c[newwordcount] = numbers + 1;
    				same = 1;
    			}
    		}
    		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++;
    		}
    	}
    }
    Last edited by nellosmomishot; 11-25-2008 at 01:44 PM.

  12. #12
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    ok I fixed the unresolved external (was missing an argument in my function call and a parameter in my prototype).

    however, it does not work properly, it prints out garbage for a word and a huge negative number for its count.

    edit: it works if I enter only one word lol, it tells me that the word appeared one time
    Last edited by nellosmomishot; 11-25-2008 at 11:51 AM.

  13. #13
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Does it print out garbage for every word, or just one word, or what? Do you get any output you like?

  14. #14
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    this is what i get for input when I enter 1 word, then when I enter more than one word

    1 word:
    Input the text to be tested:
    why
    ^Z

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

    why 1

    multiple words:
    Input the text to be tested:
    why is this not working
    ^Z

    Unhandled exception at 0x00412628 in text processing.exe: 0xC0000005: Access violation writing location 0x001302b0.

    it points to here in my program:
    b[newwordcount][charcounter] = a[counter][charcounter];

  15. #15
    Registered User
    Join Date
    Nov 2008
    Posts
    20
    I tweaked it a bit more and now it doesnt flip out when I enter more than one word (the changes are in my post 3 posts up), but it still gives crap results. here is what it sais when I have more than one word.

    Input the text to be tested:
    why do I bother
    ^Z

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

    why 1
    do 2
    I 2
    I 2

Page 1 of 2 12 LastLast
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

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