Salesforce Interview Question
Developer Program EngineersCountry: India
<pre lang="" line="1" title="CodeMonkey52596" class="run-this">#include <stdio.h>
#include <string.h>
void replace(char* str, char* r) {
int spaces = 0;
int len = strlen(str);
int i=0;
for(i=0;i<len;i++) {
if(str[i] == ' ') spaces++;
}
if(spaces) {
int p = spaces*(strlen(r)-1);
for(i=len;i>=0;i--)
if(str[i] != ' ') {
str[i+p] = str[i];
} else {
int j = strlen(r)-1;
while(j>=0) {
str[i+p] = r[j];
j--;
p--;
}
p++;
}
}
}
void main() {
char str[100] = "1 2 3 4 5 6 7 8 9 0 10 11 12 ";
// char str[100] = "I am CareerCup ";
printf("%s\n",str);
replace(str,"abc");
printf("%s\n",str);
}
</pre><pre title="CodeMonkey52596" input="yes">
</pre>
Step1: Count no of spaces by parsing the input string - O(n)
Step2: Allocate memory for strlen(input string) + (3-1)*noOfSpaces.
Step3: Copy from input string till we encounter space, replace space with replacementString.
Step4: Continue Step3 till we reach end of string.
@FirstSolution your solution is fine but instead of copying to last and start from begining, how about starting from last and do your approach? This avoids initial copying operation
is this a language specific question?
there is no way you can do this to a string in Java without creating a new string
Hi. How about this one. This does it inplace. I tested it with large samples of data. Simple I think.
# include "stdio.h"
# include "string.h"
int main()
{
char *str = malloc(100*sizeof(char));
int i,len,incnum = 0;
memcpy(str,"Chi laka paka pika boomba ",50);
printf("\n%s\n",str);
len = strlen(str);
for(i = 0 ; i < (len); i++ )
{
if(str[i+ incnum] == ' ')
{
memcpy(str+i+incnum+2,str+i+incnum,len-i);
memcpy(str+i+incnum,"ABC",3);
incnum += 3;
}
}
printf("\n%s\n",str);
free(str);
return 0;
}
<pre lang="" line="1" title="CodeMonkey45515" class="run-this">void ReplaceString(char*& str)
{
int i = 0;
int k = 0;
int j = 0;
int spaces = 0;
while(i != strlen(str))
{
if(str[i] == ' ')
{
spaces++;
i++;
}
else
{
i++;
}
}
char* string_new = (char*)malloc(100*sizeof(char));
memcpy(string_new, str, 50);
memset((char*)string_new + i,'0', 2*spaces + i);
string_new[2*i + 2*spaces] = NULL;
while(j != i)
{
string_new[i + spaces*2 + j] = string_new[j];
j++;
}
k = i + 2*spaces;
int inc = 0;
while(string_new[k] != NULL)
{
int p = 0;
int q = 0;
if(string_new[k] == ' ')
{
string_new[inc] = 'A';
string_new[inc + 1] = 'B';
string_new[inc + 2] = 'C';
inc = inc + 3;
q = inc;
p = k + 1;
while(string_new[p] != NULL)
{
string_new[q] = string_new[p];
q++;
p++;
}
k++;
}
else
{
k++;
inc++;
}
}
string_new[i + 2*spaces] = NULL;
j = 0;
while(string_new[j] != NULL)
{
printf("%c", string_new[j]);
j++;
}
}
</pre><pre title="CodeMonkey45515" input="yes">
</pre>
import java.util.*;
public class textSpace {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner scan = new Scanner(System.in);
String s = scan.nextLine();
char[] nip = s.toCharArray();
char[] ip = new char[nip.length*2];
int last = nip.length-1;
int m;
for ( m =0 ;m<nip.length;m++)
{
ip[m]=nip[m];
}
char[] a = {'A','B','C'};
int i,j,k,n_last=0;
for (i=last; i>0;i--)
{
if(ip[i] == ' ')
{
j=last;
last=last+a.length-1;
for (;j>i;j--)
{
n_last=j+a.length-1;
ip[n_last]=ip[j];
}
k=i;
for (int l=0;l<a.length;l++)
{
ip[k]=a[l];
k++;
}
}
}
for (m =0 ;m<ip.length;m++)
{
System.out.print(ip[m]+" ");
}
}
}
#include<stdio.h>
void replace(char str[]);
int main()
{
char str[100]="Interview in Seattle";
printf("%s",str);
replace(str);
printf("\n%s",str);
getch();
}
void replace(char str[])
{
int space=0,i;
for(i=0;i<strlen(str);i++)
{
if(str[i]==' ')
{
memcpy(str+i+3,str+i+1,strlen(str)-i);
memcpy(str+i,"ABC",3);
}
}
}
char *replaceWhiteSpace (char *s) {
int i;
char *strEnd;
int len = strlen (s)-1;
strEnd = s + len;
while (strEnd != s) {
if (*strEnd == ' ') {
len = strlen((strEnd+1));
char *tmp = strEnd+1;
while (len) {
tmp[len-1+2] = tmp[len-1];
len--;
}
tmp = strEnd;
*tmp++ = 'A';
*tmp++ = 'B';
*tmp++ = 'C';
}
strEnd--;
}
}
{I dont know y people r using such abig code whie salesforce provide workflow rules
to repalce....SUBSTITUTE(text, old_text, new_text)
in ex put the text what u want to replace " "(space)
old text :put the text
new put "abc"
short n simple thats it...}
#include<stdio.h>
#include<stdlib.h>
int main()
{
char ch;
int i=0,k=0;
char *a=malloc(200);
printf("enter the string\n");
while((ch=getchar())!='\n')
a[i++]=ch;
a[i]='\0';
while(k<i)
{
if(a[k]==' ')
{
memmove(a+k+3,a+k+1,i-k-1);
a[k++]='A';
a[k++]='B';
a[k++]='C';
i+=2;
}
else
k++;
}
printf("%s",a);
return;
}
public class StringSplit {
public static void main(String[] args) {
String append = "I am Chandu";
String[] splits = append.split(" ");
append="";
System.out.println("splits.size: " + splits.length);
for(int i=0; i<splits.length;i++){
append=append+splits[i]+"abc";}
System.out.println(append); }}
public static void stringReplace(char[] input, char[] replace, char[] output) {
for (int i = input.length - 1, j = output.length - 1; i >= 0; i--, j--) {
if (input[i] != ' ') {
output[j] = input[i];
} else {
for (int k = replace.length - 1; k >= 0; k--) {
output[j] = replace[k];
j--;
}
j++;
}
}
System.out.println(output);
}
public static String replaceSpace(String source, String replacer) {
for (int i=0; i<source.length(); i++) {
if (source.charAt(i)==' ') {
source = source.substring(0,i)+replacer+source.substring(i+1);
i+=replacer.length()-1; //Minus 1 since it will add again on the next loop
}
}
return source;
}
public static char[] ReplaceWithOutMemory(char []str, int length, string replacement, char c)
{
int j = str.Length - 1;
for (int i = length - 1; i >= 0; --i)
{
if (str[i] != c)
{
str[j--] = str[i];
}
else
{
for (int k = replacement.Length - 1; k >= 0; k--)
str[j--] = replacement[k];
}
}
return str;
}
public class ReplaceSpace {
public static void main(String args[]) {
ReplaceSpace rs = new ReplaceSpace();
rs.replace("This is a test", "ABC");
}
public void replace(String a, String b) {
List<Character> newString = new ArrayList<Character>();
char[] orignal = a.toCharArray();
char[] replace = b.toCharArray();
for (int i = 0; i < orignal.length; i++) {
if (orignal[i] == ' ') {
for (int k = 0; k < replace.length; k++) {
newString.add(replace[k]);
}
}
else
newString.add(orignal[i]);
}
System.out.println(newString.toString());
}
}
public class Example2 {
/**
* @param args
*/
public String fillSpace(String input){
String output= "";
for( int i=0; i < input.length(); i++){
char checkSpace = input.charAt(i);
if( checkSpace == ' '){
output += "ABC";
}else{
output += checkSpace;
}
}
return output;
}
//How to replace the space in the string with "ABC" without using extra memory.. string may contain some extra memory.
//str = "i am chandu" -- str contation more memory...
// str = "iABCamABCchandu"
public static void main(String[] args) {
// TODO Auto-generated method stub
Example2 example2 = new Example2();
System.out.println("Ouput ::::"+example2.fillSpace("I am Santhosh"));
}
}
public class ReplaceSpace {
public static void replace(String str)
{ String a="";
for(int i=0;i<str.length();i++)
{ if(str.charAt(i)==' ')
{ String b="hi";
a+=b; }
else a+=str.charAt(i);
}
System.out.println("the manipulated string is :" +a);
}
public static void main(String[] args)
{
replace("this a great day");
}
}
void replaceSpaceWith()
{
int maxlen;
cout<<"Enter max string length";
cin>>maxlen;
//allocate space of len*3, in worst case there will be len spaces
int max = maxlen*3;
char str[max];
cin.ignore();
cout<<"\n ENter string";
cin.getline(str,sizeof(str));
int len = strlen(str);
int numspaces = 0;
//find number of spaces
for(int i = len-1;i>=0;i--)
{
if(str[i] == ' ')
{
numspaces++;
}
}
int newStrLen = len + (2*numspaces);
str[newStrLen+1] = '\0';
for(int i = (len-1), j = (newStrLen-1); i>=0;i--,j--)
{
if(str[i] == ' ')
{
str[j] = '0';
str[--j] = '2';
str[--j] = '%';
}
else
{
str[j] = str[i];
}
}
cout<< "\n new string is "<<str;
}
void replaceSpaceWith()
{
int maxlen;
cout<<"Enter max string length";
cin>>maxlen;
//allocate space of len*3, in worst case there will be len spaces
int max = maxlen*3;
char str[max];
cin.ignore();
cout<<"\n ENter string";
cin.getline(str,sizeof(str));
int len = strlen(str);
int numspaces = 0;
//find number of spaces
for(int i = len-1;i>=0;i--)
{
if(str[i] == ' ')
{
numspaces++;
}
}
int newStrLen = len + (2*numspaces);
str[newStrLen+1] = '\0';
for(int i = (len-1), j = (newStrLen-1); i>=0;i--,j--)
{
if(str[i] == ' ')
{
str[j] = '0';
str[--j] = '2';
str[--j] = '%';
}
else
{
str[j] = str[i];
}
}
cout<< "\n new string is "<<str;
}
void replaceSpaceWith()
{
int maxlen;
cout<<"Enter max string length";
cin>>maxlen;
//allocate space of len*3, in worst case there will be len spaces
int max = maxlen*3;
char str[max];
cin.ignore();
cout<<"\n ENter string";
cin.getline(str,sizeof(str));
int len = strlen(str);
int numspaces = 0;
//find number of spaces
for(int i = len-1;i>=0;i--)
{
if(str[i] == ' ')
{
numspaces++;
}
}
int newStrLen = len + (2*numspaces);
str[newStrLen+1] = '\0';
for(int i = (len-1), j = (newStrLen-1); i>=0;i--,j--)
{
if(str[i] == ' ')
{
str[j] = '0';
str[--j] = '2';
str[--j] = '%';
}
else
{
str[j] = str[i];
}
}
cout<< "\n new string is "<<str;
}
copy the whole string at the end and start copying char by char to from the begining.. when space occurred add "ABC" to the string..
- Anonymous September 23, 2011here is the program...
void duplicate(char *str,int len)
{
char *strEnd = str + strlen(str);
while(strEnd != str)
{
*(str + len) = *strEnd;
len--;
*strEnd-- = '\0';
}
*(str + len ) = *strEnd;
*strEnd = '\0';
strEnd = str + len;
while(*strEnd)
{
if(*strEnd == ' ')
{
*str++ = 'A';
*str++ = 'B';
*str++ = 'C';
}
else
{
*str++ = *strEnd;
*strEnd = '\0';
}
strEnd++;
}
*str = '\0';
}