[英]C program doesn't work correctly
#include <stdio.h>
#include <string.h>
void main()
{
char alfavita[30] =
{
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
's',
't',
'u',
'v',
'w',
'x',
'y',
'z'
};
char str[20];
printf("Give a word:\n");
gets(str);
for(int i=0;i<strlen(str);i++)
{
for(int j=0;j<strlen(alfavita);j++)
if(alfavita[j] == str[i])
str[i] = alfavita[j+3];
}
puts(str);
}
For example if i give 'a' it should be return 'd' (each letter will transform into the 3d next of the alfavita array ) But it just prints me a null string. 例如,如果我给“ a”,则应返回“ d”(每个字母将转换为alfavita数组的3d),但是它只是为我输出一个空字符串。 I can't find something wrong or I don't see it .
我找不到错误或看不到它。
str[i] = alfavita[j+3];
After this line the code continues, so it will put i+3, i+6, ... until it gets out of alfavita. 在此行之后,代码将继续,因此它将放置i + 3,i + 6,...,直到退出alfavita。
You can add a break
to exit the inner loop like that: 您可以添加一个
break
以退出内部循环,如下所示:
for(int i=0;i<strlen(str);i++)
{
for(int j=0;j<strlen(alfavita);j++)
if(alfavita[j] == str[i])
{
str[i] = alfavita[j+3];
break; // next i.
}
}
, or maybe just directly access the array: ,或者直接访问数组:
for(int i=0;i<strlen(str);i++)
{
char c = str[i];
if (c >= 'a' && c <= 'z') {
str[i] = alfavita[(c - 'a' + 3) % strlen(alfavita)];
}
}
Note the % strlen(alfavita)
to avoid ending after the end of the list. 请注意
% strlen(alfavita)
以避免在列表末尾结束。 You could also write it: 您也可以编写它:
if (c >= 'a' && c <= 'z') {
str[i] = ((c - 'a' + 3) % 26) + 'a';
}
You can use a table that gives the replacement character for each character. 您可以使用为每个字符提供替换字符的表。
Then encode by computing the index into plain
, and transferring that index into encoded
: 然后通过将索引计算为
plain
进行编码,并将该索引转换为已encoded
:
char encode_char(char c)
{
const char *plain = "abcdefghijklmnopqrstuvwxyz";
const char *encoded = "defghijklmnopqrstuvwxyzabc";
const char *pp = strchr(plain, c);
if(pp != NULL)
return encoded[(ptrdiff_t) (pp - plain)];
return '?';
}
How the above works: 以上工作原理:
plain[0]
is encoded into encoded[0]
. plain[0]
被编码为encoded[0]
。 This can be more clearly modelled (ie by a struct
that has the pair) but then the iniialization becomes more complicated. struct
),但是初始化会变得更加复杂。 c
inside the plain
string. plain
字符串中搜索输入字符c
。 This returns NULL
if not found, or a pointer to somewhere inside plain
found. NULL
,或者返回到找到的plain
内部某处的指针。 NULL
before using its value. NULL
之前,请确保该指针不为NULL
。 plain
(ie &plain[0]
, the address of the a
) from pp
. plain
(即&plain[0]
则该地址a
从) pp
。 This evaluates to 0 for a
, 1 for b
, and so on. a
值为0, b
值为1,依此类推。 encoded
. encoded
的对应字符。 ?
?
. In a portable, general program, you can not use plain subtraction (ie c - 'a'
), since C does not guarantee that characters are encoded in values next to each other. 在可移植的通用程序中,您不能使用普通减法(即
c - 'a'
),因为C 不能保证字符以彼此相邻的值编码。
As pointed out, the above assumes that each character encodes in exactly one char
. 如前所述,以上假设每个字符都精确地编码为一个
char
。 That might not be true for targets with exotic encodings, in which case it really is safer to use an explicit table, like this: 对于具有奇异编码的目标,可能并非如此,在这种情况下,使用显式表确实更安全,如下所示:
const struct {
char plain;
char encoded;
} encoding[] = {
{ 'a', 'd' },
{ 'b', 'e' },
{ 'c', 'f' },
/* ... and so on ... */
};
The encoding function then becomes: 编码函数将变为:
char encode_char2(char c)
{
for(size_t i = 0; i < sizeof encoding / sizeof *encoding; ++i)
{
if(encoding[i].plain == c)
return encoding[i].encoded;
}
return '?'; /* Not found. */
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.