How to convert a string to sentence case in C

2 Answers

0 votes
#include <stdio.h>

void to_sentence_case(char str[]) {
    if (str[0] >= 'a' && str[0] <= 'z') {
        str[0] = str[0] - 32;
    }

    for (int i = 1; str[i] != '\0'; i++) {
        if ((str[i] >= 'A' && str[i] <= 'Z') || (str[i] >= 'a' && str[i] <= 'z')) {
            if (str[i] >= 'A' && str[i] <= 'Z') {
                str[i] = str[i] + 32;
            }
        }
    }
}

int main(void)
{
    char str[] = "c is a General-purpose COMPUTER PROgramming LANGuage";

    to_sentence_case(str);

    puts(str);

    return 0;
}




/*
run:

C is a general-purpose computer programming language

*/

 



answered Jul 29, 2022 by avibootz
edited Jul 29, 2022 by avibootz
0 votes
#include <stdio.h>
#include <string.h>

void to_sentence_case(char str[]) {
    _strlwr(str);

    if (str[0] >= 'a' && str[0] <= 'z') {
        str[0] = str[0] - 32;
    }
}

int main(void)
{
    char str[] = "c is a General-purpose COMPUTER PROgramming LANGuage";

    to_sentence_case(str);

    puts(str);

    return 0;
}




/*
run:

C is a general-purpose computer programming language

*/

 



answered Nov 10, 2022 by avibootz

Related questions

1 answer 130 views
1 answer 131 views
1 answer 110 views
1 answer 162 views
162 views asked Jun 3, 2017 by avibootz
1 answer 196 views
1 answer 217 views
...