C Program to Find Camel Case without using Library Function

Introduction

In this post, I am going to write a c program to find camel case without using library function. So, this program can be done, in two ways and both ways I am going to explain here. The first way is without using library function and another way is using library function.

Also Read: C Program to Display a String in Capital Letters

Before writing a c program to find camel case, let us see the expected output. This will help us to understand the logic of the program.

Expected Output for Camel Case Program in C

Enter a sentence
c program to find camel case
Original String = c program to find camel case
Camel Case = cProgramToFindCamelCase

As you can see the above expected output. We are asking the user to enter a sentence. After entering, we are removing the space between the two words and making first letter of each word uppercase.

What is the meaning of Camel case?

Camel case is nothing but a phrase without blank space between the words and each word starts with the capital letter. For example, CamelCase, iPhone, MyComputer, CProgramming and many more. This can be used as a naming conventions in many programming languages. I hope, you got the meaning of camel case.

Also Read: C Program to Count the Characters in the String Except Space

Logic to write a c program to find camel case

The logic of this program is so simple. You have to perform the following steps on the string.

  1. Read the string from the user.
  2. Scan this string from left to right until we get ‘\0’ i.e. null character.
  3. Copy each and every character from one character array i.e. string to another string.
  4. If there is a blank space then do nothing and move to the next character.
  5. As we know, if there is a blank space that means one word is over and the next word is started.
  6. So, we have to convert the first letter of this word into a capital letter.

With the help of these steps, we are writing the program using and without using library function i.e. toupper().

Also Read: Interview Questions On C

C Program to Find Camel Case without using Library Function

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char str[100],camelcase[100];
    int flag=0,i=0,j=0;
    printf("Enter a sentence\n");
    gets(str);
    printf("Original String = %s\n",str);
    while(str[i]!='\0')
    {
        if(str[i]==' ')
        {
           i++;
           flag=1;
           continue;
        }
        if(flag==1)
        {
            camelcase[j]=str[i]-32;
            flag=0;
        }
        else
        {
            camelcase[j]=str[i];
        }
        j++;
        i++;
    }
    printf("Camel Case = %s",camelcase);
    return 0;
}

C Program to Find Camel Case using Library Function

#include <stdio.h>
#include <stdlib.h>
int main()
{
    char str[100],camelcase[100];
    int flag=0,i=0,j=0;
    printf("Enter a sentence\n");
    gets(str);
    printf("Original String = %s\n",str);
    while(str[i]!='\0')
    {
        if(str[i]==' ')
        {
           i++;
           flag=1;
           continue;
        }
        if(flag==1)
        {
            camelcase[j]=toupper(str[i]);
            flag=0;
        }
        else
        {
            camelcase[j]=str[i];
        }
        j++;
        i++;
    }
    printf("Camel Case = %s",camelcase);
    return 0;
}

I hope you have understood these two programs. If you have any difficulties regarding these programs, you can contact me.

Thank you.

Leave a Comment