GeeksforGeeks » Interview Questions

C/ Java programming

(12 posts)

Tags:

  1. Vinay
    guest
    Posted 4 months ago #

    (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”

  2. Vinay
    guest
    Posted 4 months ago #

    [b]kindly give the solution of the above program

  3. kartik
    Moderator
    Posted 4 months ago #

    @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.

  4. Vinay
    guest
    Posted 4 months ago #

    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--;
    		}
    	}
  5. vinay
    guest
    Posted 4 months ago #

    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;
    	}
  6. Vinay
    guest
    Posted 4 months ago #

    @kartik
    giv the solution for the first one...
    thank u for concern

  7. kartik
    Moderator
    Posted 4 months ago #

    @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;
    }
    
  8. vinay
    guest
    Posted 4 months ago #

    str[i] = str[i] + jump;
    jump is int type but str is char array...
    possible loss of precision????

  9. Raghav
    guest
    Posted 4 months ago #

    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;
    }
    }

  10. vinay
    guest
    Posted 4 months ago #

    @ Raghav thanks but m nt familiar with hashmap could it be simpler??

  11. kartik
    Moderator
    Posted 4 months ago #

    @vinay: When we add a a char variable to an integer, the char variable is casted to int. So no loss of precison.

  12. vinay
    guest
    Posted 4 months ago #

    thanks kartik n raghav.


Reply

You must log in to post.

RSS feed for this topic