Wipro Technologies Interview Question
Software Engineer / DevelopersTeam: Rainbow
Country: India
Interview Type: Written Test
Sorry for the misleading code above. I was being stupid again.
#include <stdio.h>
#include <ctype.h>
void parse(const char* src)
{
int times;
char c, *p;
for(; c = *src; src = p){
sscanf(src+1, "%d", ×);
for(; times > 0; --times) putchar(c);
for(p = src+2; isdigit(*p); ++p) ;
}
}
int main()
{
parse("a3b2c4");
getchar();
return 0;
}
I definitely can not get the job! There's a bug in the code above again.
#include <stdio.h>
#include <ctype.h>
void parse(const char* src)
{
int times;
char c;
for(; c = *src; ){
sscanf(src+1, "%d", ×);
for(; times > 0; --times) putchar(c);
for(src += 2; isdigit(*src); ++src) ;
}
}
int main()
{
parse("a3b2c4");
getchar();
return 0;
}
C++ version.
#include <iostream>
#include <sstream>
#include <stdexcept>
std::string decode(const std::string& str) {
std::stringstream ss(str);
std::string out;
out.reserve(str.size() * 3); // estimated size to avoid reallocation
while (!ss.eof()) {
char c; int n;
ss >> c >> n;
if (ss.fail())
throw std::invalid_argument("can't decode");
out.append(n, c);
}
return out;
}
int main() {
std::cout << decode("a3b2c4") << std::endl;
return 0;
}
public static void main(String[] args) {
String str= "a13b2c14d15";
Map<Character, Integer> alphaMap= new LinkedHashMap<>();
int count=0;
char hold='\0';
boolean flag=false;
for(int i=0;i<str.length();i++)
{
if(str.charAt(i)<'0'||str.charAt(i)>'9')
{
flag=true;
count=0;
hold=str.charAt(i);
}
else
{
count=count*10+(str.charAt(i)-48);
}
if(flag)
{
alphaMap.put(hold, count);
}
}
System.out.println(alphaMap);
Set<Character> charSet= alphaMap.keySet();
for(char c:charSet)
{
for(int i=0;i<alphaMap.get(c);i++)
{
System.out.print(c);
}
}
}
Is it as simple as the following code?
- uuuouou December 02, 2013