Find number of uppercase and lowercase letters in a string

Here given code implementation process.

// C Program
// Count number of uppercase and lowercase letters in a sentence
#include <stdio.h>


void count_letters(char str[],int size)
{
  
  int lowercase=0,uppercase=0;

  for (int i = 0; i < size; ++i)
  {
    if(str[i] >= 'a' && str[i] <= 'z')
    {
      lowercase++;
    }
    else if(str[i] >= 'A' && str[i] <= 'Z')
    {
      uppercase++;
    }
  }
  //Display Result
  printf("String : %s\n",str );
  printf("Lowercase : %d\n",lowercase );
  printf("Uppercase : %d\n",uppercase );

}
int main()
{

  char str1[]="This Is simple TEXT";
  //Get the size
  int size = sizeof(str1)/sizeof(str1[0])-1;
  
  count_letters(str1,size);


  char str2[]="DeTectThisLanGuages";
  //Get the size
  size = sizeof(str2)/sizeof(str2[0])-1;
  
  count_letters(str2,size);
  return 0;
}

Output

String : This Is simple TEXT
Lowercase : 10
Uppercase : 6
String : DeTectThisLanGuages
Lowercase : 14
Uppercase : 5
// Java program
// Count number of uppercase and lowercase letters in a sentence

public class MyString {
  
  public void count_letters(String str)
  {
    int size = str.length();

    int lowercase=0,uppercase=0;

    for (int i = 0; i < size; ++i)
    {
      if(str.charAt(i) >= 'a' && str.charAt(i) <= 'z')
      {
        lowercase++;
      }
      else if(str.charAt(i) >= 'A' && str.charAt(i) <= 'Z')
      {
        uppercase++;
      }
    }
    //Display Result
    System.out.print("String :  "+str+"\n");
    System.out.print("Lowercase : "+lowercase+"\n" );
    System.out.print("Uppercase : "+uppercase+"\n" );

  }
  public static void main(String[] args) 
  {

    MyString obj = new MyString();

    obj.count_letters("This Is simple TEXT");

    obj.count_letters("DeTectThisLanGuages");
  }
}

Output

String : This Is simple TEXT
Lowercase : 10
Uppercase : 6
String : DeTectThisLanGuages
Lowercase : 14
Uppercase : 5
// C++ program
// Count number of uppercase and lowercase letters in a sentence
#include<iostream>

using namespace std;
class MyString {
	public:
    void count_letters(string str) {
      int size = str.size();
      int lowercase = 0, uppercase = 0;
      for (int i = 0; i < size; ++i) {
        if (str[i] >= 'a' &&
            str[i] <= 'z') {
          lowercase++;
        } else
          if (str[i] >= 'A' &&
              str[i] <= 'Z') {
            uppercase++;
          }
      }
      cout << "String : " << str << "\n";
      cout << "Lowercase : " << lowercase << "\n";
      cout << "Uppercase : " << uppercase << "\n";
    }
};
int main() {
	MyString obj =  MyString();
	obj.count_letters("This Is simple TEXT");
	obj.count_letters("DeTectThisLanGuages");
	return 0;
}

Output

String : This Is simple TEXT
Lowercase : 10
Uppercase : 6
String : DeTectThisLanGuages
Lowercase : 14
Uppercase : 5
// C# program
// Count number of uppercase and lowercase letters in a sentence
using System;
public class MyString {
	public void count_letters(String str) {
		int size = str.Length;
		int lowercase = 0, uppercase = 0;
		for (int i = 0; i < size; ++i) {
			if (str[i] >= 'a' &&
				str[i] <= 'z') {
				lowercase++;
			} else
			if (str[i] >= 'A' &&
				str[i] <= 'Z') {
				uppercase++;
			}
		}
		Console.Write("String : " + str + "\n");
		Console.Write("Lowercase : " + lowercase + "\n");
		Console.Write("Uppercase : " + uppercase + "\n");
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		obj.count_letters("This Is simple TEXT");
		obj.count_letters("DeTectThisLanGuages");
	}
}

Output

String : This Is simple TEXT
Lowercase : 10
Uppercase : 6
String : DeTectThisLanGuages
Lowercase : 14
Uppercase : 5
<?php
// Php program
// Count number of uppercase and lowercase letters in a sentence
class MyString {
	public 	function count_letters($str) {
		$size = strlen($str);
		$lowercase = 0;
		$uppercase = 0;
		for ($i = 0; $i < $size; ++$i) {
			if (ord($str[$i]) >= ord('a') &&
				ord($str[$i]) <= ord('z')) {
				$lowercase++;
			} else
			if (ord($str[$i]) >= ord('A') &&
				ord($str[$i]) <= ord('Z')) {
				$uppercase++;
			}
		}
		//Display Result

		echo("String : ". $str ."\n");
		echo("Lowercase : ". $lowercase ."\n");
		echo("Uppercase : ". $uppercase ."\n");
	}
}

function main() {
	$obj = new MyString();
	$obj->count_letters("This Is simple TEXT");
	$obj->count_letters("DeTectThisLanGuages");

}
main();

Output

