Pages

Wednesday, September 16, 2015

Search and Replace (CPROM Uitm Arau 2015)

Five of the 26 alphabet letters are vowels: A, E, I, O, and U.  Search and replace the vowels in the sentences or paragraph with certain symbol of character.  The symbol which assign to each vowel like in  below:-

Input 
The input should be the sentence of string values.
Output 
The output of the program should display as shown at below.


Input
Cat is a small domesticated carnivorous mammal with soft fur, a short snout, and retractile claws.

Output
C$t #s $ sm$ll d&m@st#c$t@d c$rn#v&r&%s m$mm$l w#th s&ft f%r, $ sh&rt sn&%t, $nd r@tr$ct#l@ cl$ws.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
// Solved By Afiq
import java.util.*;
import java.lang.*;
import java.math.*;
import java.text.*;

public class Q3{
   public static void main(String[] args){
      Scanner scan = new Scanner(System.in);
      
      String line =System.getProperty("line.separator");
      scan.useDelimiter(line);

      String get = scan.next();
      
      String str = get.replace("a","$");
      str = str.replace("A","$");
      str = str.replace("e","@");
      str = str.replace("E","@");
      str = str.replace("i","#");
      str = str.replace("I","#");
      str = str.replace("o","&");
      str = str.replace("O","&");
      str = str.replace("u","%");
      str = str.replace("U","%");
         
      System.out.print(str);
   }
}

Judge Answers in C++



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#include<iostream> 
#include<string.h>
using namespace std;
int main() {
    char sentence[300];
    //declaration string //user input 
    cout<<"Please enter string of sentence:";
    cin.getline(sentence,300);
    //loop for each character
    for(int i=0;i<strlen(sentence);i++) {
        //check & replase vowels : a e i o u  if((sentence[i]=='a')||(sentence[i]=='A'))  sentence[i]='$';
        else if((sentence[i]=='e')||(sentence[i]=='E'))  sentence[i]='@';
        else if((sentence[i]=='i')||(sentence[i]=='I'))  sentence[i]='#';
        else if((sentence[i]=='o')||(sentence[i]=='O'))  sentence[i]='&';
        else if((sentence[i]=='u')||(sentence[i]=='U'))  sentence[i]='%';
    }
    //display output after replacement
    cout<<"After replace the vowels:"<<sentence<<endl;
    return 0;
}

No comments:

Post a Comment