GeeksforGeeks » Interview Questions
C/ Java programming
(12 posts)-
(i) If A is be replaced by B, B is replaced by D and C is replaced by F & goes to the end with same scenario. Write a program to get the replaced value of “ SMART “
(ii) Write a Program to Mix your First Name & Surname’s character’s alternatively.
eg: For “ AMIT KUMAR” result would be “ AKMUIMTAR” -
[b]kindly give the solution of the above program
-
@Vinay: These questions look like simple assignment questions. Give them a try yourself and let use know the specific problems you face in programming them.
-
i got the second ones logic but getting problems in the 1st on 2 question.
if(c1length>0) { c3[j]=c1[k]; k++; c1length--; j++; } if(c2length>0) { c3[j]=c2[i]; i++; j++; c2length--; } } -
i tried the first one but not getting the right kindly check:
for(int i=0 ; i<len; i++) { temp=c[i]; index=temp-64; jump= index*2; if(jump>26) jump=jump-27; b[i]= (char)jump; } -
@kartik
giv the solution for the first one...
thank u for concern -
@vinay: The following program should work for your problem.
#include <stdio.h> #include <string.h> int main() { char str[] = "ABCD"; int n = strlen(str); int i, jump = 1; for (i = 0; i < n; i++) { // Considering all capital letters jump = 1 + str[i] - 'A'; // If going beyond 'Z' then start from 'A' if (jump + str[i] > 'Z') jump = jump - 27; str[i] = str[i] + jump; } printf("%s", str); return 0; } -
str[i] = str[i] + jump;
jump is int type but str is char array...
possible loss of precision???? -
public String replce(String s1)
{
char [] x = s1.toCharArray();
Map<Integer,String>map = new HashMap<Integer,String>
map.put(1,"a");
map.put(2,"b");
map.put(3"c");
for(charone:x);
int k = map.getkey(charone);
if(2k >26){
k=2k-26;
char newChar=map.get(k);
char [] newString += newChar;
newString.toString();
return newString();
}
private static String getKey(Map map, String value) {
for (Iterator i = map.keySet().iterator(); i.hasNext() {
String key = (String)i.next();
if (map.get(key).equals(value)) {
return key;
}
} -
@ Raghav thanks but m nt familiar with hashmap could it be simpler??
-
@vinay: When we add a a char variable to an integer, the char variable is casted to int. So no loss of precison.
-
thanks kartik n raghav.
Reply
You must log in to post.