Description
To remove given from a string, scan string from start and put space character
in place of this numbers and print string ignoring space character.
Example Remove 5 and 76 from gr5kj376er.
To remove 5 and 76 from a string, scan string from start and put space character
in place of this numbers and print string ignoring space character.
C/C++
/* C program to remove 5 and 76 from a string */
//Save it as RemoveParticularNumber.c
#include<stdio.h>
#include<string.h>
int main(){
char str[100];
char str2[100] = "";
int i, len;
printf("Enter a string : ");
scanf("%s",str);
len = strlen(str);
for(i=0;i<len;i++){
if(str[i] == '5'){
str[i] = ' ';
}
if(str[i] == '7'){
if(str[i+1] == '6'){
str[i] = ' ';
str[i+1] = ' ';
}
}
}
for(i=0;i<len;i++){
if(str[i] == ' '){
continue;
}
else{
strncat(str2,&str[i],1);
}
}
printf("\nString after removal of 5 and 76 : %s",str2);
return 0;
}
Input:
Enter a string : gr5kj376er
Output:
String after removal of 5 and 76 : grkj3er
Java
/* Java program to remove 5 and 76 from a string */
//Save it as RemoveParticularNumber.java
import java.io.*;
import java.util.Scanner;
public class RemoveParticularNumber {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Enter a string : ");
String str = scanner.nextLine();
String str2= "";
char[] charStr = str.toCharArray();
int i, len;
len= str.length();
for(i=0;i<len;i++) {
if(charStr[i] == '5') {
charStr[i] = ' ';
}
if(charStr[i] == '7') {
if(charStr[i+1] == '6') {
charStr[i] = ' ';
charStr[i+1] = ' ';
}
}
}
for(i=0;i<len;i++){
if(charStr[i] == ' '){
continue;
}
else{
str2 = str2 + charStr[i];
}
}
System.out.println("String after removal of 5 and 76 : " + str2);
}
}
Input:
Enter a string :
gr5kj376er
Output:
String after removal of 5 and 76 : grkj3er
Related Programs
1) Program to Multiply Numbers Present in a String2) Program to Add numbers present in a String
3) Program to find maximum number of 1s in a substring of a String
4) Count Number of Words in a String
5) Find Occurrence Of Each Word In a Sentence
6) Print All Unique Words Of A String
7) Program To Print Unique Word Of A String Using Set
8) Program to Append one String to another String
9) Program to Compare Two Strings
10) Program to Reverse a String