注册 登录
编程论坛 C语言论坛

自己还没有太入门,求助

A3C 发布于 2023-11-03 22:54, 1082 次点击
只有本站会员才能查看附件,请 登录
3 回复
#2
rjsp2023-11-03 23:20
程序代码:
char* foo( char* s )
{
    for( char* p=s; *p; ++p )
    {
        if( *p>='A' && *p<='Z' )
            *p = (*p - 'A' + 4)%26 + 'a';
        else if( *p>='a' && *p<='z' )
            *p = (*p - 'a' + 4)%26 + 'A';
    }
    return s;
}

#include <string.h>
#include <assert.h>

int main( void )
{
    char s1[] = "China";
    assert( strcmp(foo(s1),"gLMRE") == 0 );

    char s2[] = "azAZ";
    assert( strcmp(foo(s2),"EDed") == 0 );
}
#3
北城清风2023-11-04 23:41
#4
lliu_20062023-11-05 17:59
提供另外一种思路
#include <stdio.h>

static char *foo(char *s)
{
    char c;
    char *p = s;

    while (c = *p)
        *p++ = ((c & 0xDF) > 'V' ? c - 22 : c + 4) ^ 0x20;

    return s;
}

void main(int argc, char *argv[])
{
    if (argc > 1)
        printf("%s\n", foo(argv[1]));
}
1