-
Notifications
You must be signed in to change notification settings - Fork 0
/
Replace space with some character.txt
84 lines (61 loc) · 1.86 KB
/
Replace space with some character.txt
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
You have been given a string 'STR' of words. You need to replace all the spaces between words with “@40”.
Input Format:
The first line contains a single integer ‘T’ representing the number of test cases.
The first line of each test case will contain a single string 'STR' consisting of one or more words.
Output Format:
For each test case, return the modified string after replacing all the spaces between words with “@40”.
Print the output of each test case in a separate line.
Note:
You don’t need to print anything, It has already been taken care of. Just implement the given function.
Constraints:
1 <= T <= 50
0 <= |STR| <= 100
Where ‘|STR|’ is the length of a particular string including spaces.
Time limit: 1 sec
Sample Input 1:
2
Coding Ninjas Is A Coding Platform
Hello World
Sample Output 1:
Coding@40Ninjas@40Is@40A@40Coding@40Platform
Hello@40World
Explanation Of Sample Output 1:
In test case 1, After replacing the spaces with “@40” string is:
Coding@40Ninjas@40Is@40A@40Coding@40Platform
In test case 2, After replacing the spaces with “@40” string is:
Hello@40World
Sample Input 2:
3
Hello
I love coding
Coding Ninjas India
Sample Output 2:
Hello
I@40love@40coding
Coding@40Ninjas@40India
Explanation For Sample Output 2:
In test case 1, After replacing the spaces with “@40” string is:
Hello
In test case 2, After replacing the spaces with “@40” string is:
I@40love@40coding
In test case 3, After replacing the spaces with “@40” string is:
Coding@40Ninjas@40India
Solution:
string replaceSpaces(string &str)
{
string temp = "";
for(int i = 0 ; i < str.length() ; i++)
{
if(str[i] == ' ')
{
temp.push_back('@');
temp.push_back('4');
temp.push_back('0');
}
else
{
temp.push_back(str[i]);
}
}
return temp;
}