I'm currently having trouble with the output of the code. It fills the array correctly but there is something wrong with my removal of repeated words.
To fill the array word by word:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(void) {
char str[610] = "C (pronounced like the letter C) is a general-purpose computer programming language developed between 1969 and 1973 by Dennis Ritchie at the Bell Telephone Laboratories for use with the Unix operating system. Although Cwas designed for implementing system software, it is also widely used for developing portable application software. C is one of the most widely used programming languages of all time and there are very few computer architectures for which a C compiler does not exist. C has greatly influenced many other popular programming languages, most notably C++, which began as an extension to C.";
char tokChars[9]=" ;().,\n";
char *cptr;
int size = 100;
char array[1000][20];
char ob[1000][20];
int count;
int i= 0;
cptr = strtok(str, tokChars);
count = 1;
while(cptr != NULL)
{
strcpy(array[i], cptr);
printf("\n%s\n",array[i]);
printf("token %d --> %s \n",count , cptr);
cptr = strtok(NULL, tokChars);
count++;
i++;
}
To remove the repeating words:
int k = 0, r = 0,h = 0;
for(r= 0 ; r<100 ; r++)
{
while ( k< 100)
{
int a;
a = strcmp(array[r], ob[k]);
if (a != 0)
{
strcpy(ob[h],array[r]);
h++;
break;
}
k++;
}
}
int m = 0;
for(m= 0; m<size; m ++)
{
printf("\n%s\n",ob[m]);
}
return EXIT_SUCCESS;
}
Apparently the output printed out every word in the array. What should i change? or something i misunderstood
The following should work:
int k, r, h;
strcpy(ob[0],array[0]); h= 1;
for(r= 0 ; r<100 ; r++)
{
k= 0;
while (k< h)
{
if (strcmp(array[r], ob[k]) == 0)
break;
k++;
}
if (k==h) {
strcpy(ob[h],array[r]);
h++;
}
}
The main problem is that you compare the next element of array
with only one element of ob
(output array), where it is not a duplicate if none of ob
compares equal. Then you can add this element to ob
and continue.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.