2011-12-29 41 views
0

這足以說我是新的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不論條件?


我非常感謝大家,它幫了我很多。我已經想通了的方式&上午確定與輸出。我借用了A4L的一些想法:)。

+0

你忘了告訴我們究竟是代碼試圖做什麼,而恰恰是它做了什麼。我可以想到很多輸出'U'的代碼。而且我也可以考慮很多不輸出'U'的代碼。 –

+0

有一個}關閉錯位。 if(ok == 1)應該在for循環中。 – BigMike

+0

您在s []中填寫用戶的輸入,但您檢查了msg []。爲什麼? –

回答

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; 
} 
+0

是的,那麼輸出不是在兩個字符串中的字符怎麼樣? – BigMike

+0

@BigMike我只是想給作者一個參考/提示,嘗試和學習字符串操作C –

+0

非常感謝大家。這對我幫助很大。再次感謝! – user1115145

-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"); 
} 
+0

那麼你提出的實際答案是什麼? – JeremyP

+0

清理海報預期的原始代碼。從我的角度來看,「levenstein距離」比較兩個字符串的目標更合適。 (我的意思是原始帖子的作者想要顯示字符串之間的區別,恕我直言)。 – Vlad

+1

是的,我同意他做了,但你所做的只是發佈了一些代碼,沒有任何解釋。 – JeremyP

0

因爲味精中含有「藍莓」和s包含「黑莓」這個應該這樣做

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]); 
    } 
    } 
} 

是它的醜陋(使用了strlen的給我發冷,但今天我對咖啡因仍然較低^^)

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; 
} 
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") 
*/ 
0

沒有與代碼的幾個問題是:

  • 您不會null-terminate您的輸入字符串。試圖與c字符串函數一起使用會帶來麻煩。爲了解決這個問題,改變

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

    while (i < 14 && (c = getchar()) != '\n') 
    { 
        s[i] = c; 
        ++i; 
    } 
    s[i] = '\0'; 
    
  • 你的規格還不清楚,你是否希望你的程序打印獨特的字母msg,或同時smsg。 (即,你想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位的頂部創建的子列表的末尾。