Winter Bootcamp in ML and IoT in Jaipur
 Course content (For Bootcamp & Winter Training):- Machine Learning (ML) || Internet of Things (IoT) || Register for winter bootcamp
0 like 0 dislike
in Tutorial & Interview questions by (3.4k points)

If we know the length of the string, we can use a for loop to iterate over its characters:

1 Answer

0 like 0 dislike
by (3.4k points)
 
Best answer
If we know the length of the string, we can use a for loop to iterate over its characters:

char * string = "hello world"; /* This 11 chars long, excluding the 0-terminator. */

size_t i = 0;

for (; i < 11; i++)

{    

printf("%c\n", string[i]);    /* Print each character of the string. */ }

Alternatively, we can use the standard function strlen() to get the length of a string if we don't know what the string is:

size_t length = strlen(string);

size_t i = 0;

for (; i < length; i++)

{    

printf("%c\n", string[i]);    /* Print each character of the string. */

}

Finally, we can take advantage of the fact that strings in C are guaranteed to be null-terminated (which we already did when passing it to strlen() in the previous example ;-)). We can iterate over the array regardless of its size and stop iterating once we reach a null-character:

size_t i = 0;

while (string[i] != '\0')

{       /* Stop looping when we reach the null-character. */    

printf("%c\n", string[i]);    /* Print each character of the string. */    

i++;

}

Winter 10 Days boot-camp classes(7 HRS Daily) will start from 5, 20 & 27 December 2019 in:
1) Internet of things(IoT) Using RASPBERRY-PI
2) Machine Learning (ML)

70% OFF| Fee-INR 3,000/-

Limited seats!! Hurry up!!

[[ CALL - 07976731765 ]]

Some Study Resources are compiled from original Stack Overflow Documentation, the content is developed by the different experts at Stack Overflow. Study resources are released under Creative Commons BY-SA. Images may be copyright of their respective owners. This website is for self-learning and not affiliated with Stack Overflow. All trademarks and registered trademarks are the property of their respective company owners. Please send feedback and corrections to chandwaglobal@gmail.com.

Goeduhub Important Lists Our Youtube Channels (For free E-learning)
About Us List of IITs Goeduhub Technologies
Contact Us List of NITs AI and Big Data-HADOOP E-Learning Series
  List of PSUs Smart Learning PLC-SCADA, IoT and Raspberry-PI
  List of Exams After Graduation  
...