Techlogix Interview Question
InternsCountry: India
Interview Type: Written Test
#include <iostream>
#include <string.h>
using namespace std;
char *Create_String()
{
char *String, *Temp;
Temp = new char[256];
gets(Temp);
String = new char[strlen(Temp)+1];
strcpy(String, Temp);
delete [] Temp;
return String;
}
void The_Function(char *String)
{
int Rround_Times;
char Current_Char;
for (int i=0 ; String[i] != '\0' ; i=i+2)
{
Rround_Times = (String[i+1]-'0');
Current_Char = String[i];
while (Rround_Times-- != 0)
cout << Current_Char;
}
}
void main()
{
char *String = Create_String();
The_Function(String);
delete [] String;
}
#include <iostream>
#include <string.h>
using namespace std;
char *Create_String()
{
char *String, *Temp;
Temp = new char[256];
gets(Temp);
String = new char[strlen(Temp)+1];
strcpy(String, Temp);
delete [] Temp;
return String;
}
void The_Function(char *String)
{
int Rround_Times;
char Current_Char;
for (int i=0 ; String[i] != '\0' ; i=i+2)
{
Rround_Times = (String[i+1]-'0');
Current_Char = String[i];
while (Rround_Times-- != 0)
cout << Current_Char;
}
}
void main()
{
char *String = Create_String();
The_Function(String);
delete [] String;
}
#include<iostream>
using namespace std;
int main()
{
string s,str;
cin>>s;
char tmp;
string digit="";
int j=0,t=0;
for(int i=1;i<s.length();i++)
{
digit=digit+s[i];
i++;
}
for(int i=0;i<s.length();i++)
{
j=digit[t]-48;
t++;
for(int k=0;k<j;k++)
str=str+s[i];
i=i+1;
}
cout<<str<<endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s,str;
cin>>s;
char tmp;
string digit="";
int j=0,t=0;
for(int i=1;i<s.length();i++)
{
digit=digit+s[i];
i++;
}
for(int i=0;i<s.length();i++)
{
j=digit[t]-48;
t++;
for(int k=0;k<j;k++)
str=str+s[i];
i=i+1;
}
cout<<str<<endl;
return 0;
}
// ZoomBA
/*
We are assuming only alpha.
Thus, the general regex for the string is :
word -> [a-zA-Z][\d]+
s = ( word )+
Thus, the problem can be solved by tokenization of word
and then expanding the word
*/
word = '[a-zA-Z][\\d+]'
string = "a4b2c2a3f1g2"
println(string)
// tokenize on string
l = tokens( string , word ) -> {
// extract letter
letter = (tokens($.o, '[a-zA-Z]' ))[0]
// extract frequency
frequency = (tokens($.o, '\\d+' ))[0]
// expand the letter with frequency : a ** 2 -> aa
letter ** int(frequency)
}
// finally catenate over the list
println( str(l,'') )
public class CharOccurAsNumber {
public String ModifyStringAsGivenNumber(String s)
{
String numbers="";
String chars="";
String result="";
if(s!="" && s!=null)
{
//extracting all the numbers from the given String in String format.
numbers=s.replaceAll("[^0-9]","");
//extracting all the characters from the given String in String format.
chars=s.replaceAll("[0-9]","");
//if the length of numbers and chars are same then proceed.
if(numbers.length()==chars.length())
{
for(int i=0;i<numbers.length();i++)
{
//Extracting the numbers one by one from the numbers String in String format.
String s1=numbers.substring(i,i+1);
//Extracting the characters one by one from the numbers String in String format.
String s2=chars.substring(i,i+1);
//parse the numbers from the SubString to Integer one by one.
int n=Integer.parseInt(s1);
//for the amount = the extracted number repeat the extracted Character and put it inside a String.
for(int j=0;j<n;j++)
result+=s2;
}
}
else
{
result="Inserted String format is wrong";
}
return result;
}
else
{
return "Empty String or no String inserted";
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CharOccurAsNumber ob=new CharOccurAsNumber();
String s="a2b1c5e1g4";
String test=ob.ModifyStringAsGivenNumber(s);
System.out.println(test);
}
}
public class CharOccurAsNumber {
public String ModifyStringAsGivenNumber(String s)
{
String numbers="";
String chars="";
String result="";
if(s!="" && s!=null)
{
//extracting all the numbers from the given String in String format.
numbers=s.replaceAll("[^0-9]","");
//extracting all the characters from the given String in String format.
chars=s.replaceAll("[0-9]","");
//if the length of numbers and chars are same then proceed.
if(numbers.length()==chars.length())
{
for(int i=0;i<numbers.length();i++)
{
//Extracting the numbers one by one from the numbers String in String format.
String s1=numbers.substring(i,i+1);
//Extracting the characters one by one from the numbers String in String format.
String s2=chars.substring(i,i+1);
//parse the numbers from the SubString to Integer one by one.
int n=Integer.parseInt(s1);
//for the amount = the extracted number repeat the extracted Character and put it inside a String.
for(int j=0;j<n;j++)
result+=s2;
}
}
else
{
result="Inserted String format is wrong";
}
return result;
}
else
{
return "Empty String or no String inserted";
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
CharOccurAsNumber ob=new CharOccurAsNumber();
String s="a2b1c5e1g4";
String test=ob.ModifyStringAsGivenNumber(s);
System.out.println(test);
}
}
java code
public static void main(String[] args) {
String str = "a4b2c2a3f1g2";
char cha[] = str.toCharArray();
StringBuilder sb = new StringBuilder();
for(int i=0 ; i < cha.length; i++) {
concat(sb, cha[i], Character.getNumericValue(cha[i+1]));
i++;
}
System.out.println(sb);
}
private static void concat(StringBuilder sb, char c, int d) {
for(int i=0; i<d;i++)
sb.append(c);
}
public static void main(String[] args) {
String str = "a4b2c2a3f1g2";
char cha[] = str.toCharArray();
StringBuilder sb = new StringBuilder();
for(int i=0 ; i < cha.length; i++) {
concat(sb, cha[i], Character.getNumericValue(cha[i+1]));
i++;
}
System.out.println(sb);
}
private static void concat(StringBuilder sb, char c, int d) {
for(int i=0; i<d;i++)
sb.append(c);
}
public static void main(String[] args) {
String str = "a4b2c2a3f1g2";
char cha[] = str.toCharArray();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < cha.length; i++) {
concat(sb, cha[i], Character.getNumericValue(cha[i + 1]));
i++;
}
System.out.println(sb);
}
private static void concat(StringBuilder sb, char c, int d) {
for (int i = 0; i < d; i++)
sb.append(c);
}//
public class CharacterOccurence {
private static void characterPrint(String str) {
int count = 0;
while(count <= str.length()-2) {
char val = str.charAt(count);
String valCount = " "+str.charAt(count+1);
int len = Integer.parseInt(valCount);
int pointer = 0;
while(pointer <len) {
System.out.print(val);
pointer++;
}
count = count+2;
}
}
}
a basic approach would be to traverse the string from the beginning and add new character as needed.
Here is how it goes
public string transformString(string input)
{
if(String.IsNullOrWhiteSpace(input))
{
return input;
}
StringBuilder s = new StringBuilder();
int num = 0;
int j = 0;
while(j < input.Length && (input[j] >= 48 && input[j] <= 57))
{
s.Append(input[j]);
j++;
}
for(int i = j; i < input.Length;i++)
{
num = 0;
j = i+1;
while((j < input.Length) && (input[j] >= 48 && input[j] <= 57))
{
num = num*10 + (input[j] - 48);
j++;
}
while(num-- > 0)
{
s.Append(input[i]);
}
i = j;
}
return s.ToString();
}
Complexity is O(N) in Time and O(N) in space where N is the new size of the string(which is the size of the output string).
Simple Java Code:
- viksidada March 27, 2017