Camel Case Program

Here given code implementation process.

//C Program 
//Camel case of a given sentence
#include <stdio.h>

void camel_case(char str[],int size)
{
 
  for (int i = 0; i <= size; ++i)
  {

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

  int location=0;

  //Remove space
  for (int i = 0; i <= size; ++i)
  {
    if(str[i]!=' ')
    {
      str[location++]=str[i];
    }
  }
  if(location!=0 && location<=size)
  {
    //include at the end
    str[location]='\0';
  }
}
int main()
{

    //First Test Case
    char str1[]="get employee record";
    //get the size of charactor
    int size = sizeof(str1)/sizeof(str1[0]);

    printf("Before Convert : [%s]\n",str1);

    camel_case(str1,size);

    printf("After Convert : [%s]\n",str1);
    

    //Second Test Case

    char str2[]="  we   are   student ";

    size = sizeof(str2)/sizeof(str2[0]);

    printf("Before Convert : [%s]\n",str2);

    camel_case(str2,size);

    printf("After Convert : [%s]\n",str2);
    
    //Third Test Case
    char str3[]="  Get Sql Record ";
    size = sizeof(str3)/sizeof(str3[0]);
    printf("Before Convert : [%s]\n",str3);

    camel_case(str3,size);

    printf("After Convert : [%s]\n",str3);
 
  return 0;
}

Output

Before Convert : [get employee record]
After Convert : [GetEmployeeRecord]
Before Convert : [  we   are   student ]
After Convert : [WeAreStudent]
Before Convert : [  Get Sql Record ]
After Convert : [GetSqlRecord]
//Java Program
//Camel case of a given sentence
public class MyString
{

  public String camel_case(String text)
  {
    //Used to store result
    String result ="";

    if(text!=null && text.length()>0)
    {
     
      for (int i = 0; i < text.length();i++) {
  
        if(text.charAt(i)!=' ' && (i==0 && text.charAt(i)>='a' && text.charAt(i) <='z')||
          ( i > 0  && text.charAt(i-1)==' ' && text.charAt(i) >= 'a' && text.charAt(i) <='z'))
        {
          //When need to capitalize character
          //transformation lowercase a letter into a uppercase letter
          result+=(char)(text.charAt(i)-32);
        }
        else if(text.charAt(i)!=' ')
        {
          //Add a character
          result+=text.charAt(i);
        }
      }
     
    } 
    return result;
  }
  
  public static void main(String[] args) {
   

    MyString obj = new MyString();

    //First Test Case
    String text = "get employee record";

    System.out.print("Before Convert : ["+text+"]\n");

    text = obj.camel_case(text);

    System.out.print("After Convert : ["+text+"]\n");


    //Second Test Case
    text = "  we   are   student ";

    System.out.print("Before Convert : ["+text+"]\n");

    text = obj.camel_case(text);

    System.out.print("After Convert : ["+text+"]\n");

    //Third Test Case
    text = "  Get Sql Record ";

    System.out.print("Before Convert : ["+text+"]\n");

    text = obj.camel_case(text);

    System.out.print("After Convert : ["+text+"]\n");
  }
}

Output

Before Convert : [get employee record]
After Convert : [GetEmployeeRecord]
Before Convert : [  we   are   student ]
After Convert : [WeAreStudent]
Before Convert : [  Get Sql Record ]
After Convert : [GetSqlRecord]
//C++ Program
//Camel case of a given sentence
#include<iostream>
#include<string.h>
using namespace std;
class MyString {
	public:
		string camel_case(string text) {
			//Used to store result
			string result = "";
			if (text.size() > 0) {
				for (int i = 0; i < text.size(); i++) {
					if (text[i] != ' ' &&
						(i == 0 &&
							text[i] >= 'a' &&
							text[i] <= 'z') ||
						(i > 0 &&
							text[i - 1] == ' ' &&
							text[i] >= 'a' &&
							text[i] <= 'z')) {
						//When need to capitalize character
						//transformation lowercase a letter into a uppercase letter
						result += (char)(text[i] - 32);
					} else
					if (text[i] != ' ') {
						//Add a character
						result += text[i];
					}
				}
			}
			return result;
		}
};
int main() {
	MyString obj =  MyString();
	//First Test Case
	string text = "get employee record";
	cout << "Before Convert : [" << text << "]\n";
	text = obj.camel_case(text);
	cout << "After Convert : [" << text << "]\n";
	//Second Test Case
	text = "  we   are   student ";
	cout << "Before Convert : [" << text << "]\n";
	text = obj.camel_case(text);
	cout << "After Convert : [" << text << "]\n";
	//Third Test Case
	text = " Get Sql Record ";
	cout << "Before Convert : [" << text << "]\n";
	text = obj.camel_case(text);
	cout << "After Convert : [" << text << "]\n";
	return 0;
}

Output

Before Convert : [get employee record]
After Convert : [GetEmployeeRecord]
Before Convert : [  we   are   student ]
After Convert : [WeAreStudent]
Before Convert : [ Get Sql Record ]
After Convert : [GetSqlRecord]
//C# Program
//Camel case of a given sentence
using System;
public class MyString {
	public String camel_case(String text) {
		//Used to store result
		String result = "";
		if (text != null &&
			text.Length > 0) {
			for (int i = 0; i < text.Length; i++) {
				if (text[i] != ' ' &&
					(i == 0 &&
						text[i] >= 'a' &&
						text[i] <= 'z') ||
					(i > 0 &&
						text[i - 1] == ' ' &&
						text[i] >= 'a' &&
						text[i] <= 'z')) {
					//When need to capitalize character
					//transformation lowercase a letter into a uppercase letter
					result += (char)(text[i] - 32);
				} else
				if (text[i] != ' ') {
					//Add a character
					result += text[i];
				}
			}
		}
		return result;
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		//First Test Case
		String text = "get employee record";
		Console.Write("Before Convert : [" + text + "]\n");
		text = obj.camel_case(text);
		Console.Write("After Convert : [" + text + "]\n");
		//Second Test Case
		text = "  we   are   student ";
		Console.Write("Before Convert : [" + text + "]\n");
		text = obj.camel_case(text);
		Console.Write("After Convert : [" + text + "]\n");
		//Third Test Case
		text = "  Get Sql Record ";
		Console.Write("Before Convert : [" + text + "]\n");
		text = obj.camel_case(text);
		Console.Write("After Convert : [" + text + "]\n");
	}
}

Output

Before Convert : [get employee record]
After Convert : [GetEmployeeRecord]
Before Convert : [  we   are   student ]
After Convert : [WeAreStudent]
Before Convert : [  Get Sql Record ]
After Convert : [GetSqlRecord]
<?php
//Php Program
//Camel case of a given sentence
class MyString {
  public  function camel_case($text) {
    //Used to store result
    $result = "";
    if ($text != null &&
      strlen($text) > 0) {
        
      for ($i = 0; $i < strlen($text); $i++) {
   
        if (ord($text[$i]) != 32 &&( $i == 0 &&
          ord($text[$i]) >= ord('a') &&
          ord($text[$i]) <= ord('z'))
          || ($i > 0 &&
          ord($text[$i - 1]) == 32 &&
          ord($text[$i]) >= ord('a') &&
          ord($text[$i]) <= ord('z'))
          ) 
        {
                 
          //When need to capitalize character
          //transformation lowercase a letter into a uppercase letter
          $result .= chr(ord($text[$i]) - 32);
        } 
        else if (ord($text[$i]) != 32) {
          //Add a character
          $result .= $text[$i];
        }
      }
    }
    return $result;
  }
}

function main() {
  $obj = new MyString();
  //First Test Case
  $text = "get employee record";
  echo("Before Convert : [". $text ."]\n");
  $text = $obj->camel_case($text);
  echo("After Convert : [". $text ."]\n");
  //Second Test Case
  $text = "  we   are   student ";
  echo("Before Convert : [". $text ."]\n");
  $text = $obj->camel_case($text);
  echo("After Convert : [". $text ."]\n");
  //Third Test Case
  $text = "  Get Sql Record ";
  echo("Before Convert : [". $text ."]\n");
  $text = $obj->camel_case($text);
  echo("After Convert : [". $text ."]\n");

}
main();

Output

Before Convert : [get employee record]
After Convert : [GetEmployeeRecord]
Before Convert : [  we   are   student ]
After Convert : [WeAreStudent]
Before Convert : [  Get Sql Record ]
After Convert : [GetSqlRecord]
//Node Js Program
//Camel case of a given sentence
class MyString {
	camel_case(text) {
		//Used to store result
		var result = "";
		if (text != null &&
			text.length > 0) {
			for (var i = 0; i < text.length; i++) {
				if (text[i] != ' ' &&
					(i == 0 &&
						text.charCodeAt(i) >= 'a'.charCodeAt(0) &&
						text.charCodeAt(i) <= 'z'.charCodeAt(0)) ||
					(i > 0 &&
						text[i-1] == ' ' &&
						text.charCodeAt(i) >= 'a'.charCodeAt(0) &&
						text.charCodeAt(i) <= 'z'.charCodeAt(0))) {
					//When need to capitalize character
					//transformation lowercase a letter into a uppercase letter
					result +=  String.fromCharCode(text.charCodeAt(i) - 32);
				} else
				if (text[i] != ' ') {
					//Add a character
					result += text[i];
				}
			}
		}

		return result;
	}
}

function main(args) {
	var obj = new MyString();
	//First Test Case
	var text = "get employee record";
	process.stdout.write("Before Convert : [" + text + "]\n");
	text = obj.camel_case(text);
	process.stdout.write("After Convert : [" + text + "]\n");
	//Second Test Case
	text = "  we   are   student ";
	process.stdout.write("Before Convert : [" + text + "]\n");
	text = obj.camel_case(text);
	process.stdout.write("After Convert : [" + text + "]\n");
	//Third Test Case
	text = "  Get Sql Record ";
	process.stdout.write("Before Convert : [" + text + "]\n");
	text = obj.camel_case(text);
	process.stdout.write("After Convert : [" + text + "]\n");
}

main();

Output

Before Convert : [get employee record]
After Convert : [GetEmployeeRecord]
Before Convert : [  we   are   student ]
After Convert : [WeAreStudent]
Before Convert : [  Get Sql Record ]
After Convert : [GetSqlRecord]
# Python 3 Program
# Camel case of a given sentence
class MyString :
  def camel_case(self, text) :
    result = ""
    if (text != None and len(text) > 0) :
      i = 0
      while (i < len(text)) :
        if (ord(text[i]) != ord(' ')
          and(i == 0 and text[i] >= 'a'
            and ord(text[i]) <= ord('z')) or(i > 0 and ord(text[i - 1]) ==  ord(' ')
            and ord(text[i]) >= ord('a')
            and ord(text[i]) <= ord('z'))) :
          # When need to capitalize character
          # transformation lowercase a letter into a uppercase letter
          result += chr(ord(text[i]) - 32)
        elif (ord(text[i]) != ord(' ')) :
          # Add a character
          result += text[i]
        
        i += 1
      
    
    return result
  

def main() :
  obj = MyString()
  # First Test Case
  text = "get employee record"
  print("Before Convert : [{}]".format(text))
  text = obj.camel_case(text)
  print("After Convert : [{}]".format(text))
  # Second Test Case
  text = "  we   are   student "
  print("Before Convert : [{}]".format(text))
  text = obj.camel_case(text)
  print("After Convert : [{}]".format(text))
  # Third Test Case
  text = "  Get Sql Record "
  print("Before Convert : [{}]".format(text))
  text = obj.camel_case(text)
  print("After Convert : [{}]".format(text))


if __name__ == "__main__":
  main()

Output

Before Convert : [get employee record]
After Convert : [GetEmployeeRecord]
Before Convert : [  we   are   student ]
After Convert : [WeAreStudent]
Before Convert : [  Get Sql Record ]
After Convert : [GetSqlRecord]
# Ruby Program
# Camel case of a given sentence
class MyString 
  def camel_case(text) 
    result = ""
    if (text != nil &&
      text.length > 0) 
      i = 0
      while (i < text.length) 
        if (text[i] != ' ' &&
          (i == 0 &&
            text[i].ord >= 'a'.ord &&
            text[i].ord <= 'z'.ord) ||
          (i > 0 &&
            text[i - 1] == ' ' &&
            text[i].ord >= 'a'.ord &&
            text[i].ord <= 'z'.ord)) 
          # When need to capitalize character
          # transformation lowercase a letter into a uppercase letter
          result += (text[i].ord - 32).chr
        elsif (text[i]!= ' ') 
          # Add a character
          result += text[i]
        end
        i += 1
      end
    end
    return result
  end
end
def main() 
  obj = MyString.new()
  # First Test Case
  text = "get employee record"
  print("Before Convert  :[", text ,"]\n")
  text = obj.camel_case(text)
  print("After Convert  :[", text ,"]\n")
  # Second Test Case
  text = "  we   are   student "
  print("Before Convert  :[", text ,"]\n")
  text = obj.camel_case(text)
  print("After Convert  :[", text ,"]\n")
  # Third Test Case
  text = "  Get Sql Record "
  print("Before Convert  :[", text ,"]\n")
  text = obj.camel_case(text)
  print("After Convert  :[", text ,"]\n")
end
main()

Output

Before Convert  :[get employee record]
After Convert  :[GetEmployeeRecord]
Before Convert  :[  we   are   student ]
After Convert  :[WeAreStudent]
Before Convert  :[  Get Sql Record ]
After Convert  :[GetSqlRecord]
//Scala Program
//Camel case of a given sentence
class MyString {
	def camel_case(text: String): String = {
		var result: String = "";

		if (text != null &&
			text.length() > 0) {
			var i: Int = 0;
			while (i < text.length()) {
				if (text(i) != ' ' &&
					(i == 0 &&
						text(i) >= 'a' &&
						text(i) <= 'z') ||
					(i > 0 &&
						text(i - 1) == ' ' &&
						text(i) >= 'a' &&
						text(i) <= 'z')) {
					//When need to capitalize character
					//transformation lowercase a letter into a uppercase letter
					result += (text(i) - 32).toChar;
				} else
				if (text(i) != ' ') {
					//Add a character
					result += text(i);
				}
				i += 1;
			}
		}
		return result;
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();

		//First Test Case
		var text: String = "get employee record";
		print("Before Convert : [" + text + "]\n");
		text = obj.camel_case(text);
		print("After Convert : [" + text + "]\n");

		//Second Test Case
		text = "  we   are   student ";
		print("Before Convert : [" + text + "]\n");
		text = obj.camel_case(text);
		print("After Convert : [" + text + "]\n");

		//Third Test Case
		text = "  Get Sql Record ";
		print("Before Convert : [" + text + "]\n");
		text = obj.camel_case(text);
		print("After Convert : [" + text + "]\n");
	}
}

Output

Before Convert : [get employee record]
After Convert : [GetEmployeeRecord]
Before Convert : [  we   are   student ]
After Convert : [WeAreStudent]
Before Convert : [  Get Sql Record ]
After Convert : [GetSqlRecord]
//Swift Program
//Camel case of a given sentence

class MyString {
  func camel_case(_ text: String) -> String {
    var result: String = "";
    if (text.count > 0) {
      var i: Int = 0;
      var data = Array(text);
      var ch : String = " ";
      while (i < data.count) {
        ch = String(data[i]);
        if (ch != " " &&
          (i == 0 &&
           
            UnicodeScalar(ch)!.value  >=  UnicodeScalar("a")!.value &&
             UnicodeScalar(ch)!.value  <=  UnicodeScalar("z")!.value) ||
          (i > 0 &&
             data[i-1] == " " &&
            UnicodeScalar(ch)!.value  >=  UnicodeScalar("a")!.value &&
             UnicodeScalar(ch)!.value  <=  UnicodeScalar("z")!.value)) {
          //When need to capitalize character
          //transformation lowercase a letter into a uppercase letter
         
          result +=   String(UnicodeScalar(UInt8(UnicodeScalar(ch)!.value  - 32)));
        } else
        if (ch != " ") {
          //Add a character
          result += String(Array(text)[i]);
        }
        i += 1;
      }
    }
    return result;
  }
}
func main() {
  let obj: MyString = MyString();
  //First Test Case
  var text: String = "get employee record";
  print("Before Convert : [\(text)]");
  text = obj.camel_case(text);
  print("After Convert : [\(text)]");
  //Second Test Case
  text = "  we   are   student ";
  print("Before Convert : [\(text)]");
  text = obj.camel_case(text);
  print("After Convert : [\(text)]");
  //Third Test Case
  text = "  Get Sql Record ";
  print("Before Convert : [\(text)]");
  text = obj.camel_case(text);
  print("After Convert : [\(text)]");
}
main();

Output

Before Convert : [get employee record]
After Convert : [GetEmployeeRecord]
Before Convert : [  we   are   student ]
After Convert : [WeAreStudent]
Before Convert : [  Get Sql Record ]
After Convert : [GetSqlRecord]

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved