-
word count troubles
Hey, I was just wondering if you guys could help me better understand this concept:
given:
Code:
int bubble(int a[], int n)
and:
Code:
int a[8] = {1,2,3,4,5,6,7,8}
we can invoke the bubble funtion by using:
Correct?
Now, if I have the user input a sting into a program such as:
Code:
#define MAXSTRING 100
void word_cnt (char a[], int n) // not sure if that is correct//
int main(void)
{
char c, a[MAXSTRING];
int i;
printf("\nPlease enter your favorite string. ");
for(i = 0,(c = getchar()) != '\n'; ++i){
a[i] = c
}
a[i] = '\0';
word_cnt(a,MAXSTRING)
}
Is this a correct way of calling a function and passing the string to it?
Is it silly to use a function and not just count the words in main(void)?
I'm also not sure how to count through the characters in a string in another function.
-
You do not actually pass an array to function - rather you pass the address of the first element. The array name is a pointer set to that address.
In a function header, the char a[] is equivalent to char* a, which is a pointer that holds the address of the array that is passed to it.
In the function you can use the same method of accessing an array as you would had it not been in a function because this (accessing an element of the array)
is the same as
You can use either, even if it is main().
-
Yes it's correct but the lack of semicolons and that method you've got for getting a string isn't. Use fgets(a, MAXSTRING, stdin) instead.
And by counting characters, do you mean something like strlen()?
-
What I'm suppose to do it count the words in a string input by a user in a fuction word_cnt(). I'm suppose to use array notation with subscripts to do this, instead of using pointers to process the string. I just wasn't sure how to use a function if I couldn't use pointers. It's a little confusing to me.
-
This is what I came up with for my word_cnt function. I know the for statment isn't the best way, but that is what i'm suppose to use so bear with me:
Code:
void word_cnt (char a[], int n)
{
int i, cnt = 0;
for( i = 0; i != '\0'; ++i){
while(isspace (a[i])) //skip white spaces
;
if (a[i] != '\0'){ //finds a word
++cnt;
while (!isspace(a[i]) && a[i] != '\0') //skips word when found
;
}
}
printf("The number of words in your string is:%d", cnt");
}
I'm a newbie to C programming, so if this has terrible style or is rediculously wrong, go easy on me :rolleyes:
-
So, have you tried it? Think about your loop condition(s). In your while loops: what might change true to false? You can do this without nested loops if you have a variable to keep track of whether or not you are currently in a word or whitespace.
-
No, I'm sorry. I haven't been able to try it yet. I will be able to this evening. I just wanted to make sure that I was on the right track with what I was coming up with. I'm not really very good at this stuff yet, but it's coming along.
So do you think I have to much stuff in my word_cnt function?
-
It is more complicated then it needs to be, and doesn't work.
I'm sure you'd notice these errors when you actually try to compile and run
Code:
for( i = 0; [a]i != '\0'; ++i){
the first time you reach a space you'll have an infinte loop
Code:
while(isspace (a[i])) //skip white spaces
;
Something like this should work
Code:
int word_cnt(char[a])
{
int cnt = 0
char previous = ' '
for each position in the string{
if current position is not a space and previous position is a space
cnt++
previous = current
}
return cnt
}
-
is char [a] correct?
or should it be char a[] ?
-
-
Code:
printf("The number of words in your string is:%d", cnt");
You have an extra double quote there.
Code:
while(isspace (a[i])) //skip white spaces
;
To get this to work you need to increment i in the while loop.
BTW, you can use sizeof to find the size of an array:
Code:
int array[10] = {1, 2, 3};
word_cnt(array, sizeof(array)/sizeof(array[0]));
Should you be passing an int array to a word counting function?
isspace() and family are in <ctype.h>.
-
oh, for my word count function, I will be counting the words input by the user in a string, so I was wondering if I passed the address correctly as:
or should it be
Code:
word_cnt(char a[], int n)
where n is the size, but i'm confused here because i don't know the size of the string yet. Do I need to find the size of the string before I start counting words?
-
Well, look at how much you are using n in your code. Since the function takes a string and you find the end via the null termination, passing a size parameter is not necessary.
-
you're right. when looking at my function, i don't even use n. silly question on my part. :rolleyes: