How to iterate over a string in C?

前端 未结 13 1282
花落未央
花落未央 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:08

    The last index of a C-String is always the integer value 0, hence the phrase "null terminated string". Since integer 0 is the same as the Boolean value false in C, you can use that to make a simple while clause for your for loop. When it hits the last index, it will find a zero and equate that to false, ending the for loop.

    for(int i = 0; string[i]; i++) { printf("Char at position %d is %c\n", i, string[i]); }
    

提交回复
热议问题