2014-04-22 42 views
0

所以这里就是这样:我需要从用户string1和string2中获取两个字符串,然后删除string1中也存在于string2中的“words”并打印字符串1。两个字符串输入 - 从第一个字符中删除常用单词并将其打印出来

我可以令牌化他们,但接着我的想法/知识,需要帮助:)

int main() 
{ 
    char string1[100];     //declaration of array for 1st input 
    const char *tokens1[100];   //declaration of array of pointers 
    char *token_ptr1;     //pointer var to store tokens 

    char string2[100];     //declaration of array for 1st input 
    const char *tokens2[100];   //declaration of array of pointers 
    char *token_ptr2;     //pointer var to store tokens 

    int i=0; 


    //input from user: 
    printf("Enter string1: "); 
    gets(string1); 
    printf("\n"); 
    printf("Enter string2: "); 
    gets(string2); 

    printf("\n"); 

    //using strtok function to tokenize string1 

    token_ptr1=strtok(string1," "); 

    printf("Tokens1: \n");  //loop to store tokens in array of pointers and printing tokens 

    while (string1!='\0') 
    { 
     if (token_ptr1=='\0') { break; } 
     printf("%s\n",token_ptr1); 

     tokens1[i]=token_ptr1; 

     token_ptr1=strtok(NULL, " "); 

     i++; 
    } 

    //string1 is tokenized and stored in tokens1 


} 

我已经试过各种方法去除常用词无济于事

编辑在此之后:

例INPUT1:同样的问题中的另一个问题,这是一个string234

- 示例INPUT2:234

- 输出示例:这是一个使用令牌化方法的字符串“string234”保存指针数组中,现在该怎么处理这个程序中的字符串

?使用简单的数组检查单个字符?

回答

0

天真的方法是标记两个字符串,并通过string1中的每个标记进行循环,并与string2进行比较。如果令牌匹配,则通过标记来移除令牌1。

标记第二个字符串并创建一个数组来标记string1中的每个标记。 如果找到匹配项,则标记token1索引,最后只打印未标记的标记。 我可以用一个匹配功能是这样的:

int match(int ind1, int ind2){ 
    while(string1[ind1]!='0'){ 
     if (string2[ind2]==string1[ind1]){ 
      ind2++; 
      ind1++; 
     }else{ 
      break; 
     } 
    } 
    // Check if string2 also ended at same position 
    if (string1[ind1]==string2[ind2]) return 1; 
    return 0; 
} 

没有测试过,但我认为这是正确的。 希望它有帮助!

0

我在考虑你不会改变i,因为它包含了令牌的数量。

strstr(str1,str2),返回指向str2中指定的整个字符序列的str1中第一个出现的指针,或者如果该序列不存在于str1中,则返回空指针。

char * ptr; 
for (j = 0 ; j < i ; j++){ 
    ptr = strstr (string2,tokens1[j]); 
    if(ptr){ 
     token1[j] = '\0'; 
    } 
} 

for (j = 0 ; j < i ; j++){ 
    if(strlen(token1[j]) > 0){ 
     puts(token1[j]); 
    } 
} 

希望这有助于.. :)

注:我没编译它,只是写的代码在这里。而且我在过去的一年半时间里不再使用C编码。在编程竞赛中使用了很多。所以,请指出任何错误,并让我知道:)

0

首先,如果它编译完成,你会有一个很好的无限循环。你将一个常量字符指针string1与一个无意义和错误的字符进行比较。

while (string1 !≃ '0') 

应该

while (token_ptr1 != NULL) 

然后你应该跳过下一行也是如此。

您须为每个字符串的最后一个字,接着一个换行符,而不是一个空间准备,如果你不想错过这些:

strtok(string1, " \n"); 
strtok(NULL, " \n"); 

拥有一个单独的临时tokenx_ptr两个环路是多余的,你可以在两个循环中使用一个。

接下来,您需要跟踪您在每个字符串中找到了多少个单词。您可以将两个循环后的i存储到n1n2,或将最后一个失败的strtok中的NULL值存储到数组中。我打算采取第二种方法。

然后,这是一个丑陋的O(n2)双循环来打印tokens1数组中不存在于tokens2数组中的令牌。

int i1 = 0; 
while (tokens1[i1] != NULL) { 
    int i2 = 0; 
    int found = 0; 
    while (tokens[i2] != NULL) { 
     if (strncmp(tokens1[i1], tokens2[i2], 100) == 0) { 
      found = 1; 
      break; 
     } 
     i2++; 
    } 
    if (!found) 
     printf("%s ", tokens1[i1); 
    i1++; 
} 
+0

您好,我是新来编程,并有可能做了一些错误,但我想你指认他们 - 我喜欢学习。 我在这里发布的代码很好地给出了所需的标记化输出 使用string1!='\ 0'作为终止语句的原因是程序崩溃时没有此语句。 我会尽力回复你的回复,并回复。 谢谢 – user3559057

0
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h> 
#include <ctype.h> 

int cmp(const void *a, const void *b){ 
    return strcmp(*(const char**)a, *(const char**)b); 
} 

int main(){ 
    char string1[100]; 
    char *token_ptr1; 
    char string2[100]; 
    const char *tokens2[100]; 
    char *token_ptr2; 

    char result[100]={0}; 
    char *wordp, word[100]; 
    const char *delimiter = " \t\n"; 
    int i, len, ndw;//Number of delete words 

    printf("Enter string1: "); 
    fgets(string1, sizeof(string1), stdin);//include newline 
    //printf("\n"); 
    printf("\nEnter string2: "); 
    fgets(string2, sizeof(string2), stdin); 

    for(ndw=0, token_ptr2=strtok(string2, delimiter);token_ptr2;token_ptr2=strtok(NULL, delimiter)){ 
     tokens2[ndw++] = token_ptr2; 
    } 
    qsort(tokens2, ndw, sizeof(*tokens2), cmp); 

    token_ptr1 = string1; 
    token_ptr2 = result; 
    while(*token_ptr1 != '\0'){ 
     len = strspn(token_ptr1, delimiter); 
     strncpy(token_ptr2, token_ptr1, len);//delimiter remain. 
     token_ptr1 += len; 
     token_ptr2 += len; 
     len = strcspn(token_ptr1, delimiter); 
     strncpy(word, token_ptr1, len); 
     word[len] = '\0'; 
     wordp = word; 
     if(NULL == bsearch(&wordp, tokens2, ndw, sizeof(*tokens2), cmp)){ 
      strncpy(token_ptr2, token_ptr1, len); 
      token_ptr2 +=len; 
     } 
     token_ptr1 += len; 
    } 
    *token_ptr2 = '\0'; 
    printf("%s", result); 

    return 0; 
} 
相关问题