Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

40,023 questions

51,975 answers

573 users

How to convert a string to PascalCase in C

1 Answer

0 votes
#include <stdio.h>
#include <ctype.h>
#include <string.h>

void get_pascal_case(const char *input, char *output) {
    char temp[256];
    strcpy(temp, input);
    int len = strlen(temp);
    int j = 0;
    int capitalize_next = 1;
    int first_char_found = 0; // Flag to track if the first valid char is found.

    for (int i = 0; i < len; i++) {
        if (temp[i] == ' ' || temp[i] == '_') {
            capitalize_next = 1;
        } else {
            if (!first_char_found) {
                first_char_found = 1; // Set flag when first valid char is found.
            }

            if (capitalize_next) {
                output[j++] = toupper(temp[i]);
                capitalize_next = 0;
            } else {
                output[j++] = tolower(temp[i]);
            }
        }
    }
    output[j] = '\0';
}

int main() {
    char output[256];

    get_pascal_case("get file content", output);
    printf("%s\n", output);

    get_pascal_case("get_file_content", output);
    printf("%s\n", output);

    get_pascal_case("get______file___content", output);
    printf("%s\n", output);

    get_pascal_case("get______file____  content", output);
    printf("%s\n", output);

    get_pascal_case("GET FILE CONTENT", output);
    printf("%s\n", output);

    get_pascal_case("get    file      content", output);
    printf("%s\n", output);

    get_pascal_case("getFileContent", output);
    printf("%s\n", output);

    get_pascal_case("  get file content", output);
    printf("%s\n", output);

    get_pascal_case("get file content  ", output);
    printf("%s\n", output);

    return 0;
}


  
/*
run:
  
GetFileContent
GetFileContent
GetFileContent
GetFileContent
GetFileContent
GetFileContent
Getfilecontent
GetFileContent
GetFileContent
  
*/

 



answered Feb 23, 2025 by avibootz

Related questions

1 answer 72 views
1 answer 86 views
2 answers 99 views
1 answer 81 views
1 answer 89 views
1 answer 86 views
...