Remove numbers from string

Here given code implementation process.

//C Program
//Remove numbers from string
#include <stdio.h>

void replace(char str[],int size)
{
  
  if(size < 1) 
  {
    return;
  }

  int x = 0;

  printf("\nBefore : %s\n",str );
  
  for (int i = 1; i < size; ++i)
  {
    if( !(str[i]>='0' && str[i]<='9') )
    {
      //When get a character
      str[x]=str[i];
      x++;
    }
  }
  //Set the remain characters
  for (int i = x; i < size; ++i)
  {
    str[i]=' ';
  }
  printf("After : %s\n",str );
}

int main()
{

  char str1[]="0A1B82CD7E124F2G71";

  int size=sizeof(str1)/sizeof(str1[0])-1;

  replace(str1,size); //ABCDEFG


  char str2[]="4626AB-32CD";

  size=sizeof(str2)/sizeof(str2[0])-1;
  
  replace(str2,size); //AB-CD        
  return 0;
}

Output

Before : 0A1B82CD7E124F2G71
After : ABCDEFG

Before : 4626AB-32CD
After : AB-CD
// Java program
// Remove numbers from string

public class MyString {
  
  public String remove_number(String str) 
  {

    int size = str.length();
    
    if (size < 1) 
    {
      return str;
    }
    
    int x = 0;

    String result = "";

    System.out.print("\nBefore : ["+str+"]\n" );
    
    for (int i = 1; i < size; ++i) 
    {
      
      if (!(str.charAt(i) >= '0' && str.charAt(i) <= '9')) 
      {
        //When get a character
        result += str.charAt(i);
        x++;
      }
    }
    return result;
   
    
  }
  public static void main(String[] args) 
  {

    MyString obj = new MyString();

    String text = "0A1B82CD7E124F2G71";

    text=obj.remove_number(text);

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


    text = "4626AB-32CD";

    text=obj.remove_number(text);

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

  }
}

Output

Before : 0A1B82CD7E124F2G71
After : ABCDEFG

Before : 4626AB-32CD
After : AB-CD
// C++ program
// Remove numbers from string
#include<iostream>

using namespace std;
class MyString {
	public:
		string remove_number(string str) {
			int size = str.size();
			if (size < 1) {
				return str;
			}
			int x = 0;
			string result = "";
			cout << "\nBefore : [" << str << "]\n";
			for (int i = 1; i < size; ++i) {
				if (!(str[i] >= '0' && str[i] <= '9')) {
					//When get a character
					result += str[i];
					x++;
				}
			}
			return result;
		}
};
int main() {
	MyString obj =  MyString();
	string text = "0A1B82CD7E124F2G71";
	text = obj.remove_number(text);
	cout << "After : [" << text << "] \n";
	text = "4626AB-32CD";
	text = obj.remove_number(text);
	cout << "After : [" << text << "] \n";
	return 0;
}

Output

Before : [0A1B82CD7E124F2G71]
After : [ABCDEFG]

Before : [4626AB-32CD]
After : [AB-CD]
// C# program
// Remove numbers from string
using System;
public class MyString {
	public String remove_number(String str) {
		int size = str.Length;
		if (size < 1) {
			return str;
		}
		int x = 0;
		String result = "";
		Console.Write("\nBefore : [" + str + "]\n");
		for (int i = 1; i < size; ++i) {
			if (!(str[i] >= '0' &&
					str[i] <= '9')) {
				//When get a character
				result += str[i];
				x++;
			}
		}
		return result;
	}
	public static void Main(String[] args) {
		MyString obj = new MyString();
		String text = "0A1B82CD7E124F2G71";
		text = obj.remove_number(text);
		Console.Write("After : [" + text + "] \n");
		text = "4626AB-32CD";
		text = obj.remove_number(text);
		Console.Write("After : [" + text + "] \n");
	}
}

Output

Before : [0A1B82CD7E124F2G71]
After : [ABCDEFG]

Before : [4626AB-32CD]
After : [AB-CD]
<?php
// Php program
// Remove numbers from string
class MyString {
	public 	function remove_number($str) {
		$size = strlen($str);
		if ($size < 1) {
			return $str;
		}
		$x = 0;
		$result = "";
		echo("\nBefore : [". $str ."]\n");
		for ($i = 1; $i < $size; ++$i) {
			if (!(ord($str[$i]) >= ord('0') &&
					ord($str[$i]) <= ord('9'))) {
				//When get a character
				$result .= $str[$i];
				$x++;
			}
		}
		return $result;
	}
}

function main() {
	$obj = new MyString();
	$text = "0A1B82CD7E124F2G71";
	$text = $obj->remove_number($text);
	echo("After : [". $text ."] \n");
	$text = "4626AB-32CD";
	$text = $obj->remove_number($text);
	echo("After : [". $text ."] \n");

}
main();

Output

Before : [0A1B82CD7E124F2G71]
After : [ABCDEFG]

