Traversing Strings

Explore different ways of traversing strings in C.

Method 1: Using the for loop

RUN the code given below and see the output!

Press + to interact
#include<stdio.h>
int main() {
char name[] = {'S', 'a', 'n', 'j', 'a', 'y', '\0'};
int i;
// Traverse string using for loop
for (i = 0; i <=5; i++){
printf("%c ", name[i]);
}
}

Line 7: The loop runs only up to i = 5. In this method, we must specify the length of the string in the for loop.

Line 8: Prints the character at index, i, of an array, ...

Access this course and 1400+ top-rated courses and projects.