如何在 C 中迭代字符串?

问题描述 投票:0回答:14

现在我正在尝试这个:

#include <stdio.h>

int main(int argc, char *argv[]) {

    if (argc != 3) {

        printf("Usage: %s %s sourcecode input", argv[0], argv[1]);
    }
    else {
        char source[] = "This is an example.";
        int i;

        for (i = 0; i < sizeof(source); i++) {

            printf("%c", source[i]);
        }
    }

    getchar();

    return 0;
}

这也不起作用:

char *source = "This is an example.";
int i;

for (i = 0; i < strlen(source); i++){

    printf("%c", source[i]);
}

我收到错误

Test.exe 中 0x5bf714cf (msvcr100d.dll) 处未处理的异常:0xC0000005:在位置 0x00000054 读取时发生访问冲突。

(从德语大致翻译)

那么我的代码有什么问题吗?

c iteration
14个回答
90
投票

你想要:

for (i = 0; i < strlen(source); i++) {

sizeof
给出指针的大小,而不是字符串的大小。但是,如果您将指针声明为数组,它就会起作用:

char source[] = "This is an example.";

但是如果将数组传递给函数,它也会衰减为指针,这就是为什么对于字符串最好始终使用

strlen

另请注意其他人关于更改 printf 以使用 %c 的说法,并且考虑到 mmyers 对效率的评论,最好将调用移至循环之外:

strlen

或者重写循环:

int len = strlen(source); for (i = 0; i < len; i++) {



65
投票

for (i = 0; source[i] != 0; i++) {

一些注意事项:

在 C 中,字符串是
    以 null 结尾的
  • 。当读取的字符不是空字符时进行迭代。
  • char* c = source; while (*c) putchar(*c++);
  • 递增
    *c++
    并返回
    c
    的取消引用的 old 值。
  • c
  • 打印以 null 结尾的字符串,而不是字符。这就是您访问违规的原因。
    
        

8
投票

printf("%s")

大多数 C 字符串都是以 null 结尾的,这意味着一旦字符变成 
for (char character = *string; character != '\0'; character = *++string) { putchar(character); // Do something with character. }

,循环就应该停止。

'\0'
将指针移动一个字节,然后取消引用它,然后重复循环。

这比

*++string

更高效的原因是因为 strlen 已经循环遍历字符串来查找长度,因此您实际上可以使用

strlen()
循环两次(比需要的多一次)。
    


5
投票

strlen()



2
投票
#include <stdio.h> int main(int argc, char *argv[]) { const char *const pszSource = "This is an example."; const char *pszChar = pszSource; while (pszChar != NULL && *pszChar != '\0') { printf("%s", *pszChar); ++pszChar; } getchar(); return 0; }

返回指针

sizeof(source)
所需的字节数。您应该将其替换为
char*
,这将是您尝试显示的字符串的长度。

此外,您可能应该将

strlen(source)

替换为

printf("%s",source[i])
,因为您正在显示一个字符。
    


2
投票
sizeof() 包含终止空字符。您应该使用 strlen() (但将调用放在循环之外并将其保存在变量中),但这可能不是导致异常的原因。
  1. 你应该在 printf 中使用“%c”,而不是“%s” - 你正在打印一个字符,而不是一个字符串。

2
投票

printf("%c",source[i])



2
投票

#include <stdio.h> #include <string.h> int main(int argc, char *argv[]){ char *source = "This is an example."; int length = (int)strlen(source); //sizeof(source)=sizeof(char *) = 4 on a 32 bit implementation for (int i = 0; i < length; i++) { printf("%c", source[i]); } }



1
投票
    for(int i = 0; string[i]; i++) { printf("Char at position %d is %c\n", i, string[i]); }
  • 返回给您的是
    sizeof(source)
    的大小,而不是字符串的长度。您应该使用
    char*
    ,并且应该将其移出循环,否则您将在每个循环中重新计算字符串的大小。
    通过使用 
  • strlen(source)
  • 格式修饰符进行打印,
    %s
    正在寻找
    printf
    ,但实际上您传递的是
    char*
    。您应该使用
    char
    修饰符。
    
        

1
投票

像这样:

%c



1
投票

char *source = "This is an example."; int i; for (i = 0; i < strlen(source); i++){ printf("%c", source[i]); }

C 中的 char 是一个 8 位整数,带有相应字符的 ASCII 数字值。这意味着在 char[19] 之前,source[i] 是一个正整数,char[19] 是最后一个“.”之后的空终止符。空字符是 ASCII 0。这是循环终止的地方。该循环迭代每个字符,而不考虑数组的长度。


0
投票


0
投票
char source[] = "This is an example."; for (int i = 0; source[i]; i++) printf("%c", source[i]);

返回指针的大小,因为源被声明为 char *。 正确的使用方法是

sizeof(source)

下一个:

strlen(source)

需要字符串。即 %s 需要字符串,但您正在循环中迭代以打印每个字符。因此使用%c。 

但是,您使用索引 i 访问(迭代)字符串的方式是正确的,因此其中没有其他问题。


0
投票

printf("%s",source[i]);

会完成工作

另外,当然你应该指的是

printf("%s", source + i);

,而不是

strlen(source)
    

© www.soinside.com 2019 - 2024. All rights reserved.