Before : [4626AB-32CD]
After : [AB-CD]
// Node Js program
// Remove numbers from string
class MyString {
	remove_number(str) {
		var size = str.length;
		if (size < 1) {
			return str;
		}
		var x = 0;
		var result = "";
		process.stdout.write("\nBefore : [" + str + "]\n");
		for (var i = 1; i < size; ++i) {
			if (!((str[i]).charCodeAt(0) >= ('0').charCodeAt(0) &&
					(str[i]).charCodeAt(0) <= ('9').charCodeAt(0))) {
				//When get a character
				result += str[i];
				x++;
			}
		}

		return result;
	}
}

function main(args) {
	var obj = new MyString();
	var text = "0A1B82CD7E124F2G71";
	text = obj.remove_number(text);
	process.stdout.write("After : [" + text + "] \n");
	text = "4626AB-32CD";
	text = obj.remove_number(text);
	process.stdout.write("After : [" + text + "] \n");
}

main();

Output

Before : [0A1B82CD7E124F2G71]
After : [ABCDEFG]

Before : [4626AB-32CD]
After : [AB-CD]
#  Python 3 program
#  Remove numbers from string
class MyString :
	def remove_number(self, str) :
		size = len(str)
		if (size < 1) :
			return str
		
		x = 0
		result = ""
		print("\nBefore : [{}]\n".format(str))
		i = 1
		while (i < size) :
			if (not (ord(str[i]) >= ord('0') and ord(str[i]) <= ord('9'))) :
				# When get a character
				result += str[i]
				x += 1
			
			i += 1
		
		return result
	

def main() :
	obj = MyString()
	text = "0A1B82CD7E124F2G71"
	text = obj.remove_number(text)
	print("After : [{}] ".format(text))
	text = "4626AB-32CD"
	text = obj.remove_number(text)
	print("After : [{}]".format(text))


if __name__ == "__main__":
	main()

Output

Before : [0A1B82CD7E124F2G71]

After : [ABCDEFG]

Before : [4626AB-32CD]

After : [AB-CD]
#  Ruby program
#  Remove numbers from string
class MyString 
	def remove_number(str) 
		size = str.length()
		if (size < 1) 
			return str
		end
		x = 0
		result = ""
		print("\nBefore  :[", str ,"]\n")
		i = 1
		while (i < size) 
			if (!((str[i]).ord >= ('0').ord &&
					(str[i]).ord <= ('9').ord)) 
				# When get a character
				result += str[i]
				x += 1
			end
			i += 1
		end
		return result
	end
end
def main() 
	obj = MyString.new()
	text = "0A1B82CD7E124F2G71"
	text = obj.remove_number(text)
	print("After  :[", text ,"] \n")
	text = "4626AB-32CD"
	text = obj.remove_number(text)
	print("After  :[", text ,"] \n")
end
main()

Output

Before  :[0A1B82CD7E124F2G71]
After  :[ABCDEFG] 

Before  :[4626AB-32CD]
After  :[AB-CD] 
// Scala program
// Remove numbers from string
class MyString {
	def remove_number(str: String): String = {
		var size: Int = str.length();

		if (size < 1) {
			return str;
		}
		var x: Int = 0;
		var result: String = "";
		print("\nBefore : [" + str + "]\n");
		var i: Int = 1;
		while (i < size) {
			if (!(str(i) >= '0' &&
					str(i) <= '9')) {
				//When get a character
				result += str(i);
				x += 1;
			}
			i += 1;
		}
		return result;
	}
}
object Main {
	def main(args: Array[String]): Unit = {
		var obj: MyString = new MyString();
		var text: String = "0A1B82CD7E124F2G71";
		text = obj.remove_number(text);
		print("After : [" + text + "] \n");
		text = "4626AB-32CD";
		text = obj.remove_number(text);
		print("After : [" + text + "] \n");
	}
}

Output

Before : [0A1B82CD7E124F2G71]
After : [ABCDEFG]

Before : [4626AB-32CD]
After : [AB-CD]
// Swift program
// Remove numbers from string
class MyString {
	func remove_number(_ data: String) -> String {
      	var str = Array(data);
		let size: Int = str.count;
		if (size < 1) {
			return data;
		}
		var x: Int = 0;
		var result: String = "";
      	print("\nBefore : [\(data)]");
		var i: Int = 1;
		while (i < size) {
			if (!(str[i] >= "0" &&
					str[i] <= "9")) {
				//When get a character
				result += String(str[i]);
				x += 1;
			}
			i += 1;
		}
		return result;
	}
}
func main() {
	let obj: MyString = MyString();
	var text: String = "0A1B82CD7E124F2G71";
	text = obj.remove_number(text);
	print("After : [\(text)] ");
	text = "4626AB-32CD";
	text = obj.remove_number(text);
	print("After : [\(text)] ");
}
main();

Output

Before : [0A1B82CD7E124F2G71]
After : [ABCDEFG]

Before : [4626AB-32CD]
After : [AB-CD]

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