转换codeword到codeword,反之亦然c程序

6za6bjd0  于 5个月前  发布在  其他
关注(0)|答案(5)|浏览(74)

我正在努力实现一个函数,它可以将字母转换为字母,反之亦然。
我不明白的是,当我在主程序中使用代码时,它可以工作,当我试图把它放在一个函数中时,它不工作,我不明白。

#include <stdio.h>
#include <ctype.h>

char sentence[100];
int count, ch, i;

void KUlowerL(char *sentence)
{
    printf("%s", sentence); // Test if I get char from sentence. it works ?!?!
    for (i=0;(sentence[i] = getchar()) != '\n'; i++); /* enters the user sentence to change the case */

    sentence[i] = '\0';
    count = i;
    printf("Der Satz ist: %s", sentence);
    printf("\nDer veränderte Satz ist: ");

    for (i = 0; i < count; i++)
    {
        ch = islower(sentence[i]) ? toupper(sentence[i]) : tolower(sentence[i]);
        putchar(ch);
    }
}

int main()
{
    printf("Bitte gebe deinen Satz ein: ");
    fgets(sentence, 100, stdin);
    KUlowerL(sentence);
}

字符串

sczxawaw

sczxawaw1#

我注意到以下情况:

fgets(sentence, 100, stdin);

字符串
.然后我注意到你正在传递给KUlowerL,那么getchar循环是什么?你已经用fgets代码做过了,是吗?
关于getchartolower,这两者在某种程度上是fgets无法联系在一起的.你需要将char的值转换为unsigned char,然后传递给tolower,在那里它们将被提升为int,而getchar返回你已经为你转换的角色,因此,如果将返回值保持为**int**形式,则不需要强制转换。

mf98qq94

mf98qq942#

对于初学者来说,使用这样的全局变量是一个坏主意

char sentence[100];
int count, ch, i;

字符串
没有必要将它们定义为全局的。
该函数已经通过其参数获得了一个字符串。因此使用getchar调用重写它没有意义。

void KUlowerL(char *sentence)
{
    printf("%s", sentence); // Test if I get char from sentence. it works ?!?!
    for (i=0;(sentence[i] = getchar()) != '\n'; i++); /* enters the user sentence to change the case */ 
    //...


此外,该函数不会更改已处理的字符串。
可以通过以下方式声明和定义函数

char * KUlowerL( char *sentence )
{
    for ( char *p = sentence; *p != '\0'; ++p )
    {
        if ( islower( ( unsigned char )*p ) )
        {
            *p = toupper( ( unsigned char )*p );
        }
        else
        {
            *p = tolower( ( unsigned char )*p );
        }
    }

    return sentence;
}


在main中,函数可以这样调用:

int main( void )
{
    enum { N = 100 };
    char sentence[N];

    printf( "Bitte gebe deinen Satz ein: " );
    fgets( sentence, N, stdin );

    printf( "%s", KUlowerL(sentence) );
}

oxf4rvwz

oxf4rvwz3#

你不需要这个?运算符。该函数将改变字母的大小写,忽略非字母。

ch = toupper((unsigned char)sentence[i]);

字符串
下面这条线毫无意义

for (i=0;(sentence[i] = getchar()) != '\n'; i++);


你只需要在字符串中遍历

void KUlowerL(char *sentence)
{
    while(*sentence && *sentence != '\n')
        putchar(toupper((unsigned char)*sentence++));
}

int main()
{
    char sentence[100];
    printf("Bitte gebe deinen Satz ein: ");
    fgets(sentence, 100, stdin);
    printf("\n");
    KUlowerL(sentence);
}


编辑:
换箱子

void SwapCase(char *sentence)
{
    while(*sentence && *sentence != '\n')
    {
        int c = *sentence;

        if(islower((unsigned char)c)) c = toupper((unsigned char)c);
        else if(isupper((unsigned char)c)) c = tolower((unsigned char)c);
        sentence++;
        putchar(c);
    }
}

int main()
{
    char sentence[100];
    printf("Bitte gebe deinen Satz ein: ");
    fgets(sentence, 100, stdin);
    printf("\n");
    SwapCase(sentence);
}


https://godbolt.org/z/WdcKh5TfK

vxqlmq5t

vxqlmq5t4#

它看起来不起作用的原因是使用getchar()的循环,你不希望输入句子ZWEI MAL!你按回车键,什么也看不到,因为第二个“句子”是零个字符长.
我检查了你的代码做了些修改...

#include <stdio.h>
#include <ctype.h>

char sentence[100]; // global. need not be passed around
// other variables declared/defined closer to where they are used.

// main() prints the first input sentence so no need to pass a global variable
void KUlowerL( void )
{
    printf("Bitte gebe deinen Satz noch mal ein: ");
    // that for() loop was alright, but doing the work of a standard library function.
    // Use (proven) library functions when possible.
    // "sizeof" is better than repeating "magic number" of 100
    // The compiler knows the size and sizeof means you can grow or
    // shrink buffer in one place, not two...
    fgets( sentence, sizeof sentence, stdin );

    // measure the length (incl. the '\n'
    int count = strlen( sentence );
    printf("Der Satz hält %d buchstabben: %s", count, sentence );

    printf("\nDer veränderte Satz ist: ");
    for( int i = 0; i < count; i++ )
    {
        // 'c' is easier to read than 3 copies of 'sentence[i]'
        char c = sentence[ i ];
        // You want to 'swap' the upper/lower case-ness of each letter
        putchar( islower( c ) ? toupper( c ) : tolower( c ) );
    }
}

int main()
{
    printf("Bitte gebe deinen Satz ein: ");
    fgets(sentence, 100, stdin);

    printf("main(): sentence is %s", sentence ); // includes '\n' from fgets()

    KUlowerL();

    return 0;
}

字符串
输出量:

Bitte gebe deinen Satz ein: Guten morgen
main(): sentence is Guten morgen
Bitte gebe deinen Satz noch mal ein: Schlaff Schoen
Der Satz hõlt 15 buchstabben: Schlaff Schoen
{empty line because buffer contains '\n' too.)
Der verõnderte Satz ist: sCHLAFF sCHOEN

uelo1irk

uelo1irk5#

实现一个函数,该函数将把小写字母转换为小写字母,反之亦然。

完善案例交换功能

  • 一个 string 以一个 *null字符 * 结尾。一个 line 的输入以一个'\n'结尾。最好创建一个函数来处理一个 string 并在一个 *null字符 * 处停止,而不是'\n'
  • 遍历字符串一次。没有必要先找到字符串长度,然后再转换。两者一起做。
  • KUlowerL()中删除用户I/O代码。让调用代码读取数据并打印文本。OP试图读取两行输入(一行在main()中,一行在KUlowerL()中)会引起麻烦。它可以解释“当我在main中使用代码时,它可以工作,当我试图将其放入函数时,它不工作”。
  • is...(int ch)to...(int ch)对于值[0...UCHAR_MAX]EOF是明确定义的,否则如果ch < 0,结果是 undefined behavior(UB)。
  • str...()函数的执行方式就像通过unsigned char *访问 * 字符串 * 一样,即使char是 * 签名的 *。这种情况下转换代码也应该执行同样的操作。
  • KUlowerL()是一个容易混淆的名称,用于交换大小写的函数。
  • 返回一些有用的东西,比如字符串的开头。
#include <ctype.h>

char *TLG_swapcase(char *s) {
  unsigned char *us = (unsigned char *) s;
  while (*us) {  // Test for string's end.
    *us = islower(*us) ? toupper(*us) : tolower(*us);  // Swap  case.
    us++; // Advance pointer to next character.
  }
  return s;
}

字符串

测试代码

  • 使用本地缓冲区。
  • 最好测试输入函数的返回值,以确定输入是否成功。
#include <stdio.h>

int main(void) {
  char line[100];
  printf("Bitte gebe deinen Satz ein: ");
  if (fgets(line, sizeof line, stdin)) {
    printf("%s", TLG_swapcase(line));
  }
  return 0;  // Optional with main().
}

相关问题