试图比较两个字符串

时间:2011-12-29 08:59:03

标签: c

说我是C的新手就足够了所以请表现出一些怜悯之情;)。

我正在尝试比较两个字符串。输出不应包含常见字符。可悲的是,确实如此。

以下是代码:

#include <stdio.h>
#include <stdlib.h>

int main(void) 
{
    const char msg[15] = "blueberry";
    int c;
    int s[15];
    int j = 0;
    int i = 0;
    int k= 0;
    int ok = 0;
    int t = 0;

    while (i < 15 && (c = getchar()) != '\n')
    {
        s[i] = c;
        ++i;
    }

    for (t=j=0; t < 15; ++t)
    {
        ok = 1;
        //printf ("%c", s[t]);
    }

    for (k=0; msg[k] != '\0'; ++k)
    {
        if (s[t] == msg[k]) 
        {
            ok = 0;
        }
    }

    if (ok == 1)
    {
        s[j] = s[t];
        j++;
    }
    s[j] = '\0';

    for (j = 0; j < 15; ++j)
        printf ("%c ", s[j]);
}

键盘的输入是blackberry,预期输出应该是U,但遗憾的是它不是。请帮忙。另外,为什么它进入嵌套的for循环而不考虑条件?


我非常感谢大家,这对我帮助很大。我想出了一个方法&amp;我的输出没问题。我从A4L借了一些想法:)。

6 个答案:

答案 0 :(得分:1)

要比较两个字符串,您可以使用strcmp()

以下是一个字符串比较程序,您可以将其用作参考。我有数组和指针版本,以便更好地理解。

#include <stdio.h>

int strcmp1(char a[], char b[])
{
        int i=0;
        while (a[i] == b[i]) {
                if (a[i] == '\0')
                        return 0;
                i++;
        }

        return a[i]-b[i];
}

int strcmp2(char *a, char *b)
{
        while (*a == *b) {
                if (*a == '\0')
                        return 0;
                a++; b++;
        }
        return *a-*b;
}

int main()
{
        char s1[] = "test string1";
        char s2[] = "test string";
        char s3[] = "aaa";
        char s4[] = "bbb";

        printf("strcmp1(%s, %s) = %d \n", s1, s2, strcmp1(s1, s2));
        printf("strcmp2(%s, %s) = %d \n", s3, s4, strcmp2(s3, s4));

        return 0;
}

答案 1 :(得分:0)

鉴于msg包含“blueberry”而s包含“blackberry”,这应该这样做

for (int i=0; i < strlen(msg); i++) {
  for (int j = 0; j < strlen(s); j++) {
    if (msg[i] != s[j]) {
       printf ("%c", msg[i]);
    }
  }
}
是的,它是丑陋的(使用for中的strlen给了我一些寒意,但我今天仍然低于咖啡因^^)

答案 2 :(得分:0)

我想你想找到输入与消息

不同的第一个字母

这是您自己的代码,包含一些修复

#include <stdio.h>
#include <stdlib.h>

int main(void) {
const char msg[15] = "blueberry";
int c;
char s[15];
int i = 0;
int k= 0;
int ok = 0;


while (i < 15 && (c = getchar()) != '\n')
{
    s[i] = (char) c;
    ++i;
}

// make sure to terminate the string after hitting enter
s[i] = '\0';

printf("input: %s\n", s);
printf("messg: %s\n", msg);

// run through both input and message with one counter
for (k=0; ok == 0 && msg[k] != '\0' && s[k] != '\0'; )
{
    // if different chars stop
    if (s[k] != msg[k]){
        ok = 1;
    } else {
        // next char
        k++;
    }
}
if (ok == 1)
{
    printf ("diff @ index %d -> %c\n", k, msg[k]);
}
else
{
    printf ("no diff\n");
}

return 0;
}

答案 3 :(得分:0)

#include <stdio.h>
#include <string.h>

