1 solutions

  • 0
    @ 2026-10-22 18:19:15

    C :

    #define _CRT_SECURE_NO_WARNINGS
    #include<stdio.h>
    #include<string.h>
    #include<stdlib.h>
    
    int main() {
    
    	char buf[1024] = { 0 };
    	char str1[100] = { 0 };
    	char str2[100] = { 0 };
    	char newWord[100] = { 0 };
    	int iBuf = 0;
    	int iDst = 0;
    	int i = 0;
    	gets(buf);
    	scanf("%s",str2);
    	scanf("%s", newWord);
    
    
    	while (iBuf < strlen(buf)) {
    		while (buf[iBuf] != 0 && buf[iBuf] != ' ' ) {
    			str1[i++] = buf[iBuf++];
    		}
    		if (strcmp(str1, str2) == 0) {
    			printf("%s", newWord);
    			if (buf[iBuf] != 0)
    				putchar(' ');
    		}
    		else {
    			printf("%s", str1);
    			if (buf[iBuf] != 0)
    				putchar(' ');
    		}
    
    		iBuf++;
    		memset(str1, 0, 100);
    		i = 0;
    	}
    	putchar('\n');
    	return 0;
    }
    

    C++ :

    #include <iostream>
    #include <sstream>
    #include <string>
    
    using namespace std;
    
    int main() {
        string sentence, oldWord, newWord;
        getline(cin, sentence);
        cin >> oldWord;
        cin >> newWord;
    
        stringstream ss(sentence);
        string word;
        string result;
    
        while (ss >> word) {
            if (word == oldWord) {
                result += newWord + " ";
            } else {
                result += word + " ";
            }
        }
    
        if (!result.empty()) {
            result.pop_back();
        }
    
        cout << result << endl;
        return 0;
    }
    

    Java :

    import java.util.Scanner;
    
     class Main {
        public static void main(String[] args) {
            Scanner scanner = new Scanner(System.in);
            String i = scanner.nextLine();
            String j = scanner.nextLine();
            String k = scanner.nextLine();
    
            String[] words = i.split(" ");
    
    
            StringBuilder sb = new StringBuilder();
    
    
            for (String word : words) {
    
                if (word.equals(j)) {
                    sb.append(k).append(" ");
                } else {
    
                    sb.append(word).append(" ");
                }
            }
            System.out.println(sb.toString().trim());
        }
    
    
    }
    

    Python :

    # coding=utf-8
    sentence = input().strip()  
    replace = input().strip()  
    replacement_word = input().strip()   
    words = sentence.split()  
    new_words = [replacement_word if word == replace else word for word in words]  
    new_sentence = ' '.join(new_words)  
    print(new_sentence)
    
    • 1

    Information

    ID
    343
    Time
    1000ms
    Memory
    128MiB
    Difficulty
    (None)
    Tags
    (None)
    # Submissions
    0
    Accepted
    0
    Uploaded By