簡體   English   中英

檢查C中是否有兩個字符串排列

[英]Check if two strings are permutations in C

我實際上試圖讓一些程序可以檢查兩個字符串是否相互排列。 我解釋 :

如果我考慮:

Eagle

Hdjoh

(我在上一個問題中使用了這兩個例子)。

我得到一個排列,排列參數是3.為什么? 因為在字母表中:E + 3 = H,a + 3 = d等。

我使用unsigned char,因為如果我在我的一個字符串中得到az,我想要(例如)z + 3 = c。


我開始做什么:

#include <stdio.h>
#define N 20

int my_strlen(unsigned char *string){
    int length;
    for (length = 0; *string != '\0'; string++){
        length++;
    }
    return(length);
}

int main()
{
    unsigned char string1[N], string2[N];
    int test=0, i=0, length1, length2;
    scanf("%s", string1);
    scanf("%s", string2);

    length1=my_strlen(string1);
    length2=my_strlen(string2);

    if(length1==length2){
        for(i=0; i<length1; i++){
            if(string1[i]==string2[i]){
                test=1;
                }
                else{
                    test=0;
                }
        }
        printf("Test = %d", test);
    }
    else{
        printf("Error");
    }

    return 0;
}

我剛開始考慮它..所以目前我只是試着逐個字母地比較兩個字符串。

問題在於 :如果我嘗試比較Hellohello ,或者HelloHelqo,我得到Test = 1。

所以有人可以告訴我這里有什么不對嗎?

非常感謝。


編輯1:

#include <stdio.h>
#define N 20

int my_strlen(unsigned char *string){
    int length;
    for (length = 0; *string != '\0'; string++){
        length++;
    }
    return(length);
}

int main()
{
    unsigned char string1[N], string2[N];
    int test=0, i=0, length1, length2;
    scanf("%s", string1);
    scanf("%s", string2);

    length1=my_strlen(string1);
    length2=my_strlen(string2);

    if(length1==length2){
        for(i=0; i<length1; i++){
            if(string1[i]==string2[i]){
                test=1;
                }
                else{
                    test=0;
                    break;
                }
        }
        printf("Test = %d", test);
    }
    else{
        printf("Error");
    }

    return 0;
}

現在這是正確的。 我會繼續。


編輯2 - 6.7.14:

我實際上是工作和該計划的“第二部分”。 我正在尋找d,我驗證它是否是一個排列。 沒那么容易,所以我需要一些建議,我是否必須編寫其他功能來做到這一點? 或者只是處理我的代碼的這一部分:

if(length1==length2){
            for(i=0; i<length1; i++){
                if(string1[i]==string2[i]){
                    test=1;
                    }
                    else{
                        test=0;
                        break;
                    }
            }
            printf("Test = %d", test);
        }
        else{
            printf("Error");
        }

        return 0;
    }

我暫時寫了這樣的話:

if(length1==length2){
        for(i=0; i<length1; i++){
                for(d=0; d<255; d++){
                    if(string1[i]==string2[i] + d){
                        permutation=1;
                }
                else{
                    permutation=0;
                    break;
                }
                }
        }
        printf("\nPermutation = %d \nd = %d", permutation, d);
    }
    else{
        printf("Not a permutation");
    }

    return 0;
}

(我知道它不起作用,但我只是試過......)。

謝謝你的幫助。

您只打印出最后一個字符比較的結果。

for(i=0; i<length1; i++){
            if(string1[i]==string2[i]){
                test=1;
                }
                else{
                    test=0;
                }
}

這通過並比較每個字符並每次更改測試。 在循環結束時,僅輸出最后一個字符比較。

這是因為您的test變量會針對字符串中的每個字符進行更新。

對於字符串Hellohello ,或者HelloHelqo ,最后一個字符是相同的( 'o' ),因此在循環結束時, test更新為1

試試HelloHellm ,你會得到test = 0

你可以考慮這樣的問題。 要使兩個字符串成為有效的排列,字符串中的每個字符的字符距離必須相等。 因此,您可以檢查第一個字符距離,然后循環其他字符並驗證距離是否相同。 一旦它不等於第一個字符距離 ,你就可以得出結論,它不是一個排列。

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

int main(void)
{
    int i;
    unsigned char string1[] = "test";
    unsigned char string2[] = "vguv";

    int slength1 = 4;
    int slength2 = 4;

    int distance;
    int is_permutation = 1;

    if (slength1 != slength2) {
        is_permutation = 0;
    }

    distance = (int)string2[0] - (int)string1[0];

    for (i=1; i<slength1; ++i) {
        if ( ((int)string2[i] - (int)string1[i]) != distance ) {
            is_permutation = 0;
            break;
        }
    }

    if (is_permutation) {
        printf("%s is a permutation of %s with distance %d\n", string1, string2, distance);
    } else {
        printf("%s is not a permutation of %s\n", string1, string2);
    }

    return EXIT_SUCCESS;
}

請注意,我使用了靜態定義的字符串和stringlengths。 您在用戶輸入中閱讀的原始方式很容易出現未定義的行為。 聲明一個固定長度的字符串(OP中為20),因此如果用戶輸入的字符串長度超過19,則scanf將超出邊界並調用未定義的行為。 這非常糟糕,你應該閱讀它。

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
    char a[100], b[100];
    int size_a, size_b, i, j, flag;
    scanf("%s", &a);
    scanf("%s", &b);
    size_a = strlen(a);
    size_b = strlen(b);
    if(size_a!=size_b)                   //if size of both strings are      unequal then print and exit
    {
        printf("no");
        exit(0);
    }
    for(i=0;i<size_a;i++)               //compare every element of a with every element of b
    {
        for(j=0; j<size_b;j++)
    {
        if(a[i]==b[j])
        {
            b[j]=32;                //when elements matches add character 'space' there, of ASCII value 32
            flag++;                 //flag guards of every successful match
        }
    }
}
if(flag==size_a&&flag==size_b)
    printf("yes");
else
    printf("no");
return 0;

}

我寫了這段代碼,對我來說很好

bool IsPremutation(char* s1, char* s2, int sl1, int sl2)

{

if (sl1 != sl2)
{
    return false;
}

std::set<char> charset;

for (int index = 0;  index < (sl1 + sl2); index++)
{
    int source = (index) / sl1;

    if (source == 0)
{
    charset.insert(s1[index]);
    }
    else
    {
    charset.insert(s2[source - 1]);
    }
}

return charset.size() == sl2;

}

我知道這是舊的,但答案的質量令人不安。 首先,你並不是在尋找一種排列; 你正在通過移位密碼檢查兩個字符串是否不同。

接受的答案的更清晰版本,處理您提到的案例轉換:

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


int shifted(const char* a, const char* b, int* amount) {
    size_t len_a = strlen(a);
    size_t len_b = strlen(b);
    if (len_a != len_b) return 0;

    int shift_amount = *b - *a;
    for (; *a; a++, b++)
        if (tolower(*b) - tolower(*a) != shift_amount) return 0;

    *amount = shift_amount;
    return 1;
}

int main(void) {
    const char *a = "shift";
    const char *b = "tigU";

    int shift_amount = 0;
    if (shifted(a, b, &shift_amount))
        printf("Shifted by amount: %d\n", shift_amount);
    else
        printf("Not shifted\n");

    a = "shift";
    b = "shifT";
    if (shifted(a, b, &shift_amount))
        printf("Shifted by amount: %d\n", shift_amount);
    else
        printf("Not shifted\n");

    a = "shift";
    b = "shifv";
    if (shifted(a, b, &shift_amount))
        printf("Shifted by amount: %d\n", shift_amount);
    else
        printf("Not shifted\n");

    return EXIT_SUCCESS;
}

輸出:

Shifted by amount: 1
Shifted by amount: 0
Not shifted
#include <stdio.h>
#define possiblechars 256

int is_permutation(char *str1, char *str2)
{
    int count1[possiblechars] = {0};
    int count2[possiblechars] = {0};
    int i;

    //sets count arrays '0' for 256 times
    for (i = 0; str1[i] && str2[i]; i++)
    {
        count1[str1[i]]++;
        count2[str2[i]]++;
    }

    //interates until either string 1 or 2 hits null
    //increments individual indexes of the count arrays
    if (str1[i] || str2[i]){
        return 0;
    }

    //if different lengths, then return false
    for (i = 0; i < possiblechars; i++){
        if (count1[i] != count2[i]){
            return 0;
        }
    }
    return 1;
}

int main(int argc, char *argv[])
{
    char str1[] = "Eagle";
    char str2[] = "Hdjoh";
    if (is_permutation(str1, str2)){
      printf("Permutation\n");
    }
    else {
      printf("Not a permutation\n");
    }
    return 0;
}
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define N 20

int my_strlen(unsigned char *string)
{
    int length;

    for (length = 0; *string != '\0'; string++)
    {
        length++;
    }
    return (length);
}

int main()
{
    unsigned char string1[N], string2[N];
    int permutations = 0, i = 0, d = 0, length1, length2;
    scanf("%s", string1);
    scanf("%s", string2);
    length1 = my_strlen(string1);
    length2 = my_strlen(string2);
    d = string1[0] - string2[0];   //finding out the distance
    if (length1 == length2)
    {
        for (i = 0; i < length1; i++)
        {
            if (d < 0)           //if less then 0 then String2 >String1
            {
                if (string2[i] == string1[i] + d * (-1)) //when d enters here d will be always negative
                {
                    permutations=1;
                }
                else
                {
                    permutations = 0;
                    break;
                }
            }
            else           //else String1 >String2
            {
                if (string1[i] == string2[i] + d)
                {
                    permutations = 1;
                }
                else
                {
                    permutations = 0;
                    break;
                }
            }
        }
        printf("Permutations=%d\n and distamce=%d", permutations, d);
    }
    else
    {
        printf("Error");
    }
    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM