Count special characters in string

Here given code implementation process.

// Java program
// Count special characters in string
class MyString
{
	//Determine whether given character is special or not
	public boolean is_special(char data)
	{
		//Condition which is checking the following case
		//Character is alphabetic character like [a-z, A-Z]
		//Character is numeric character, like [0-9]
		//Any one condition true then given character not special
		if ((data >= 'a' && data <= 'z') 
            || (data >= 'A' && data <= 'Z') 
            || (data >= '0' && data <= '9'))
		{
			return false;
		}
		//Otherwise character is special character
		return true;
	}
	//Calculate the number of all special characters in string
	public void count_special_char(String text)
	{
		//Get the size
		int size = text.length();
		//
		int result = 0;
		//Loop which is iterating string elements
		for (int i = 0; i < size; i++)
		{
			if (is_special(text.charAt(i)) == true)
			{
				//When get a special character
				result = result + 1;
			}
		}
		//Display resultant information
		System.out.print("\nGiven String : " + text);
		System.out.print("\nSpecial characters : " + result);
	}
	public static void main(String[] args)
	{
		MyString obj = new MyString();
		obj.count_special_char("%Sorry&-this_is!wrong$methods@");
		obj.count_special_char("Are-you^ready`tojoin007");
	}
}

Output

Given String : %Sorry&-this_is!wrong$methods@
Special characters : 7
Given String : Are-you^ready`tojoin007
Special characters : 3
//Include header file
#include <iostream>
#include<string.h>
using namespace std;

// C++ program
// Count special characters in string
class MyString
{
	public:
		//Determine whether given character is special or not
		bool is_special(char data)
		{
			//Condition which is checking the following case
			//Character is alphabetic character like [a-z, A-Z]
			//Character is numeric character, like [0-9]
			//Any one condition true then given character not special
			if ((data >= 'a' && data <= 'z') 
                || (data >= 'A' && data <= 'Z') 
                || (data >= '0' && data <= '9'))
			{
				return false;
			}
			//Otherwise character is special character
			return true;
		}
	//Calculate the number of all special characters in string
	void count_special_char(string text)
	{
		//Get the size
		int size = text.size();
		//
		int result = 0;
		//Loop which is iterating string elements
		for (int i = 0; i < size; i++)
		{
			if (this->is_special(text[i]) == true)
			{
				//When get a special character
				result = result + 1;
			}
		}
		//Display resultant information
		cout << "\nGiven String : " << text;
		cout << "\nSpecial characters : " << result;
	}
};
int main()
{
	MyString obj = MyString();
	obj.count_special_char("%Sorry&-this_is!wrong$methods@");
	obj.count_special_char("Are-you^ready`tojoin007");
	return 0;
}

Output

Given String : %Sorry&-this_is!wrong$methods@
Special characters : 7
Given String : Are-you^ready`tojoin007
Special characters : 3
//Include namespace system
using System;

// C# program
// Count special characters in string
class MyString
{
	//Determine whether given character is special or not
	public Boolean is_special(char data)
	{
		//Condition which is checking the following case
		//Character is alphabetic character like [a-z, A-Z]
		//Character is numeric character, like [0-9]
		//Any one condition true then given character not special
		if ((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || (data >= '0' && data <= '9'))
		{
			return false;
		}
		//Otherwise character is special character
		return true;
	}
	//Calculate the number of all special characters in string
	public void count_special_char(String text)
	{
		//Get the size
		int size = text.Length;
		//
		int result = 0;
		//Loop which is iterating string elements
		for (int i = 0; i < size; i++)
		{
			if (is_special(text[i]) == true)
			{
				//When get a special character
				result = result + 1;
			}
		}
		//Display resultant information
		Console.Write("\nGiven String : " + text);
		Console.Write("\nSpecial characters : " + result);
	}
	public static void Main(String[] args)
	{
		MyString obj = new MyString();
		obj.count_special_char("%Sorry&-this_is!wrong$methods@");
		obj.count_special_char("Are-you^ready`tojoin007");
	}
}

Output

Given String : %Sorry&-this_is!wrong$methods@
Special characters : 7
Given String : Are-you^ready`tojoin007
Special characters : 3
<?php
// Php program
// Count special characters in string
class MyString
{
	//Determine whether given character is special or not
	public	function is_special($data)
	{
		//Condition which is checking the following case
		//Character is alphabetic character like [a-z, A-Z]
		//Character is numeric character, like [0-9]
		//Any one condition true then given character not special
		if ((ord($data) >= ord('a') && ord($data) <= ord('z')) 
            || (ord($data) >= ord('A') && ord($data) <= ord('Z')) 
            || (ord($data) >= ord('0') && ord($data) <= ord('9')))
		{
			return false;
		}
		//Otherwise character is special character
		return true;
	}
	//Calculate the number of all special characters in string
	public	function count_special_char($text)
	{
		//Get the size
		$size = strlen($text);
		//
		$result = 0;
		//Loop which is iterating string elements
		for ($i = 0; $i < $size; $i++)
		{
			if ($this->is_special($text[$i]) == true)
			{
				//When get a special character
				$result = $result + 1;
			}
		}
		echo "\nGiven String : ". $text;
		echo "\nSpecial characters : ". $result;
	}
}

function main()
{
	$obj = new MyString();
	$obj->count_special_char("%Sorry&-this_is!wrong\$methods@");
	$obj->count_special_char("Are-you^ready`tojoin007");
}
main();

Output

Given String : %Sorry&-this_is!wrong$methods@
Special characters : 7
Given String : Are-you^ready`tojoin007
Special characters : 3
// Node Js program
// Count special characters in string
class MyString
{
	//Determine whether given character is special or not
	is_special(data)
	{
		//Condition which is checking the following case
		//Character is alphabetic character like [a-z, A-Z]
		//Character is numeric character, like [0-9]
		//Any one condition true then given character not special
		if (((data).charCodeAt(0) >= ('a').charCodeAt(0) && (data).charCodeAt(0) <= ('z').charCodeAt(0)) 
            || ((data).charCodeAt(0) >= ('A').charCodeAt(0) && (data).charCodeAt(0) <= ('Z').charCodeAt(0)) 
            || ((data).charCodeAt(0) >= ('0').charCodeAt(0) && (data).charCodeAt(0) <= ('9').charCodeAt(0)))
		{
			return false;
		}
		//Otherwise character is special character
		return true;
	}
	//Calculate the number of all special characters in string
	count_special_char(text)
	{
		//Get the size
		var size = text.length;
		//
		var result = 0;
		//Loop which is iterating string elements
		for (var i = 0; i < size; i++)
		{
			if (this.is_special(text[i]) == true)
			{
				//When get a special character
				result = result + 1;
			}
		}
		process.stdout.write("\nGiven String : " + text);
		process.stdout.write("\nSpecial characters : " + result);
	}
}

function main()
{
	var obj = new MyString();
	obj.count_special_char("%Sorry&-this_is!wrong$methods@");
	obj.count_special_char("Are-you^ready`tojoin007");
}
main();

Output

Given String : %Sorry&-this_is!wrong$methods@
Special characters : 7
Given String : Are-you^ready`tojoin007
Special characters : 3
#  Python 3 program
#  Count special characters in string
class MyString :
	# Determine whether given character is special or not
	def is_special(self, data) :
		# Condition which is checking the following case
		# Character is alphabetic character like [a-z, A-Z]
		# Character is numeric character, like [0-9]
		# Any one condition true then given character not special
		if ((ord(data) >= ord('a') and ord(data) <= ord('z')) 
          or (ord(data) >= ord('A') and ord(data) <= ord('Z')) 
          or (ord(data) >= ord('0') and ord(data) <= ord('9'))) :
			return False
		
		# Otherwise character is special character
		return True
	
	# Calculate the number of all special characters in string
	def count_special_char(self, text) :
		# Get the size
		size = len(text)
		# 
		result = 0
		# Loop which is iterating string elements
		i = 0
		while (i < size) :
			if (self.is_special(text[i]) == True) :
				# When get a special character
				result = result + 1
			
			i += 1
		
		print("\nGiven String : ", text, end = "")
		print("\nSpecial characters : ", result, end = "")
	

def main() :
	obj = MyString()
	obj.count_special_char("%Sorry&-this_is!wrong$methods@")
	obj.count_special_char("Are-you^ready`tojoin007")

if __name__ == "__main__": main()

Output

Given String :  %Sorry&-this_is!wrong$methods@
Special characters :  7
Given String :  Are-you^ready`tojoin007
Special characters :  3
#  Ruby program
#  Count special characters in string
class MyString

	# Determine whether given character is special or not
	def is_special(data)
	
		# Condition which is checking the following case
		# Character is alphabetic character like [a-z, A-Z]
		# Character is numeric character, like [0-9]
		# Any one condition true then given character not special
		if (((data).ord >= ('a').ord && (data).ord <= ('z').ord) || 
            ((data).ord >= ('A').ord && (data).ord <= ('Z').ord) || 
            ((data).ord >= ('0').ord && (data).ord <= ('9').ord))
		
			return false
		end
		# Otherwise character is special character
		return true
	end
	# Calculate the number of all special characters in string
	def count_special_char(text)
	
		# Get the size
		size = text.length()
		# 
		result = 0
		# Loop which is iterating string elements
		i = 0
		while (i < size)
		
			if (self.is_special(text[i]) == true)
			
				# When get a special character
				result = result + 1
			end
			i += 1
		end
		# Display resultant information
		print("\nGiven String : ", text)
		print("\nSpecial characters : ", result)
	end
end
def main()

	obj = MyString.new()
	obj.count_special_char("%Sorry&-this_is!wrong$methods@")
	obj.count_special_char("Are-you^ready`tojoin007")
end
main()

Output

Given String : %Sorry&-this_is!wrong$methods@
Special characters : 7
Given String : Are-you^ready`tojoin007
Special characters : 3
// Scala program
// Count special characters in string
class MyString
{
	//Determine whether given character is special or not
	def is_special(data: Char): Boolean = {
		//Condition which is checking the following case
		//Character is alphabetic character like [a-z, A-Z]
		//Character is numeric character, like [0-9]
		//Any one condition true then given character not special
		if ((data >= 'a' && data <= 'z') 
      		|| (data >= 'A' && data <= 'Z') 
      		|| (data >= '0' && data <= '9'))
		{
			return false;
		}
		//Otherwise character is special character
		return true;
	}
	//Calculate the number of all special characters in string
	def count_special_char(text: String): Unit = {
		//Get the size
		var size: Int = text.length();
		//
		var result: Int = 0;
		//Loop which is iterating string elements
		var i: Int = 0;
		while (i < size)
		{
			if (is_special(text(i)) == true)
			{
				//When get a special character
				result = result + 1;
			}
			i += 1;
		}
		//Display resultant information
		print("\nGiven String : " + text);
		print("\nSpecial characters : " + result);
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		obj.count_special_char("%Sorry&-this_is!wrong$methods@");
		obj.count_special_char("Are-you^ready`tojoin007");
	}
}

Output

Given String : %Sorry&-this_is!wrong$methods@
Special characters : 7
Given String : Are-you^ready`tojoin007
Special characters : 3
// Swift program
// Count special characters in string
class MyString
{
	//Determine whether given character is special or not
	func is_special(_ data: Character) -> Bool
	{
		//Condition which is checking the following case
		//Character is alphabetic character like [a-z, A-Z]
		//Character is numeric character, like [0-9]
		//Any one condition true then given character not special
		if ((data >= "a" && data <= "z") 
            || (data >= "A" && data <= "Z") 
            || (data >= "0" && data <= "9"))
		{
			return false;
		}
		//Otherwise character is special character
		return true;
	}
	//Calculate the number of all special characters in string
	func count_special_char(_ data: String)
	{	
      	let text = Array(data);
		//Get the size
		let size: Int = text.count;
		//
		var result: Int = 0;
		//Loop which is iterating string elements
		var i: Int = 0;
		while (i < size)
		{
			if (self.is_special(text[i]) == true)
			{
				//When get a special character
				result = result + 1;
			}
			i += 1;
		}
		print("\nGiven String : ", data, terminator: "");
		print("\nSpecial characters : ", result, terminator: "");
	}
}
func main()
{
	let obj: MyString = MyString();
	obj.count_special_char("%Sorry&-this_is!wrong$methods@");
	obj.count_special_char("Are-you^ready`tojoin007");
}
main();

Output

Given String :  %Sorry&-this_is!wrong$methods@
Special characters :  7
Given String :  Are-you^ready`tojoin007
Special characters :  3


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