Hey, thanks for the replies.
I tried your suggestions and now after I input the string and press enter I get the message: "Segmentation Fault (core dumped)"
Im including my code this time.
Maybe you can spot the error. I suspect it has something to do with pointers, since they are new to me.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_SIZE 1000 /* max size of the char array*/
Code:
int main(void)
{
int wordLength;
char line[MAX_SIZE]; /*array for the line of codeas a whole*/
char *tokenPtr; /* create char pointer */
int sum, length, counter = 0; /*sum of all words */
float average;
int j, i=0;
printf("Enter String: "); /*prompt for string */
gets(line); /* use function gets from the string header to read input from keyboard */
tokenPtr = strtok( line, " \t\n"); /* begin tokenizing sentence */
while(tokenPtr != NULL) {
sum += findLength(tokenPtr);
counter++;
average = (float)sum / (float)counter;
tokenPtr = strtok(NULL, " \t\n"); /*get next token */
}/*end while */
/*some sample calculations */
for(j=0; j < strlen(tokenPtr); j++)
printf("%s\n", tokenPtr);
for (i = 0; i < strlen(tokenPtr) ; i++ ){
if ( tokenPtr[i] == 'a' )
printf("Found an a! \n");
else
printf("not found");
}
printOutput(sum, counter, average);
}
int findLength(char *newWord)
{
int length;
return length = strlen(newWord);
}
int printOutput(int s,int count,int ave)
{
printf("the sum is %d\n", s);
printf("the number of words is %d\n", count);
printf("the average is %.2f\n", ave);
}
PS. I know that gets is evil and all, but this is the only way that i know for now. ;0
thanks a bunch