C:遍历char数组,修改文字

时间:2011-06-21 11:16:40

标签: c arrays char

我是C编程的新手,虽然有C#经验。我正在尝试通过我的char数组并用'x'替换'a'并将其打印到屏幕上。不知怎的,这不起作用......为什么;)

在Debian 6 64bit上使用gcc 2.2.4

#include <stdio.h>
int main()
{
/* A nice long string */
char string[256];
int i;
printf( "Please enter a long string: " );
/* notice stdin being passed in */
fgets ( string, 256, stdin );           

    for( i = 0; i < 256; i++)
    {
            if( string[i] == 'a' )
            {
                    string[i] == 'x';
                    printf("%s", "foo");
            }
    }
  printf( "You entered a very long string, %s", string );
  getchar();
}

3 个答案:

答案 0 :(得分:2)

你有双倍的等价

if( string[i] == 'a' )
{
    string[i] == 'x'; /* <-- here */

这样做:

if( string[i] == 'a' )
{
    string[i] = 'x';

答案 1 :(得分:0)

string[i]='x',而不是== 'x'

答案 2 :(得分:0)

至少有一条评论说:“编译器应警告这一点”。我强烈建议你 make 编译器警告你这件事。 :) 让我解释。

对于其他平台的YMMV,但在 Unix 中,您可以将CFLAGS环境变量设置为在默认情况下使用make实用程序时默认包含警告。

以下是我的bash会话的成绩单,我将代码粘贴到一个文件中,使用make进行编译,然后将优化添加到CFLAGS变量并重新编译以查看是否会发出更多警告。

503(1)06:22 PM:~ 0> cat >sostr.c
#include <stdio.h>
int main()
{
/* A nice long string */
char string[256];
int i;
printf( "Please enter a long string: " );
/* notice stdin being passed in */
fgets ( string, 256, stdin );

    for( i = 0; i < 256; i++)
    {
            if( string[i] == 'a' )
            {
                    string[i] == 'x';
                    printf("%s", "foo");
            }
    }
  printf( "You entered a very long string, %s", string );
  getchar();
}
504(1)06:22 PM:~ 0> make sostr
cc -g -Wall    sostr.c   -o sostr
sostr.c: In function 'main':
sostr.c:15: warning: statement with no effect
sostr.c:21: warning: control reaches end of non-void function
505(1)06:22 PM:~ 0> export CFLAGS+=' -O2'
506(1)06:23 PM:~ 0> touch sostr.c
507(1)06:23 PM:~ 0> make sostr
cc -g -Wall -O2    sostr.c   -o sostr
sostr.c: In function 'main':
sostr.c:15: warning: statement with no effect
sostr.c:21: warning: control reaches end of non-void function
508(1)06:23 PM:~ 0>

获取大量警告的另一个工具是splint;虽然它经常太挑剔。

509(1)06:33 PM:~ 0> splint sostr.c
Splint 3.1.2 --- 23 Aug 2008

sostr.c: (in function main)
sostr.c:9:1: Return value (type char *) ignored: fgets(string, 25...
  Result returned by function call is not used. If this is intended, can cast
  result to (void) to eliminate message. (Use -retvalother to inhibit warning)
sostr.c:15:21: Statement has no effect: string[i] == 'x'
  Statement has no visible effect --- no values are modified. (Use -noeffect to
  inhibit warning)
sostr.c:20:3: Return value (type int) ignored: getchar()
  Result returned by function call is not used. If this is intended, can cast
  result to (void) to eliminate message. (Use -retvalint to inhibit warning)
sostr.c:21:2: Path with no return in function declared to return int
  There is a path through a function declared to return a value on which there
  is no return statement. This means the execution may fall through without
  returning a meaningful result to the caller. (Use -noret to inhibit warning)

Finished checking --- 4 code warnings
510(1)06:34 PM:~ 1>

因此这两种方法都捕获相同的行15 == / =问题。两者都警告你,主要应该返回0;如果成功的话并且夹板还抱怨您忽略fgetsgetchar的返回值。看起来你真的想忽略getchar的结果,所以我建议写(void)getchar();来表明故意忽略该值。但是,对于fgets,情况并非如此。我建议你使用返回值或让警告立场(这个玩具代码可能会忽略该值)。检查返回值在其他程序中可能很重要,因此开始标记它们不是明智的习惯(void)