Capitalize each word in string

Here given code implementation process.

//C Program 
//Capitalize each word in string
#include <stdio.h>

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

    if(str[i]>='a' && str[i] <='z')
    {
      //When get a small letter
      str[i] = str[i] - 32;
    }
  }
 
}
int main()
{
  //Given Text String
  char str[]="all simple code test";

  int s = sizeof(str)/sizeof(str[0]);

  printf("%s\n",str );
  capitalize(str,s-2);
  printf("%s\n",str );
  

  return 0;
}

Output

all simple code test
ALL SIMPLE CODE TEST
//Java program
//Capitalize each word in string

public class MyString {

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

    String result ="";

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

      if(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 = "all simple code test";

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

    text = obj.capitalize(text);

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

Output

all simple code test
ALL SIMPLE CODE TEST
//C++ program
//Capitalize each word in string
#include<iostream>

using namespace std;
class MyString {
	public:
		string capitalize(string str) {
			int size = str.size();
			string result = "";
			for (int i = 0; i < size; ++i) {
				if (str[i] >= 'a' &&
					str[i] <= 'z') {
					//When get a small letter
					result += (char)(str[i] - 32);
				} else {
					result += str[i];
				}
			}
			return result;
		}
};
int main() {
	MyString obj =  MyString();
	string text = "all simple code test";
	cout << " Before : " << text;
	text = obj.capitalize(text);
	cout << "\n After : " << text;
	return 0;
}

Output

 Before : all simple code test
 After : ALL SIMPLE CODE TEST
//C# program
//Capitalize each word in string
using System;
public class MyString {
	public String capitalize(String str) {
		int size = str.Length;
		String result = "";
		for (int i = 0; i < size; ++i) {
			if (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 = "all simple code test";
		Console.Write(" Before : " + text);
		text = obj.capitalize(text);
		Console.Write("\n After : " + text);
	}
}

Output

 Before : all simple code test
 After : ALL SIMPLE CODE TEST
<?php
//Php program
//Capitalize each word in string
class MyString {
	public 	function capitalize($str) {
		$size = strlen($str);
		$result = "";
		for ($i = 0; $i < $size; ++$i) {
			if ( ord($str[$i]) >=  ord('a') &&
				 ord($str[$i]) <=  ord('z')) {
				//When get a small letter
				$result .= chr( ord($str[$i]) - 32);
			} else {
				$result .= $str[$i];
			}
		}
		return $result;
	}
}

function main() {
	$obj = new MyString();
	$text = "all simple code test";
	echo(" Before : ". $text);
	$text = $obj->capitalize($text);
	echo("\n After : ". $text);

}
main();

Output

 Before : all simple code test
 After : ALL SIMPLE CODE TEST
//Node Js program
//Capitalize each word in string
class MyString {
	capitalize(str) {
		var size = str.length;
		var result = "";
		for (var i = 0; i < size; ++i) {
			if (str.charCodeAt(i) >= 'a'.charCodeAt(0) &&
				str.charCodeAt(i) <= 'z'.charCodeAt(0)) {
				//When get a small letter
				result += String.fromCharCode((str.charCodeAt(i) - 32));
			} else {
				result += str[i];
			}
		}

		return result;
	}
}

function main(args) {
	var obj = new MyString();
	var text = "all simple code test";
	process.stdout.write(" Before : " + text);
	text = obj.capitalize(text);
	process.stdout.write("\n After : " + text);
}

main();

Output

 Before : all simple code test
 After : ALL SIMPLE CODE TEST
# Python 3 program
# Capitalize each word in string
class MyString :
	def capitalize(self, str) :
		size = len(str)
		result = ""
		i = 0
		while (i < size) :
			if (ord(str[i]) >= ord('a')
            and ord(str[i]) <= ord('z')) :
				# When get a small letter
				result += chr( ord(str[i]) - 32)
			else :
				result += str[i]
			
			i += 1
		
		return result
	

def main() :
	obj = MyString()
	text = "all simple code test"
	print(" Before : ", text, end = "")
	text = obj.capitalize(text)
	print("\n After : ", text, end = "")


if __name__ == "__main__":
	main()

Output

 Before :  all simple code test
 After :  ALL SIMPLE CODE TEST
# Ruby program
# Capitalize each word in string
class MyString 
	def capitalize(str) 
		size = str.length()
		result = ""
		i = 0
		while (i < size) 
			if (str[i].ord >= 'a'.ord &&
				str[i].ord <= 'z'.ord) 
				# When get a 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 = "all simple code test"
	print(" Before : ", text)
	text = obj.capitalize(text)
	print("\n After : ", text)
end
main()

Output

 Before : all simple code test
 After : ALL SIMPLE CODE TEST
//Scala program
//Capitalize each word in string
class MyString {
	def capitalize(str: String): String = {
		var size: Int = str.length();
		var result: String = "";
		var i: Int = 0;
		while (i < size) {
			if (str(i) >= 'a' &&
				str(i) <= 'z') {
				//When get a 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 = "all simple code test";
		print(" Before : " + text);
		text = obj.capitalize(text);
		print("\n After : " + text);
	}
}

Output

 Before : all simple code test
 After : ALL SIMPLE CODE TEST
//Swift program
//Capitalize each word in string
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 (UnicodeScalar(ch)!.value  >=  UnicodeScalar("a")!.value &&
             UnicodeScalar(ch)!.value  <=  UnicodeScalar("z")!.value) {
				//When get a 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 = "all simple code test";
	print(" Before : ", text, terminator: "");
	text = obj.capitalize(text);
	print("\n After : ", text, terminator: "");
}
main();

Output

 Before :  all simple code test
 After :  ALL SIMPLE CODE TEST

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