Capitalize first letter in word

Here given code implementation process.

//C Program 
//Capitalize first letter in word
#include <stdio.h>

void capitalize(char str[],int size)
{
  for (int i = 0; i <= size; ++i)
  {
    
    if((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 main()
{

  char str[]="I have an logic to solve this problem";
  //Get the size
  int s=sizeof(str)/sizeof(str[0]);
  
  printf("%s\n",str );
  
  capitalize(str,s-2);
  
  printf("%s\n",str );
  

  return 0;
}

Output

I have an logic to solve this problem
I Have An Logic To Solve This Problem
// C++ program
// Capitalize first letter in word
#include<iostream>

using namespace std;
class MyString {
	public:
		string capitalize(string str) {
			int size = str.size();
			string result = "";
			int i = 0;
			while (i < size) {
				if ((i == 0 &&
						str[i] >= 'a' &&
						str[i] <= 'z') ||
					(i > 0 &&
						str[i - 1] == ' ' &&
						str[i] >= 'a' &&
						str[i] <= 'z')) {
					//When get a first small letter
					result += (char)(str[i] - 32);
				} else {
					result += str[i];
				}++i;
			}
			return result;
		}
};
int main() {
	MyString obj =  MyString();
	string text = "I have an logic to solve this problem";
	cout << " Before : " << text;
	text = obj.capitalize(text);
	cout << "\n After : " << text;
	return 0;
}

Output

 Before : I have an logic to solve this problem
 After : I Have An Logic To Solve This Problem
// Java program
// Capitalize first letter in word

public class MyString {

  public String capitalize(String str)
  {
    int size = str.length();

    String result ="";

    for (int i = 0; i < size; ++i)
    {

      if( (i == 0 && str.charAt(i) >= 'a' && str.charAt(i) <= 'z') ||
          (i > 0 && str.charAt(i-1) == ' ' && str.charAt(i) >= 'a' && str.charAt(i) <='z') )
      {
        //When get a small letter
        result +=(char)(str.charAt(i) - 32);
      }
      else 
      {
         result += str.charAt(i);
      }
    }
    return result;
   
  }
  public static void main(String[] args) {

    MyString obj = new MyString();
    
    String text = "I have an logic to solve this problem";

    System.out.print(" Before : "+text);

    text = obj.capitalize(text);

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

Output

I have an logic to solve this problem
I Have An Logic To Solve This Problem
// C# program
// Capitalize first letter in word
using System;
public class MyString {
	public String capitalize(String str) {
		int size = str.Length;
		String result = "";
		for (int i = 0; i < size; ++i) {
			if ((i == 0 &&
					str[i] >= 'a' &&
					str[i] <= 'z') ||
				(i > 0 &&
					str[i - 1] == ' ' &&
					str[i] >= 'a' &&
					str[i] <= 'z')) {
				//When get a small letter
				result += (char)(str[i] - 32);
			} else {
				result += str[i];
			}
		}
		return result;
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		String text = "I have an logic to solve this problem";
		Console.Write(" Before : " + text);
		text = obj.capitalize(text);
		Console.Write("\n After : " + text);
	}
}

Output

 Before : I have an logic to solve this problem
 After : I Have An Logic To Solve This Problem
<?php
// Php program
// Capitalize first letter in word
class MyString {
	public 	function capitalize($str) {
		$size = strlen($str);
		$result = "";
		for ($i = 0; $i < $size; ++$i) {
			if (($i == 0 &&
					ord($str[$i]) >= ord('a') &&
					ord($str[$i]) <= ord('z')) ||
				($i > 0 &&
					$str[$i - 1] == ' ' &&
					ord($str[$i]) >= ord('a') &&
					ord($str[$i]) <= ord('z'))) {
				//When get a first small letter
				$result .= chr( ord($str[$i]) - 32);
			} else {
				$result .= $str[$i];
			}
		}
		return $result;
	}
}

function main() {
	$obj = new MyString();
	$text = "I have an logic to solve this problem";
	echo(" Before : ". $text);
	$text = $obj->capitalize($text);
	echo("\n After : ". $text);

}
main();

Output

 Before : I have an logic to solve this problem
 After : I Have An Logic To Solve This Problem
// Node Js program
// Capitalize first letter in word
class MyString {
	capitalize(str) {
		var size = str.length;
		var result = "";
		for (var i = 0; i < size; ++i) {
			if ((i == 0 && str.charCodeAt(i) >= 'a'.charCodeAt(0) &&
				str.charCodeAt(i) <= 'z'.charCodeAt(0)) ||
				(i > 0 &&
					str[i - 1] == ' ' &&
					str.charCodeAt(i) >= 'a'.charCodeAt(0) &&
					str.charCodeAt(i) <= 'z'.charCodeAt(0))) {
				//When get a first small letter
				result += String.fromCharCode((str.charCodeAt(i) - 32));
			} else {
				result += str[i];
			}
		}

		return result;
	}
}

function main(args) {
	var obj = new MyString();
	var text = "I have an logic to solve this problem";
	process.stdout.write(" Before : " + text);
	text = obj.capitalize(text);
	process.stdout.write("\n After : " + text);
}

main();

Output

 Before : I have an logic to solve this problem
 After : I Have An Logic To Solve This Problem
#  Python 3 program
#  Capitalize first letter in word
class MyString :
	def capitalize(self, str) :
		size = len(str)
		result = ""
		i = 0
		while (i < size) :
			if ((i == 0 and ord(str[i]) >= ord('a')
            and ord(str[i]) <= ord('z')) 
            or(i > 0 and str[i - 1] == ' '
				 and ord(str[i]) >= ord('a')
           		 and ord(str[i]) <= ord('z'))) :
				# When get a first small letter
				result += chr( ord(str[i]) - 32)
			else :
				result += str[i]
			
			i += 1
		
		return result
	

def main() :
	obj = MyString()
	text = "I have an logic to solve this problem"
	print(" Before : ", text)
	text = obj.capitalize(text)
	print(" After : ", text)


if __name__ == "__main__":
	main()

Output

 Before :  I have an logic to solve this problem
 After :  I Have An Logic To Solve This Problem
#  Ruby program
#  Capitalize first letter in word
class MyString 
	def capitalize(str) 
		size = str.length()
		result = ""
		i = 0
		while (i < size) 
			if ((i == 0 &&
					str[i].ord >= 'a'.ord &&
					str[i].ord <= 'z'.ord) ||
				(i > 0 &&
					str[i - 1] == ' ' &&
					str[i].ord >= 'a'.ord &&
					str[i].ord <= 'z'.ord)) 
				# When get a first small letter
				result += (str[i].ord - 32).chr
			else 
				result += str[i]
			end
			i += 1
		end
		return result
	end
end
def main() 
	obj = MyString.new()
	text = "I have an logic to solve this problem"
	print(" Before  : ", text)
	text = obj.capitalize(text)
	print("\n After  : ", text)
end
main()

Output

 Before  : I have an logic to solve this problem
 After  : I Have An Logic To Solve This Problem
// Scala program
// Capitalize first letter in word
class MyString {
	def capitalize(str: String): String = {
		var size: Int = str.length();
		var result: String = "";
		var i: Int = 0;
		while (i < size) {
			if ((i == 0 &&
					str(i) >= 'a' &&
					str(i) <= 'z') ||
				(i > 0 &&
					str(i - 1) == ' ' &&
					str(i) >= 'a' &&
					str(i) <= 'z')) {
				//When get a first small letter
				result += (str(i) - 32).toChar;
			} else {
				result += str(i);
			}
			i += 1;
		}
		return result;
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		var text: String = "I have an logic to solve this problem";
		print(" Before : " + text);
		text = obj.capitalize(text);
		print("\n After : " + text);
	}
}

Output

 Before : I have an logic to solve this problem
 After : I Have An Logic To Solve This Problem
// Swift program
// Capitalize first letter in word
class MyString {
	func capitalize(_ str: String) -> String {
		let size: Int = str.count;
		var result: String = "";
		var i: Int = 0;
		var data = Array(str);
      	var ch : String = " ";
		while (i < size) {
          	ch = String(data[i]);
			if ((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 get a first small letter
				result +=  String(UnicodeScalar(UInt8(UnicodeScalar(ch)!.value  - 32)));
			} else {
				result += ch;
			}
			i += 1;
		}
		return result;
	}
}
func main() {
	let obj: MyString = MyString();
	var text: String = "I have an logic to solve this problem";
	print(" Before : ", text, terminator: "");
	text = obj.capitalize(text);
	print("\n After : ", text, terminator: "");
}
main();

Output

 Before :  I have an logic to solve this problem
 After :  I Have An Logic To Solve This Problem


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