Hello, I was wondering if anyone knew how to get a program to count the number of certain elements in a string.
Here's an example code I made. How would I determine the number of spaces in line1 and the number of punctuation marks in line2 that would return that back to the main?
Code:
#include <stdio.h>
#include <string.h>
int spaceCount(char line1[]);
int locatePunctuation(char line2[]);
void main()
{
char line1[] = "I like chocolate chip cookies."
char line2[] = "Hi. My name is DW. How are you today?"
printf(spaceCount(line1));
printf(locatePunctuation(line2));
}
int spaceCount (char line1[])
{
}
int locatePunctuation(char line2[])
{
}
I'm guessing for punctuation I would need something like (correct me if I'm wrong):
Code:
int count = 0, i;
for(i = 0; i < strlen(line);i++) // counts # of punctuation marks in the string
{
if(ispunct(line[i] )) { count++; }
}
printf(“%d \n”, count);
??
Not sure about spaces though.
Thanks!