user2440329
user2440329

Reputation: 217

Printing only the first character of an array

int
cycle(n) {
    char *s3 = "Different";
    int i, length;
    length = strlen(s3)
    printf("%s\n", s3);
    for (i=0; i<length; i++) {
        printf("%s\n", &s3[i]);

    }
    return 0;
}

I'm trying to print out one character at a time on each newline however the output looks like this -

C: Different   
C: ifferent
C: fferent
ect.

How do I make it just output one character at a time? Example (C: D, C:i, C:f, ect.)

Upvotes: 2

Views: 1923

Answers (2)

Alexander Oh
Alexander Oh

Reputation: 25621

#include <stdio.h>
for (int i = 0; i < strlen(s3); i++) {
  printf("%c\n", s3[i]);
}

Upvotes: 2

Sani Huttunen
Sani Huttunen

Reputation: 24375

You should change:

printf("%s\n", &s3[i]);

to

printf("%c\n", s3[i]);

The former prints a string while the latter prints a char.

Upvotes: 4

Related Questions