Algorithm Analysis
Read the input string and replace all the characters a with b and vice versa.
Algorithm Implementation
Declare a character array to store the string.
char s[200];
Read the input string.
gets(s);
Iterate through the string characters. Change every letter ‘a’ to ‘b’. Change every letter ‘b’ to ‘a’.
for (int i = 0; i < strlen(s); i++) if (s[i] == 'a') s[i] = 'b'; else if (s[i] == 'b') s[i] = 'a';
Print the resulting string.
puts(s);
Algorithm Implementation – C++
Read the input string.
getline(cin, s);
Iterate through the string characters. Change every letter ‘a’ to ‘b’. Change every letter ‘b’ to ‘a’.
for (int i = 0; i < s.size(); i++) if (s[i] == 'a') s[i] = 'b'; else if (s[i] == 'b') s[i] = 'a';
Print the resulting string.
cout << s << endl;
Java Implementation
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', '0'); s = s.replace('b', 'a'); s = s.replace('0', 'b'); System.out.println(s); con.close(); } }
Python Implementation
s = input() s = s.replace('a', '0') s = s.replace('b', 'a') s = s.replace('0', 'b') print(s)