C语言 在 C 中逐字符读取字符串

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26072322/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:24:52  来源:igfitidea点击:

Reading string character by character in C

carraysstringcharmain

提问by aNobody

So I have a string passed into main function: int main(int argc, char* argv[])

所以我有一个字符串传递给主函数: int main(int argc, char* argv[])

I understand argc (which is 2 in this case), but don't understand how I can read argv[] character by character? When I print argv[0] shouldn't that print the first character in the array of characters for that string?

我了解 argc(在本例中为 2),但不明白我如何逐个读取 argv[] ?当我打印 argv[0] 时,不应该打印该字符串的字符数组中的第一个字符吗?

Thanks

谢谢

回答by BLUEPIXY

sample

样本

#include <stdio.h> 

int main(int argc, char *argv[]){
    int i,j;
    for(i=0; i<argc; ++i){
        for(j=0; argv[i][j] != '
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (int argc, char *argv[])
{
    if(argc != 2)
    {
        //argv[0] is name of executable
        printf("Usage: %s argument\n", argv[0]);
        exit(1);
    }
    else
    {
        int i;
        int length = strlen(argv[1]);
        for(i=0;i<length;i++)
        {
            printf("%c",argv[1][i]);
        }
        printf("\n");
        return 0;
    }
}
'; ++j){ printf("(%c)", argv[i][j]); } printf("\n"); } return 0; }

回答by user1336087

One more example:

再举一个例子:

##代码##