How to iterate over a string in C?

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

    You want:

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

    sizeof gives you the size of the pointer, not the string. However, it would have worked if you had declared the pointer as an array:

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

    but if you pass the array to function, that too will decay to a pointer. For strings it's best to always use strlen. And note what others have said about changing printf to use %c. And also, taking mmyers comments on efficiency into account, it would be better to move the call to strlen out of the loop:

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

    or rewrite the loop:

    for (i = 0; source[i] != 0; i++){
    
    0 讨论(0)
  • 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]); }
    
    0 讨论(0)
  • 2020-11-28 07:15
    • sizeof(source) is returning to you the size of a char*, not the length of the string. You should be using strlen(source), and you should move that out of the loop, or else you'll be recalculating the size of the string every loop.
    • By printing with the %s format modifier, printf is looking for a char*, but you're actually passing a char. You should use the %c modifier.
    0 讨论(0)
  • 2020-11-28 07:16

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

    #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;
    }
    
    0 讨论(0)
  • 2020-11-28 07:16

    sizeof(source) returns the number of bytes required by the pointer char*. You should replace it with strlen(source) which will be the length of the string you're trying to display.

    Also, you should probably replace printf("%s",source[i]) with printf("%c",source[i]) since you're displaying a character.

    0 讨论(0)
  • 2020-11-28 07:17

    An optimized approach:

    for (char character = *string; character != '\0'; character = *++string)
    {
        putchar(character); // Do something with character.
    }
    

    Most C strings are null-terminated, meaning that as soon as the character becomes a '\0' the loop should stop. The *++string is moving the pointer one byte, then dereferencing it, and the loop repeats.

    The reason why this is more efficient than strlen() is because strlen already loops through the string to find the length, so you would effectively be looping twice (one more time than needed) with strlen().

    0 讨论(0)
提交回复
热议问题