#include <stdio.h>
char *cap_string(char *str);
int main(void) {
char str[] = "Expect the best. Prepare for the worst. Capitalize on what comes.\nhello world! hello-world 0123456hello world\thello world.hello world\n";
char *ptr;
ptr = cap_string(str);
printf("%s", ptr);
printf("%s", str);
return (0);
}
char *cap_string(char *str)
{
int index = 0;
while (str[index])
{
while (!(str[index] >= 'a' && str[index] <= 'z'))
index++;
if (str[index - 1] == ' ' ||
str[index - 1] == '\t' ||
str[index - 1] == '\n' ||
str[index - 1] == ',' ||
str[index - 1] == ';' ||
str[index - 1] == '.' ||
str[index - 1] == '!' ||
str[index - 1] == '?' ||
str[index - 1] == '"' ||
str[index - 1] == '(' ||
str[index - 1] == ')' ||
str[index - 1] == '{' ||
str[index - 1] == '}' ||
index == 0)
str[index] -= 32;
index++;
}
return (str);
}
I want to understand what this loop is doing, I just cant follow
while (!(str[index] >= 'a' && str[index] <= 'z')){
index++;
defines a function
cap_stringwhich capitalizes the first letter of each word in a given string, where words are defined as sequences of characters separated by spaces, tabs, newlines, commas, semicolons, periods, exclamation marks, question marks, double quotes, parentheses, or curly braces. The main function defines a stringstr, passes it tocap_string, and then prints the modified string and the original string to the console.however this loop is checking if there's a spaces, tabs, newlines, commas, semicolons, periods, exclamation marks, question marks, double quotes, parentheses, or curly braces in the main string and devide it and capitalize the first letter in it