Algorithm Analysis
Declare two pointers at the beginning of the array: i = j = 0. Move the pointer i through the characters of the string. For each character of the string s, not equal to ‘a’, we copy s[i]
to s[j]
and move the pointer j one position forward.
Algorithm Implementation
Declare a character array.
char s[1001];
Read the input string.
fgets(s, sizeof(s), stdin);
Move letters, different from ‘a’, to the left.
int j = 0; for (int i = 0; i < strlen(s); i++) if (s[i] != 'a') s[j++] = s[i];
At the end of the resulting string, put a null byte.
s[j] = 0;
Output the result.
puts(s);
Algorithm Implementation – C++
Read the input string.
getline(cin, s);
Attach letters, different from ‘a’, to the resulting string res.
for (int i = 0; i < s.length(); i++) if (s[i] != 'a') res = res + s[i];
Output the result.
cout << res;
Java Implementation
import java.util.*; public class Main { public static void main(String[] args) { Scanner con = new Scanner(System.in); String s = con.nextLine(); String res = ""; for(int i = 0; i < s.length(); i++) if (s.charAt(i) != 'a') res = res + s.charAt(i); System.out.printf(res); con.close(); } }
Java Implementation – replace
import java.util.*; public class Main { public static void main(String[] args) { Scanner con = new Scanner(System.in); String s = con.nextLine(); s = s.replace("a", ""); System.out.printf(s); con.close(); } }
Python Implementation
s = input() print(s.replace('a', ''))