How to iterate over a string in C?

前端 未结 13 1276
花落未央
花落未央 2020-11-28 06:43

Right now I\'m trying this:

#include 

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

    if (argc != 3) {

        printf(\"Usage: %s %s sourcecode inpu         


        
13条回答
  •  北海茫月
    2020-11-28 07:16

    Rather than use strlen as suggested above, you can just check for the NULL character:

    #include 
    
    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;
    }
    

提交回复
热议问题