String : This Is simple TEXT
Lowercase : 10
Uppercase : 6
String : DeTectThisLanGuages
Lowercase : 14
Uppercase : 5
// Node Js program
// Count number of uppercase and lowercase letters in a sentence
class MyString {
	count_letters(str) {
		var size = str.length;
		var lowercase = 0;
		var uppercase = 0;
		for (var i = 0; i < size; ++i) {
			if ((str[i]).charCodeAt(0) >= ('a').charCodeAt(0) &&
				(str[i]).charCodeAt(0) <= ('z').charCodeAt(0)) {
				lowercase++;
			} else
			if ((str[i]).charCodeAt(0) >= ('A').charCodeAt(0) &&
				(str[i]).charCodeAt(0) <= ('Z').charCodeAt(0)) {
				uppercase++;
			}
		}

		//Display Result

		process.stdout.write("String : " + str + "\n");
		process.stdout.write("Lowercase : " + lowercase + "\n");
		process.stdout.write("Uppercase : " + uppercase + "\n");
	}
}

function main(args) {
	var obj = new MyString();
	obj.count_letters("This Is simple TEXT");
	obj.count_letters("DeTectThisLanGuages");
}

main();

Output

String : This Is simple TEXT
Lowercase : 10
Uppercase : 6
String : DeTectThisLanGuages
Lowercase : 14
Uppercase : 5
#  Python 3 program
#  Count number of uppercase and lowercase letters in a sentence
class MyString :
	def count_letters(self, str) :
		size = len(str)
		lowercase = 0
		uppercase = 0
		i = 0
		while (i < size) :
			if (ord(str[i]) >= ord('a') and ord(str[i]) <= ord('z')) :
				lowercase += 1
			elif (ord(str[i]) >= ord('A') and ord(str[i]) <= ord('Z')) :
				uppercase += 1
			
			i += 1
		
		print("String : ", str ,"\n", end = "")
		print("Lowercase : ", lowercase ,"\n", end = "")
		print("Uppercase : ", uppercase ,"\n", end = "")
	

def main() :
	obj = MyString()
	obj.count_letters("This Is simple TEXT")
	obj.count_letters("DeTectThisLanGuages")


if __name__ == "__main__":
	main()

Output

String :  This Is simple TEXT
Lowercase :  10
Uppercase :  6
String :  DeTectThisLanGuages
Lowercase :  14
Uppercase :  5
#  Ruby program
#  Count number of uppercase and lowercase letters in a sentence
class MyString 
	def count_letters(str) 
		size = str.length()
		lowercase = 0
		uppercase = 0
		i = 0
		while (i < size) 
			if ((str[i]).ord >= ('a').ord &&
				(str[i]).ord <= ('z').ord) 
				lowercase += 1
			elsif ((str[i]).ord >= ('A').ord &&
				(str[i]).ord <= ('Z').ord) 
				uppercase += 1
			end
			i += 1
		end
		print("String  : ", str ,"\n")
		print("Lowercase  : ", lowercase ,"\n")
		print("Uppercase  : ", uppercase ,"\n")
	end
end
def main() 
	obj = MyString.new()
	obj.count_letters("This Is simple TEXT")
	obj.count_letters("DeTectThisLanGuages")
end
main()

Output

String  : This Is simple TEXT
Lowercase  : 10
Uppercase  : 6
String  : DeTectThisLanGuages
Lowercase  : 14
Uppercase  : 5
// Scala program
// Count number of uppercase and lowercase letters in a sentence
class MyString {
	def count_letters(str: String): Unit = {
		var size: Int = str.length();
		var lowercase: Int = 0;
		var uppercase: Int = 0;
		var i: Int = 0;
		while (i < size) {
			if (str(i) >= 'a' &&
				str(i) <= 'z') {
				lowercase += 1;
			} else
			if (str(i) >= 'A' &&
				str(i) <= 'Z') {
				uppercase += 1;
			}
			i += 1;
		}
		print("String : " + str + "\n");
		print("Lowercase : " + lowercase + "\n");
		print("Uppercase : " + uppercase + "\n");
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		obj.count_letters("This Is simple TEXT");
		obj.count_letters("DeTectThisLanGuages");
	}
}

Output

String : This Is simple TEXT
Lowercase : 10
Uppercase : 6
String : DeTectThisLanGuages
Lowercase : 14
Uppercase : 5
// Swift program
// Count number of uppercase and lowercase letters in a sentence
class MyString {
	func count_letters(_ data: String) {
      	var str = Array(data);
		let size: Int = str.count;
		var lowercase: Int = 0;
		var uppercase: Int = 0;
		var i: Int = 0;
		while (i < size) {
			if (str[i] >= "a" &&
				str[i] <= "z") {
				lowercase += 1;
			} else
			if (str[i] >= "A" &&
				str[i] <= "Z") {
				uppercase += 1;
			}
			i += 1;
		}
		print("String : ", data ,"\n", terminator: "");
		print("Lowercase : ", lowercase ,"\n", terminator: "");
		print("Uppercase : ", uppercase ,"\n", terminator: "");
	}
}
func main() {
	let obj: MyString = MyString();
	obj.count_letters("This Is simple TEXT");
	obj.count_letters("DeTectThisLanGuages");
}
main();

Output

String :  This Is simple TEXT
Lowercase :  10
Uppercase :  6
String :  DeTectThisLanGuages
Lowercase :  14
Uppercase :  5


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