Please help,the problem is described in code Please open to see my problem | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Please help,the problem is described in code Please open to see my problem

https://code.sololearn.com/ci4NvBuP7649/?ref=app You write a phrase and include a lot of number characters (0-9), but you decide that for numbers 10 and under you would rather write the word out instead. Can you go in and edit your phrase to write out the name of each number instead of using the numeral? Task: Take a phrase and replace any instances of an integer from 0-10 and replace it with the English word that corresponds to that integer. Input Format: A string of the phrase in its original form (lowercase). Output Format: A string of the updated phrase that has changed the numerals to words. Sample Input: i need 2 pumpkins and 3 apples Sample Output: i need two pumpkins and three apples

30th Jan 2020, 9:04 PM
Prabhjot Singh
Prabhjot Singh - avatar
16 Answers
30th Jan 2020, 9:18 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 11
s=input() n={0: "zero",1: "one",2: "two",3: "three",4:"four",5:"five",6:"six",7:"seven",8:"eight",9:"nine"} i=0 s=s.replace("10","ten") while i <10: s=s.replace(str(i),n[i]) i=i+1 print(s)
25th Mar 2020, 5:36 PM
S V S L N Surya Suhas Vaddhiparthy
S V S L N Surya Suhas Vaddhiparthy - avatar
+ 5
Hrushikesh U N S V S L N Surya Suhas Vaddhiparthy don't post irrelevant answers here please. You are free to post a new question if you want to. Thanks for understanding.
25th Mar 2020, 6:08 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 4
Here in C #include <stdio.h> #include <string.h> int main() { char s[60]; gets(s); int n; n = strlen(s); for(int i = 0; i < n; i++){ if(s[i] == '0'){ printf("zero"); } else if(s[i] == '1'){ if(s[i+1] == '0'){ printf("ten"); i++; } else{ printf("one"); } } else if(s[i] == '2'){ printf("two"); } else if(s[i] == '3'){ printf("three"); } else if(s[i] == '4'){ printf("four"); } else if(s[i] == '5'){ printf("five"); } else if(s[i] == '6'){ printf("six"); } else if(s[i] == '7'){ printf("seven"); } else if(s[i] == '8'){ printf("eight"); } else if(s[i] == '9'){ printf("nine"); } else{ printf("%c", s[i]); } } }
31st Aug 2021, 12:51 PM
Rusiru Rathmina
Rusiru Rathmina - avatar
+ 2
Nothing is described in the code. Can you please give more info ?
30th Jan 2020, 9:09 PM
Aymane Boukrouh
Aymane Boukrouh - avatar
+ 2
For C language #include <stdio.h> #include <string.h> int main() { char s[60]; gets(s); int n; n = strlen(s); for(int i = 0; i < n; i++){ if(s[i] == '0'){ printf("zero"); } else if(s[i] == '1'){ if(s[i+1] == '0'){ printf("ten"); i++; } else{ printf("one"); } } else if(s[i] == '2'){ printf("two"); } else if(s[i] == '3'){ printf("three"); } else if(s[i] == '4'){ printf("four"); } else if(s[i] == '5'){ printf("five"); } else if(s[i] == '6'){ printf("six"); } else if(s[i] == '7'){ printf("seven"); } else if(s[i] == '8'){ printf("eight"); } else if(s[i] == '9'){ printf("nine"); } else{ printf("%c", s[i]); } } }
1st Feb 2022, 7:13 PM
Avrian Shandy
Avrian Shandy - avatar
+ 1
Wherever the number comes from 1 to 9 .it changes the number from any sentence to string
30th Jan 2020, 9:13 PM
Prabhjot Singh
Prabhjot Singh - avatar
+ 1
Try this guys it may help import java.util.Scanner; public class Program { public static void main(String[] args) { Scanner input = new Scanner(System.in); String text=input.nextLine(); String [] number={"zero","one","two","three","four","five","six","seven","eight","nine"}; char [] txt=text.toCharArray(); for(int i=0;i<txt.length;i++){ if(Character.isDigit(txt[i])){ text=text.replace(Character.toString(txt[i]), number[Character.getNumericValue(txt[i])]); } } if(text.contains("onezero")){ System.out.print(text.replaceAll("onezero","ten")); } else{ System.out.print(text); } } }
13th Sep 2022, 5:32 PM
Ong'ondi Eugene
Ong'ondi Eugene - avatar
0
Please can i get this in C or C++.....i got struck at replacing numbers by words in a phrase .
8th Feb 2020, 8:42 AM
Hrushikesha U N
Hrushikesha U N - avatar
0
I'll try 😁and post if I get the correct answer
25th Mar 2020, 5:47 PM
S V S L N Surya Suhas Vaddhiparthy
S V S L N Surya Suhas Vaddhiparthy - avatar
0
Here's my code in Java I used String and StringBuilder class to display the output Scanner s = new Scanner(System.in); System.out.print("Input: "); String strn = s.nextLine(); StringBuilder str = new StringBuilder(strn); for(int i = 0; i < str.length(); i++){ if(str.charAt(i) == '0'){ str.deleteCharAt(i); str.insert(i,"zero"); } else if(str.charAt(i) == '1'){ str.deleteCharAt(i); str.insert(i,"one"); } else if(str.charAt(i) == '2'){ str.deleteCharAt(i); str.insert(i,"two"); } else if(str.charAt(i) == '3'){ str.deleteCharAt(i); str.insert(i,"three"); } else if(str.charAt(i) == '4'){ str.deleteCharAt(i); str.insert(i,"four"); } else if(str.charAt(i) == '5'){ str.deleteCharAt(i); str.insert(i,"five"); } else if(str.charAt(i) == '6'){ str.deleteCharAt(i); str.insert(i,"six"); }
22nd Jul 2021, 11:18 AM
Ian Edwin Kaire
Ian Edwin Kaire - avatar
0
Here's the continuation: else if(str.charAt(i) == '7'){ str.deleteCharAt(i); str.insert(i,"seven"); } else if(str.charAt(i) == '8'){ str.deleteCharAt(i); str.insert(i,"eight"); } else if(str.charAt(i) == '9'){ str.deleteCharAt(i); str.insert(i,"nine"); } } String st = str.toString(); if(st.contains("onezero")){ System.out.print(st.replaceAll("onezero","ten")); } else{ System.out.print(st); }
22nd Jul 2021, 11:19 AM
Ian Edwin Kaire
Ian Edwin Kaire - avatar
0
Here's my code in C++: #include <iostream> #include <string> using namespace std; bool ReplaceString(string &s, const string &oldSubstr, const string &newSubstr); int main(){ cout<<"Input: "; string str; getline(cin,str); for(int i = 0; i < str.length(); i++){ if(str[i] == '0'){ str.erase(i,1); str.insert(i,"zero"); } else if(str[i] == '1'){ str.erase(i,1); str.insert(i,"one"); } else if(str[i] == '2'){ str.erase(i,1); str.insert(i,"two"); } else if(str[i] == '3'){ str.erase(i,1); str.insert(i,"three"); } else if(str.at(i) == '4'){ str.erase(i,1); str.insert(i,"four"); } else if(str.at(i) == '5'){ str.erase(i,1); str.insert(i,"five"); } else if(str.at(i) == '6'){ str.erase(i,1); str.insert(i,"six"); }
22nd Jul 2021, 11:23 AM
Ian Edwin Kaire
Ian Edwin Kaire - avatar
0
Continuation: else if(str.at(i) == '7'){ str.erase(i,1); str.insert(i,"seven"); } else if(str.at(i) == '8'){ str.erase(i,1); str.insert(i,"eight"); } else if(str[i] == '9'){ str.erase(i,1); str.insert(i,"nine"); } } bool isReplaced = ReplaceString(str,"onezero","ten"); if(isReplaced){ cout<<str; } else{ cout<<str; } } bool ReplaceString(string &s, const string &oldSubstr, const string &newSubstr){ bool isReplaced = false; int currentPosition = 0; while(currentPosition < s.length()){ int position = s.find(oldSubstr, currentPosition); if(position == string::npos){ return isReplaced; } else{ s.replace(position,oldSubstr.length(),newSubstr); currentPosition = position + newSubstr.length(); isReplaced = true; } } return isReplaced; }
22nd Jul 2021, 11:24 AM
Ian Edwin Kaire
Ian Edwin Kaire - avatar
- 1
This is a challenge posted in community challenges section of sololearn app This works like this: INPUT : i have 2 apples. OUTPUT : i have two apples
30th Jan 2020, 9:12 PM
Prabhjot Singh
Prabhjot Singh - avatar
- 1
Yes
25th Mar 2020, 5:46 PM
S V S L N Surya Suhas Vaddhiparthy
S V S L N Surya Suhas Vaddhiparthy - avatar