A company provides network encryption for secure data transfer. The data string is encrypted prior to transmission and gets decrypted at the receiving end. But due to some technical error, the encrypted data is lost and the received string is different from the original string by 1 character. Arnold, a network administrator, is tasked with finding the character that got lost in the network so that the bug does not harm other data that is being transferred through the network.
Write an algorithm to help Arnold find the character that was missing at the receiving end but present at the sending end.
Input
The first line of the input consists of a string - stringSent, representing the string that was sent through the network. The next line consists of a string-string Rec, representing the string that was received at the receiving end of the network.
Output
Print a character representing the character that was lost in the network during transmission.
Note: The input strings string Sent and Stringer consists of lowercase and uppercase English alphabets[i.e. a-z, A-Z].
Example
Input: abcdfjgerj ,abcdfjger
Output: 5
#python3 code
def find_m(string_sent,string_rec):
char_count = [0]*256
for char in string_sent:
char_count[ord(char)]+=1
for char in string_rec:
char_count[ord(char)]-=1
for i in range(256):
if char_count[i]!=0:
missing_char=chr(i)
break
return missing_char
string_sent=input()
string_rec=input()
missing_char=find_m(string_sent,string_rec)
print(missing_char)
#c++ code
#include <iostream>
#include <cstring>
char findMissingChar(const std::string& stringSent, const std::string& stringRec) {
int charCount[256] = {0};
for (char ch : stringSent) {
charCount[static_cast<unsigned char>(ch)]++;
}
for (char ch : stringRec) {
charCount[static_cast<unsigned char>(ch)]--;
}
char missingChar = '\0';
for (int i = 0; i < 256; ++i) {
if (charCount[i] != 0) {
missingChar = static_cast<char>(i);
break;
}
}
return missingChar;
}
int main() {
std::string stringSent, stringRec;
std::cout << "Enter the string_sent: ";
std::cin >> stringSent;
std::cout << "Enter the string_rec: ";
std::cin >> stringRec;
char missingChar = findMissingChar(stringSent, stringRec);
std::cout << "Missing character: " << missingChar << std::endl;
return 0;
}
#Java code
import java.util.Scanner;
public class MissingCharacterFinder {
public static char findMissingChar(String stringSent, String stringRec) {
int[] charCount = new int[256];
for (char ch : stringSent.toCharArray()) {
charCount[ch]++;
}
for (char ch : stringRec.toCharArray()) {
charCount[ch]--;
}
char missingChar = '\0';
for (int i = 0; i < 256; i++) {
if (charCount[i] != 0) {
missingChar = (char) i;
break;
}
}
return missingChar;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the string_sent: ");
String stringSent = scanner.nextLine();
System.out.print("Enter the string_rec: ");
String stringRec = scanner.nextLine();
char missingChar = findMissingChar(stringSent, stringRec);
System.out.println("Missing character: " + missingChar);
}
}
Do you dream of turning your thoughts and words into income ? join us now and become blogger now.
nice