I'm trying to split a sentence the user inputs to an array of words so I can later manipulate the words separately as strings.
The code is compiling but prints only garbage after the user input.
I tried debugging but don't see the problem. Can someone help me fix it?
#include <stdio.h>
#include <string.h>
int main() {
char str[1000];
int i = 0;
char rev[1000][1000];
int r = 0;
puts("Enter text:");
gets(str);
int k, length = 0;
printf_s("So the words are:\n");
while (str[i] != '\0') {
if (str[i] == ' ') {
k = i - length;
do {
rev[r][k] = (str[k]);
k++;
} while (str[k] != ' ');
printf(" ");
length = (-1);
r++;
} else
if (str[i + 1] == '\0') {
k = i - length;
do {
rev[r][k] = (str[k]);
k++;
} while (str[k] != '\0');
length = 0;
r++;
}
length++;
i++;
}
for (int r = 0; r < 1000; r++)
printf("%s ", rev[r]);
return 0;
}
fix like this
#include <stdio.h>
int main(void) {
char str[1000];
char rev[1000][1000];
puts("Enter text:");
fgets(str, sizeof str, stdin);//Use fgets instead of gets. It has already been abolished.
int r = 0;
int k = 0;
for(int i = 0; str[i] != '\0'; ++i){
if (str[i] == ' ' || str[i] == '\n'){//is delimiter
if(k != 0){
rev[r++][k] = '\0';//add null-terminator and increment rows
k = 0;//reset store position
}
} else {
rev[r][k++] = str[i];
}
}
if(k != 0)//Lastly there was no delimiter
rev[r++][k] = '\0';
puts("So the words are:");
for (int i = 0; i < r; i++){
printf("%s", rev[i]);
if(i < r - 2)
printf(", ");
else if(i == r - 2)
printf(" and ");
}
return 0;
}
Replace you declaration
char rev[1000][1000];
with
char * rev[1000]; // We will need pointers only
int i = 0; // Index to previous array
and all your code after
puts( "Enter text:" );
with this:
fgets( str, 998, stdin ); // Safe way; don't use gets(str)
const char delim[] = ",; "; // Possible delimiters - comma, semicolon, space
char *word;
/* Get the first word */
word = strtok( str, delim );
rev[i++] = word;
/* Get the next words */
while( word != NULL )
{
word = strtok( NULL, delim );
rev[i++] = word;
}
/* Testing */
for (int r = 0; r < i - 1; r++)
printf( "%s\n", rev[r] );
return 0
}
As you can see, all dirty work is done with the strtok() function ("string to tokens") which walks through other and other words ("tokens"), recognizing them as delimited by one or more characters from the string delim.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int count_spaces(char *str)
{
if (str == NULL || strlen(str) <= 0)
return (0);
int i = 0, count = 0;
while (str[i])
{
if (str[i] == ' ')
count++;
i++;
}
return (count);
}
int count_char_from_pos(char *str, int pos)
{
if (str == NULL || strlen(str) <= 0)
return 0;
int i = pos, count = 0;
while (str[i] && str[i] != ' ')
{
count++;
i++;
}
return count;
}
char **get_words(char *str)
{
if (str == NULL || strlen(str) <= 0)
{
printf("Bad string inputed");
return NULL;
}
int i = 0, j = 0, k = 0;
char **dest;
if ((dest = malloc(sizeof(char*) * (count_spaces(str) + 1))) == NULL
|| (dest[0] = malloc(sizeof(char) * (count_char_from_pos(str, 0) + 1))) == NULL)
{
printf("Malloc failed\n");
return NULL;
}
while (str[i])
{
if (str[i] == ' ') {
dest[j++][k] = '\0';
if ((dest[j] = malloc(sizeof(char) * (count_char_from_pos(str, i) + 1))) == NULL)
{
printf("Malloc failed\n");
return NULL;
}
k = 0;
}
else {
dest[j][k++] = str[i];
}
i++;
}
dest[j][k] = 0;
dest[j + 1] = NULL;
return dest;
}
int main(void) {
char *line = NULL;
size_t n = 0;
getline(&line, &n, stdin);
printf("%s\n", line);
line[strlen(line) - 1] = 0;
printf("%s\n", line);
char **tab = get_words(line);
int i = 0;
while (tab[i])
{
printf("%s\n", tab[i++]);
}
}
here is a long but fully working example
get the user input
then send it to get_words function. It will get the number of words, the number of characters for each words, allocate everything in memory and writes chars then return it. You get a char ** and prints it just tested it it works
If you wish to split a string into an array of strings, you should consider the strtok function from #include <string.h>. The strtok function will the split the string on the given delimiter(s). For your case, it would the " ".
Using the strtok example from Tutorials Point:
#include <string.h>
#include <stdio.h>
int main(){
char str[80] = "This is - www.tutorialspoint.com - website";//The string you wish to split
const char s[] = "-";//The thing you want it to split from. But there is no need to this.
char *token;//Storing the string
/* get the first token */
token = strtok(str, s);//Split str one time using the delimiter s
/* walk through other tokens */
while( token != NULL )
{
printf( " %s\n", token );//Print the string
token = strtok(NULL, s);//Split the string again using the delimiter
}
return(0);
}
Related
I have been attempting to fix this for hours. I am trying to read a file called wiktionary called "stuff.txt" which has input of the form:
10000
5627187200 the
3395006400 of
2994418400 and
2595609600 to
1742063600 in
...
The 10000 represents the number of lines. I am then reading it in using a function read_in_terms, and extracting the number (weight) and the word (word) from it. I have tested my get_word and get_weight functions seperately and they seem to be working. However, when I am reading in, my get_word function is not returning the word .... the get_weight number is...
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
char* concat(const char *s1, const char *s2) {
char *result = malloc(strlen(s1) + strlen(s2) + 1);
strcpy(result, s1);
strcat(result, s2);
return result;
}
// from a string of the form " 29480398934 the" it returns number
long get_weight(char *string) {
long length = strlen(string);
char number[200];
int count = 0;
// Here I am cycling through until X__X combination
// I only add to the number if it is not " "
for (int i = 0; i < length; i++) {
if (string[i-1] != ' ' && string[i] == ' ' && string[i+1] == ' ' && string[i+1] != ' ') {
break;
} else if (string[i] != ' '){
number[count] = string[i];
count++;
}
}
number[count] = '\0';
return atol(number); // converts it to a long and then returns
}
// from a string of the form " 453495834 word" it returns word
char * get_word(char* string) {
long length = strlen(string);
char word[200];
int count = 0;
// the double space detection only works once we reach a character
// this becomes 1 once that happens, allowing us to proceed to the next part
int blocker = 0;
for (int i = 0; i < length; i++) {
if (string[i] != ' '){
blocker++;
}else if (blocker > 0 && string[i] == ' ' && string[i+1] == ' ') {
int int_count = i+1;
while (string[int_count] != '\0') {
int_count++;
word[count] = string[int_count];
count++;
}
}
}
word[count] = '\0';
// now I need to copy this string into malloc
char *str = (char *)malloc(count*sizeof(char));
strcpy(str, word);
return str;
}
void read_in_terms(int *pnterms, char *filename) {
char part1[] = "/Users/adammartinez/Desktop/";
char* title = concat(part1,filename);
FILE *fp = fopen(title, "r");
char line[200];
fgets(line, sizeof(line), fp);
int num_lines = atoi(line);
for (int i = 0; i < num_lines; i++){
fgets(line, sizeof(line), fp);
printf("%ld \n", get_weight(line));
printf("%s \n", get_word(line)); // NOT RETURNING WORDS
}
}
int main(void) {
//char* test_str = " 547584758475 the";
//printf("%s %ld", get_word(test_str), get_weight(test_str));
int terms = 1000;
int *pointer = &terms;
read_in_terms(pointer, "stuff.txt");
return 0;
}
It seems to work fine on my computer. Please look at the output below and tell me if it's what you're looking for. I set part1 to "" and I changed the number of lines in stuff.txt to 5.
5627187200
the
3395006400
of
2994418400
and
2595609600
to
1742063600
in
I've encountered a problem with my code where the function I made rotate_left that's supposed to take the first word in a given string and puts it at the end of that string and that function works but when it tried to activate it twice it doesn't do anything other than printing the same result the first function printed anyone got any ideas about it?
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 80
void rotate_left(char str[])
{
int j = 0;
int i = 0;
char Temp[MAX];
char Temp2[MAX];
while (str[i] != ' ')
{
i++;
}
Temp[i] = '\0';
strncpy(Temp, str, i);
strcat(str, " ");
while (str[i] != '\0')
{
Temp2[j] = str[i];
i++;
j++;
}
Temp2[j] = '\0';
strcat(Temp2, Temp);
printf("%s\n", Temp2);
}
int main()
{
char str[MAX];
char Temp2[MAX];
printf("Enter Your String To Swtich The : ");
gets(str);
rotate_left(str);
rotate_left(str);
rotate_left(str);
return 0;
}
You have two bugs in these codes :
First you must copy the rotated string in the passed one, printing the result is not sufficient. Next you must remove the space at the beginning of the extracted word otherwise the new string start with a space.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 80
void rotate_left(char str[])
{
int j = 0;
int i = 0;
char Temp[MAX];
char Temp2[MAX];
// If the first space is not removed (see below), the next time
// the function is called, this loop stops immediately. It's why
// the second rotation didn't occur
while (str[i] != ' ')
{
i++;
}
Temp[i] = '\0';
strncpy(Temp, str, i);
strcat(str, " ");
i++; //<====== removes the space
while (str[i] != '\0')
{
Temp2[j] = str[i];
i++;
j++;
}
Temp2[j] = '\0';
strcat(Temp2, Temp);
strncpy(str, Temp2, strlen(Temp2) + 1); //<====== copies the string
printf("%s\n", str);
}
int main()
{
char str[MAX] = "Enter Your String To Switch The";
char Temp2[MAX];
for (int i=0; i<7; i++) rotate_left(str);
return 0;
}
Output :
Your String To Switch The Enter
String To Switch The Enter Your
To Switch The Enter Your String
Switch The Enter Your String To
The Enter Your String To Switch
Enter Your String To Switch The
Your String To Switch The Enter
I didn't handle special cases (if there is no space in the provided string for instance) and I removed the interactive dimension (gets) for the example.
Your code has issues:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define MAX 80
void rotate_left(char str[])
{
int j = 0;
int i = 0;
char Temp[MAX];
char Temp2[MAX];
while (str[i] != ' ')
{
i++;
}
in the code below you are having a security leak and a potential problem because you have temp variables in the stack and you are touching these without boundary checks: Example of a buffer overflow leading to a security leak
you need to write code with boundary checks especially when the variable is in the stack:
while (i < MAX /*or i < sizeof(str)*/ && str[i] != ' ' && str[i] != '\0' /*Null check also?*/ )
{
i++;
}
If you call this method with a string without space in it then god knows what will it result,
There are other issues as well like "strncpy(str, Temp2, strlen(Temp2) + 1);" this is not how this method is intended to be called as it should be "strncpy(str, Temp2, sizeof(str));": https://linux.die.net/man/3/strncpy
Let me write you sample code it will be faster for you to track some of the missing checks:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX 80
/* just your code with safety checks so you can track why it fails */
int rotate_left(char str[], const size_t inputSize)
{
int j = 0;
int i = 0;
char Temp[MAX];
char Temp2[MAX];
while (i < inputSize && str[i] != ' ' && str[i] != '\0')
{
i++;
}
if (i == inputSize || str[i] == '\0')
{
// string without any space in it so no need to rotate?
return 0;
}
if (i + 1/*+1 to include null*/ >= MAX) {
// We dont have enough memory to handle this request
return -1;
}
strncpy(Temp, str, i);
Temp[i] = '\0';
++i;
while (i < inputSize && j < MAX && str[i] != '\0')
{
Temp2[j] = str[i];
i++;
j++;
}
if (i == inputSize) {
// String is not null terminated?
return -1;
}
if (j + 2 /*Include space and null character*/ >= MAX) {
// We dont have enough memory to handle this request
return -1;
}
Temp2[j] = ' ';
Temp2[j + 1] = '\0';
strncat(Temp2, Temp, sizeof(Temp2));
strncpy(str, Temp2, inputSize); //<====== copies the string
printf("%s\n", str);
return 0;
}
/* bit more optimization */
errno_t rotate_left_optimized(char str[], const size_t inputSize)
{
errno_t lastCall;
char temp[MAX];
int i = 0;
while (i < inputSize && str[i] != ' ' && str[i] != '\0')
{
i++;
}
if (i == inputSize || str[i] == '\0')
{
// string without any space in it so no need to rotate?
return 0;
}
const size_t inputLen = strnlen(str, inputSize);
if (i + 1 /* Include lazy space character */>= sizeof(temp)) {
// We dont have enough memory to handle this request
return -1;
}
// hidden assert(str[i] == ' ');
temp[0] = ' ';
memcpy(temp + 1, str, i);
memmove(str, str + i + 1, inputLen);
lastCall = strncat_s(str, inputSize, temp, i + 1);
if (!lastCall)
{
return lastCall;
}
return 0;
}
int main()
{
char str[MAX] = "Enter Your String To Switch The";
for (int i = 0; i < 7; i++) {
if (rotate_left_optimized(str, MAX)) {
perror("failed to rotate");
exit(EXIT_FAILURE);
}
printf("%s\n", str);
}
return 0;
}
I have program to remove the similar words from string but this program only removing at once word not a repeating words.
For example input:
sabunkerasmaskera kera
and should an output:
sabunmas
This my code:
#include <stdio.h>
#include <string.h>
void remove(char x[100], char y[100][100], char words[100]) {
int i = 0, j = 0, k = 0;
for (i = 0; x[i] != '\0'; i++) {
if (x[i] == ' ') {
y[k][j] = '\0';
k++;
j = 0;
} else {
y[k][j] = x[i];
j++;
}
}
y[k][j] = '\0';
j = 0;
for (i = 0; i < k + 1; i++) {
if (strcmp(y[i], kata) == 0) {
y[i][j] = '\0';
}
}
j = 0;
for (i = 0; i < k + 1; i++) {
if (y[i][j] == '\0')
continue;
else
printf("%s ", y[i]);
}
printf ("\n");
}
int main() {
char x[100], y[100][100], kata[100];
printf ("Enter word:\n");
gets(x);
printf("Enter word to remove:\n");
gets(words);
remove(x, y, words);
return 0;
}
My program output its:
sabunkerasmaskerara
and that should not be the case. Maybe I need your opinion to fixed this program and also I need help to make it better.
Your solution does not work because it uses strcmp to compare the string portions, which only works if the substring is at the end of the string, as this makes it null-terminated.
You should instead use strstr to locate the matches and use memmove to shift the string contents.
There are other issues in your code:
do not use gets()
y is unnecessary for this task.
words is not defined
Here is a modified version:
#include <stdio.h>
#include <string.h>
char *remove_all(char *str, const char *word) {
size_t len = strlen(word);
if (len != 0) {
char *p = str;
while ((p = strstr(p, word)) != NULL) {
memmove(p, p + len, strlen(p + len) + 1);
}
}
return str;
}
int main() {
char str[100], word[100];
printf ("Enter string:\n");
if (!fgets(str, sizeof str, stdin))
return 1;
printf("Enter word to remove:\n");
if (!fgets(word, sizeof word, stdin))
return 1;
word[strcspn(word, "\n")] = '\0'; // strip the trailing newline if any
remove_all(str, word);
fputs(str, stdout);
return 0;
}
I have problem with my alignement. This time I want my program to return words that ends and starts with the same letter. I've wrote something like this, but it seems to return random words.
#include <stdio.h>
#include <string.h>
void main()
{
char str[100];
int i, t, j, len;
printf("Enter a string : ");
scanf("%[^\n]s", str);
len = strlen(str);
str[len] = ' ';
for (t = 0, i = 0; i < strlen(str); i++)
{
if ((str[i] == ' ') && (str[i - 1] == str[0]))
{
for (j = t; j < i; j++)
printf("%c", str[j]);
t = i + 1;
printf("\n");
}
else
{
if (str[i] == ' ')
{
t = i + 1;
}
}
}
}
You can use strtok to split the strings from stdin, then apply a letter checker on each parsed word one at a time.
Something like this:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAXCHAR 100
int is_start_end(char *word);
void exit_if_null(void *ptr, const char *msg);
int
main(void) {
char str[MAXCHAR];
char *word;
char **all_words;
int words_size = 1, word_count = 0;
int i, found;
all_words = malloc(words_size * sizeof(*all_words));
exit_if_null(all_words, "initial Allocation");
printf("Enter words(enter empty line to terminate):\n");
while (fgets(str, MAXCHAR, stdin) != NULL && strlen(str) != 1) {
word = strtok(str, " \n");
while (word !=NULL) {
if (words_size == word_count) {
words_size *= 2;
all_words = realloc(all_words, words_size * sizeof(*all_words));
exit_if_null(all_words, "Reallocation");
}
all_words[word_count] = malloc(strlen(word)+1);
exit_if_null(all_words[word_count], "Initial Allocation");
strcpy(all_words[word_count], word);
word_count++;
word = strtok(NULL, " \n");
}
}
printf("Words that have equal first and last letters:\n");
found = 0;
for (i = 0; i < word_count; i++) {
if (is_start_end(all_words[i])) {
found = 1;
printf("%s\n", all_words[i]);
}
free(all_words[i]);
all_words[i] = NULL;
}
if (found == 0) {
printf("None Found\n");
}
free(all_words);
all_words = NULL;
return 0;
}
int
is_start_end(char *word) {
int len;
len = strlen(word);
if ((len == 1) || (tolower(word[0]) == tolower(word[len-1]))) {
return 1;
}
return 0;
}
void
exit_if_null(void *ptr, const char *msg) {
if (!ptr) {
printf("Unexpected null pointer: %s\n", msg);
exit(EXIT_FAILURE);
}
}
This line removes the null terminator of the string:
len = strlen(str);
str[len] = ' ';
thus the string no longer exists, what is left is just an ordinary array of characters.
The next call to strlen, in the body of the for loop, will cause undefined behavior.
I have an array of string from which i have to find duplicate string and then remove that duplicate string like i have string
char aa[50]="Amit Hanish Mahesh Amit"
Now Amit is duplicate and have to remove it from string .
#include "string.h"
main()
{
char x[100] = "Amit Hanish Mahesh Amit";
char y[3][100];
int i = 0, k = 0, j = 0, c = 0, end, t;
int current = 1;
while (x[i] != '\0') {
if (x[i] != ' ') {
y[k][j] = x[i];
j++;
i++;
} else {
// c = c + 1;
i++;
k++;
j = 0;
}
y[k][j] = '\0';
}
for (end = 1; end <= 3; end++) {
for (t = 0; t < end; t++) {
if (strcmp(y[end], y[t]) == 0) break;
}
if (end == t) {
strcpy(y[current],y[t]);
current++;
}
}
y[current] = 0;
printf("%s",y);
}
I have written a smalll routine for it .Does not seems to be worked .Any one have any suggestion where i am going wrong?
The other answers you got work fine for a small number strings (your example code only has 4). But, if you're comparing a large number this will be quite slow since you're doing n^2 comparisons. I'd suggest first splitting the string into an array of strings, then sorting the array using qsort(). In a sorted array all duplicates are guaranteed to be adjacent. This reduces the time from n^2 to n log n -- the time required to sort.
I would split the string array using strtok (see the man page).
So I would have something like this
char x[100]="Amit Hanish Mahesh Amit";
/* Preparing the result string */
size_t sz_result = sizeof(char) * (strlen(x) + 1);
char* result = (char*) malloc( sz_result );
result[0] = '\0';
/* Parsing the string from one element to the other */
char* elm = strtok(x, " ");
while( (elm = strtok(NULL, " ")) != NULL )
{
...
You will have each element of the string to verify if they are unique.
Then I would use something like a hashmap (you can use the one from the glib) or I would put the read string element in a new string only if it is not already in.
Here is an example for the second solution:
...
/* Is the element already in the result string? */
if ( strstr(result, elm) == NULL )
{
/* Then we should add it */
snprintf( result, sz_result - 1, "%s %s", result, elm );
}
}
In the end if you want x to be modified, you simply copy result in x:
strncpy( x, result, 99 );
Here is a sample code (not optimised, not using the strn* primitives, etc.)
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
char x[100]="Amit Hanish Mahesh Amit";
/* Preparing the result string */
size_t sz_result = sizeof(char) * (strlen(x) + 1);
char* result = (char*) malloc( sz_result );
result[0] = '\0';
/* Parsing the string from one element to the other */
char* elm = strtok(x, " ");
if (elm != NULL) strcpy(result, elm);
while( (elm = strtok(NULL, " ")) != NULL )
{
/* Is the element already in the result string? */
if ( strstr(result, elm) == NULL )
{
/* Then we should add it */
strcat( result, " " );
strcat( result, elm );
}
}
strcpy( x, result );
fprintf( stdout, "Result: %s\n", x );
}
To remove duplicates from an array without preserving the order of elements:
sort the array
copy unique elements to the beginning of the array
remove the tail with duplicate elements
int remove_duplicates(StringArray array) {
if (! (array and array->items)) return 0; // empty array or NULL
StringArray_sort(array); // sort
// unique_copy()
String result = array->items, last = array->items + array->size;
for (String first = array->items; first != last; ++result) {
String_copy(result, first); // copy first to result
for (String prev = first; ++first != last and String_cmp(prev, first) == 0;)
{ /* skip adjacent equal items */ }
}
// shrink
return StringArray_remove(array, result, last);
}
Example
int main() {
char text[] = "Mahesh Amit Hanish Amit";
StringArray array = split(text, sizeof(text));
StringArray_dump(array, "<"); // print array before removing duplicates
if (remove_duplicates(array) < 0)
perror("error remove_duplicates(), OS error if any");
StringArray_dump(array, ">"); // print it after
StringArray_destroy(array);
return 0;
}
Where split() is:
StringArray split(const char* text, size_t size) {
if (! (text and text[size-1] == '\0')) return NULL;
StringArray array = StringArray_create();
if (! array) return NULL;
size_t n = -1;
for (const char* p = text; p != text+size; p += n+1) {
n = strcspn(p, " \t\n"); // find index of the next whitespace
if (n == 0) continue; // skip consecutive whitespace
// append characters in range [p, p+n)
// as a string to the array
const String string = String_create(p, n);
if (StringArray_append(array, string) < 0) {
String_destroy(string);
StringArray_destroy(array);
return NULL;
}
String_destroy(string);
}
return array;
}
Output
Mahesh<Amit<Hanish<Amit<
Amit>Hanish>Mahesh>
Full source code
I'm pretty sure, that the following line is not intended (assignment, not comparison)
if (end = t) {
See what happens, if you code a == and come back, if you still have problems.
Hint: Always code blanks around operators, so expressions are easier to read.
It's always fun to try to solve this kind of simple problems in C as exercise. Here's my take.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char* strstrn(const char *haystack, const char *needle, size_t needle_len)
{
while((haystack = strchr(haystack, *needle)))
{
if (strncmp(haystack, needle, needle_len) == 0)
return (char *) haystack;
haystack++;
}
return NULL;
}
char* find_duplicate(const char* str, size_t len, size_t dup_len)
{
for(size_t i = 0; i < (len - dup_len); i++)
{
char* r = strstrn(str + i + 1, str + i, dup_len);
if(r) return r;
}
return NULL;
}
int main(int argc, char** argv)
{
if(argc < 3)
{
fprintf(stderr, "Usage: %s haystack dup_size\n", argv[0]);
return 1;
}
char* haystack = argv[1];
size_t len = atoi(argv[2]);
char* r;
while((r = find_duplicate(haystack, strlen(haystack), len)))
{
strcpy(r, r + len);
}
puts(haystack);
return 0;
}
/*
* C Program to Find the Frequency of Every Word in a
* given String
*/
#include <stdio.h>
#include <string.h>
void main()
{
int count = 0, c = 0, i, j = 0, k, space = 0;
char str[100], p[50][100], str1[20], ptr1[50][100];
printf("Enter the string\n");
scanf(" %[^\n]s", str);
printf("string length is %d\n", strlen(str));
for (i = 0;i<strlen(str);i++)
{
if ((str[i] == ' ')||(str[i] == ', ')||(str[i] == '.'))
{
space++;
}
}
for (i = 0, j = 0, k = 0;j < strlen(str);j++)
{
if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))
{
p[i][k] = '\0';
i++;
k = 0;
}
else
p[i][k++] = str[j];
}
k = 0;
for (i = 0;i <= space;i++)
{
for (j = 0;j <= space;j++)
{
if (i == j)
{
strcpy(ptr1[k], p[i]);
k++;
count++;
break;
}
else
{
if (strcmp(ptr1[j], p[i]) != 0)
continue;
else
break;
}
}
}
for (i = 0;i < count;i++)
{
for (j = 0;j <= space;j++)
{
if (strcmp(ptr1[i], p[j]) == 0)
c++;
}
printf("%s -> %d times\n", ptr1[i], c);
c = 0;
}
}