Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #define MAX 1000
- #define SIZE 50
- void read_line(char *n);
- int main() {
- char string[MAX], words[MAX][SIZE], aux[SIZE];
- int i, j, k, n, count;
- char *token;
- const char s[4] = " .,!";
- i = j = k = n = 0;
- printf("Please enter the sentence that you would like to tokenize: ");
- read_line(string);
- token = strtok(string, s);
- while( token != NULL )
- {
- strcpy(words[j], token);
- j++;
- token = strtok(NULL, s);
- }
- n = j-1;
- for (i = 0; i < n; i++) {
- strcpy(aux, words[i]);
- for (j = i + 1; j <= n; j++) {
- if (strcmp(words[i], words[j]) > 0) {
- strcpy(aux, words[j]);
- strcpy(words[j], words[i]);
- strcpy(words[i], aux);
- }
- }
- }
- printf("Frequency:\n");
- i = 0;
- while (i <= n) {
- count = 1;
- if (i != n) {
- for (j = i + 1; j <= n; j++) {
- if (strcmp(words[i], words[j]) == 0) {
- count++;
- }
- }
- }
- printf("%s\t%d\n", words[i], count);
- i = i + count;
- }
- return 0;
- }
- void read_line(char *n) {
- char ch;
- while ( (ch = getchar()) != '\n') {
- *n = ch;
- n++;
- }
- *n = '\0';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement