检查两个字符串是否是C中的置换
我实际上在尝试制作一些程序,可以检查两个字符串是否相互排列。我解释:检查两个字符串是否是C中的置换,c,string,C,String,我实际上在尝试制作一些程序,可以检查两个字符串是否相互排列。我解释: 如果我考虑: Eagle 及 (我在上一个问题中使用了这两个例子) 我得到一个置换,置换参数是3。为什么?因为在字母表中:E+3=H,a+3=d等等 我使用了无符号字符,因为如果我在一个字符串中得到一个z,我希望它(例如)z+3=c 我开始做的是: #include <stdio.h> #define N 20 int my_strlen(unsigned char *string){ int len
如果我考虑:
Eagle
及
(我在上一个问题中使用了这两个例子)
我得到一个置换,置换参数是3。为什么?因为在字母表中:E+3=H,a+3=d等等
我使用了无符号字符,因为如果我在一个字符串中得到一个z,我希望它(例如)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;
}
#包括
#定义n20
int my_strlen(无符号字符*字符串){
整数长度;
对于(长度=0;*string!='\0';string++){
长度++;
}
返回(长度);
}
int main()
{
无符号字符string1[N],string2[N];
int测试=0,i=0,长度1,长度2;
scanf(“%s”,string1);
scanf(“%s”,string2);
长度1=我的斯特伦(string1);
长度2=我的斯特伦(string2);
如果(长度1==长度2){
对于(i=0;i这是因为test
变量会针对字符串中的每个字符进行更新
对于字符串Hello和Hello,或Hello和Helqo,最后一个字符是相同的('o'
),因此在循环结束时,test
更新为1
尝试使用Hello和Hellm,您将得到test=0
您只打印最后一个字符比较的结果
for(i=0; i<length1; i++){
if(string1[i]==string2[i]){
test=1;
}
else{
test=0;
}
}
for(i=0;i您可以考虑这样的问题
字符串中每个字符的字符距离必须相等。
因此,您可以检查第一个字符的距离,然后在其他字符上循环
并验证距离是否相同。只要它不等于第一个字符的距离,您就可以安全地得出结论,它不是排列
#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;
}
#包括
#包括
内部主(空)
{
int i;
无符号字符string1[]=“test”;
无符号字符string2[]=“vguv”;
int-slength1=4;
int-slength2=4;
整数距离;
int是_置换=1;
如果(SLENGHT1!=SLENGHT2){
is_置换=0;
}
距离=(int)string2[0]-(int)string1[0];
对于(i=1;i#包括
#包括
#包括
int main()
{
字符a[100],b[100];
int size_a,size_b,i,j,flag;
scanf(“%s”、&a);
scanf(“%s”、&b);
尺寸a=strlen(a);
尺寸b=strlen(b);
if(size\u a!=size\u b)//如果两个字符串的大小不相等,则打印并退出
{
printf(“否”);
出口(0);
}
对于(i=0;i
{
if(sl1!=sl2)
{
返回false;
}
std::set字符集;
对于(整数索引=0;索引<(sl1+sl2);索引++)
{
int source=(索引)/sl1;
如果(源==0)
{
插入字符集(s1[索引]);
}
其他的
{
插入字符集(s2[源代码-1]);
}
}
返回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;
}
#包括
#定义可能的字符256
int是_置换(char*str1,char*str2)
{
int count1[possiblechars]={0};
int count2[possiblechars]={0};
int i;
//将计数数组“0”设置256次
对于(i=0;str1[i]&&str2[i];i++)
{
count1[str1[i]++;
count2[str2[i]]++;
}
//交互,直到字符串1或2中的任何一个命中null
//递增计数数组的各个索引
if(str1[i]| | str2[i]){
返回0;
}
//如果长度不同,则返回false
对于(i=0;i
#包括
#包括
#包括
#定义n20
int my_strlen(无符号字符*字符串)
{
整数长度;
对于(长度=0;*string!='\0';string++)
{
长度++;
}
返回(长度);
}
int main()
{
无符号字符string1[N],string2[N];
整数置换=0,i=0,d=0,长度1,长度2;
scanf(“%s”,string1);
scanf(“%s”,string2);
长度1=我的斯特伦(string1);
长度2=我的斯特伦(string2);
d=string1[0]-string2[0];//找出距离
如果(长度1==长度2)
{
对于(i=0;iString1
{
如果(string2[i]==string1[i]+d*(-1))//当d进入此处时,d将始终为负
{
排列=1;
}
其他的
{
排列=0;
打破
}
}
else//else String1>String2
{
if(string1[i]==string2[i]+d)
{
排列=1;
}
其他的
{
排列=0;
打破
}
}
}
printf(“排列=%d\n和距离=%d”,排列,d);
}
其他的
{
printf(“错误”);
}
返回0;
}
数组如何整数字母[26]
然后对每个字母进行计数,然后比较数组……这是一个非常简单的实现。同一字母的小写字母和大写字母之间的距离是多少?您正在覆盖循环中的变量test
,因此只对最后一个字符进行比较计数。只需中断f
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;
}
}
#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;
}
#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;
}