10

I have a string.

char foo[] = "abcdefgh";

I would like to write a for loop, and one by one print out all of the characters:

a
b
c

etc.

This is in C.

mpenkov
  • 21,621
  • 10
  • 84
  • 126
Honza Pokorny
  • 3,205
  • 5
  • 37
  • 43
  • 3
    Is this homework? Writing the code to iterate over characters of an array is rather trivial to write, relatively speaking. – In silico Dec 18 '10 at 00:49
  • I'm trying to learn C from the ground up. Qt and the like make things easy for you. I want to learn the basics (how things like Qt are implemented). I googled a lot for this, and couldn't find it. Thanks – Honza Pokorny Dec 18 '10 at 00:56
  • Qt4 is C++. I know how to do this in C++, but not in C. – Honza Pokorny Dec 18 '10 at 00:56
  • 2
    I think, you can do this using Boost or STL or combining both – osgx Dec 18 '10 at 00:59
  • the easy way (like shown in Ninefingers's answer) is not the always the true. There are sometimes unicode chars, multibyte char, chinease chars. Using a sole library will help to provide universalism. – osgx Dec 18 '10 at 01:05
  • @Honza knowing C is not necessarily a bad thing but Qt is implemented in C++, the same C++ you've been using. C is its predecessor. However, if you're really interested in understanding C, there's plenty of books around that aren't bad at all. Search this site for "list of programming books". –  Dec 18 '10 at 01:07
  • You can do it in C++ exactly as you would in C, most valid C code is also valid C++ code. It is just a character array, surely you have used arrays in C++!? – Clifford Dec 18 '10 at 06:23

4 Answers4

13

Ok, well, this is a question so I'm going to answer it, but my answer is going to be slightly unusual:

#include <stdio.h>

int main(int argc, char** argv)
{
    char string[] = "abcdefghi";
    char* s;

    for ( s=&string[0]; *s != '\0'; s++ )
    {
        printf("%c\n", *s);
    }

    return 0;
}

This is not the simplest way to achieve the desired outcome; however, it does demonstrate the fundamentals of what a string is in C. I shall leave you to read up on what I've done and why.

  • 1
    I'm sorry that you think I'm ridiculous. This answer is perfect. I think I finally get strings in C. Thanks! – Honza Pokorny Dec 18 '10 at 01:05
  • 5
    @Honza Pokorny: It wasn't ridiculous as much as it was unusual because it's such a fundamental part of the language that it was surprising to see someone not know how strings are handled in C and C++. It's usually one of the first things you learn in C and C++ (and probably just about every other language out there). I suggest you pick up a good C or C++ book (depending on which one you want to learn) and really learn the fundamentals. – In silico Dec 18 '10 at 01:06
  • 1
    @Honza exactly. My personal thought was you'd seen one too many "give me the codez" style questions and thought you'd make a tongue in cheek question. Yes, get a book as @In Silico says. I've also written this using pointers deliberately to be awkward. There is a simpler solution (not by much) but it doesn't demonstrate a few things I wanted to demonstrate. –  Dec 18 '10 at 01:17
  • @Honza: No the answer is not "perfect", it is deliberately over complicated; that is clear from the first sentence, and previous comment. – Clifford Dec 18 '10 at 06:25
  • `s=&string[0]`?! Why?? I've seen enough such nonsense in existing code; don't teach newbies that way. – R.. GitHub STOP HELPING ICE Dec 18 '10 at 11:58
  • 2
    @r because I didn't want to give the straight up answer to the question, because the OP will see a better way to do it as soon as he picks up a book. Hopefully this will get him thinking what that line genuinely means. I think I made it clear that it wasn't the usual way to approach things. I personally wouldn't ever use this particular method for printing a string, but I'd have liked to have seen it earlier than I did. –  Dec 18 '10 at 12:04
  • This is fine as long as string[] is not a constant string. – Sarah cartenz Oct 07 '19 at 13:11
2
void main(int argc, char** argv)
{
    char foo[] = "abcdefgh"; 
    int len = strlen(foo);
    int i = 0;
    for (i=0; i < len; i++)
    {
        printf("%c\n", foo[i]);
    }
    return 0;
}
Honza Pokorny
  • 3,205
  • 5
  • 37
  • 43
1

I find this method more useful than using strlen(). Because strings in C terminates with a null byte, we can loop them like this :

void loop_str(char *s) {
    for(int i = 0; s[i] != '\0';  i ++) {
        printf("s[%d] -> %c\n", i , s[i]);
    }
}
0

Yet another way

int main(int argc, char *argv[])
{
   char foo[] = "abcdefgh";
   int len = sizeof(foo)/sizeof(char);
   int i = 0;
   for (i=0; i < len; i++) {
      printf("%c\n", foo[i]);
   }
   return 0;
}
Nick Masao
  • 1,088
  • 12
  • 25