//Length to match
int comm(char* s1, char* s2){
    int len = 0;
    while(*s1 && *s2 && *s1++ == *s2++)
        ++len;
    return len;
}
//commdiffcomm
/*
int commr(char* s1, char* s2){
    int len = 0, limit;
    int len1,len2;
    len1 = strlen(s1);
    len2 = strlen(s2);
    limit = len1 > len2 ? len2 : len1;
    s1 = s1 + len1;
    s2 = s2 + len2;
    while(limit-- && *--s1 == *--s2)
        ++len;
    return len;
}
//bad
int diff(char* s1, char* s2, int* len1, int* len2){
    int len, lenr, s1_len, s2_len, wk_max, i, j;

    len = comm(s1, s2);
    if(strcmp(s1, s2)==0){
        *len1 = *len2 = 0;
        return len;
    }
    lenr = commr(s1, s2);
    *len1 = strlen(s1) - len - lenr;
    *len2 = strlen(s2) - len - lenr;
    return len;
}
*/
int diff(char* s1, char* s2, int* len1, int* len2){
    int len, s1_len, s2_len, wk_max, i, j;

    len = comm(s1, s2);
    if(strcmp(s1, s2)==0){
        *len1 = *len2 = 0;
        return len;
    }
    s1_len = strlen(s1 + len + 1);
    s2_len = strlen(s2 + len);
    wk_max = 0;
    for(i = 1; i < s1_len ; i++){
        for(j = 0; j < s2_len; j++){
            int com_len;
            com_len = comm(s1 + len + i, s2 + len + j);
            if(wk_max < com_len){
                wk_max = com_len;
                *len1 = i;
                *len2 = j;
            }
        }
    }
    return len;
}

int main(){
    char str1[16] = "blueberry";
    char str2[16] = "blackberry";
    char dif1[16] = "";
    char dif2[16] = "";
    int len0;//length of top to diff pos
    int len1;
    int len2;

    len0 = diff(str1, str2, &len1, &len2);
    strncpy(dif1, str1 + len0, len1);
    strncpy(dif2, str2 + len0, len2);
    if(len1 !=0 && len2 != 0){
        printf("%s different %s at position %d length %d (\"%s\")\n", str1, str2, len0, len1, dif1);
        printf("%s different %s at position %d length %d (\"%s\")\n", str2, str1, len0, len2, dif2);
    } else {
        printf("two string is same.");
    }
    return 0;
}

/*
blueberry different blackberry at position 2 length 2 ("ue")
blackberry different blueberry at position 2 length 3 ("ack")
*/

答案 4 :(得分:0)

代码存在一些问题:

  • 您没有空终止输入字符串。试图将它与c string functons一起使用会带来麻烦。要解决此问题,请更改

    while (i < 15 && (c = getchar()) != '\n')
    {
        s[i] = c;
        ++i;
    }
    

    while (i < 14 && (c = getchar()) != '\n')
    {
        s[i] = c;
        ++i;
    }
    s[i] = '\0';
    
  • 您的规范不清楚您是否希望自己的程序打印msgsmsg所特有的字母。 (即,你想要msg-s(msg ∪ s)-(msg ∩ s)假设第一个,你的程序的重要部分是这样的:

    k=0;
    for(i=0;i<strlen(msg);i++){
        int exists = 0;
        for(j=0;!exists && j<strlen(s);j++){
            if(msg[j] == s[i])
                exists = 1;
        }
        if(!exists)
            msg[k++] = msg[i];
    }
    s[k] = '\0';
    

    内部循环检查s是否包含msg中的当前字符。如果确实如此,我们不会做任何事情,但如果没有,我们会将其附加到我们已经处理的msg位之上创建的子列表的末尾。

答案 5 :(得分:-1)

即使在重写之后你的代码仍然很乱 - 有太多错误需要详细描述

/*
blackbery
b l u e b e r r y
. . a c k b e . .
result = non-equal
*/
#include <stdio.h>
#include <stdlib.h>

int main(void) {
const char msg[15] = "blueberry";
int c, s[15], i,j,k, ok;

for (i=0; i < 15; i++) s[i] = 0;
for (i=0; i < 15 && (c = getchar()) != '\n'; i++) s[i] = c;

for (ok=1, k=0; msg[k] != '\0'; ++k)
    if (s[k] != msg[k]) ok = 0; else s[k] = '.';

for (j = 0; j < 15; ++j) printf ("%c ", msg[j]);
printf("\n");

for (j = 0; j < 15; ++j) printf ("%c ", s[j]);
printf("\nresult = %s\n", ok ? "equal" : "non-equal");
}