C Question - 1

String S1 = "M, B , C , A , E "
String S2 = " X , Y , Z , A, C"

Problem:
Given String 1 and String 2, we need to write a program to find out the first occurance of character in string 2 which is present in string 1.

from example, in the above given string A is the character which is in s2 and s1 and which is found in S2 first.
got my question?
then write a C Program or give a procedure how you do it?
then I will come up with more question on this.

Replies

  • Harshad Italiya
    Harshad Italiya
    unsigned char S1[]="MBCAE"
     
    unsigned char S2[]="XYZAC"
     
    unsigned char i,j,match;
     
    void main()
     
    {
     
    for(i=0;i<5;i++)
     
    {
     
    for(j=0;j<5;j++)
     
    {
     
    if(S1==S2[j])
     
    {
     
    match =1;
     
    break;
     
    }
     
    }
     
    if(match==1)
     
    break;
     
    }
     
    print(S1);
     
    }
     
    
    PS:- Haven't compile this code this is logic only It may be wrong.. 😉
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    Ok, the logic is correct.

    Here you are using two for loops, so the worst case scenario is you have the number of comparisions on the order of n[sup]2[sup].
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    Now, I need ideas on how will you reduce the order of the program.

    PS. THis is not my assignment or my project work 😛
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    TO add curiosity , this is a Microsoft Interview question! faced by my friend.

    Hink: Sorting technique will reduce the order to some extent.

    There are other ways as well to reduce the order to only "n". So think and post your answers.
  • simplycoder
    simplycoder
    Sort and use binary search. Where, sort can be implemented in O(n) time and binary search takes up O(lgn)
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    Great! so we have reduced the order to nlogn now.

    There is a way where we can reduce the way only to "n" ways.

    That is using the hast table technique.
  • Saandeep Sreerambatla
    Saandeep Sreerambatla
    Next question on the same one, what all scenarios you can think in testing the above code?

    One is already done, we test the order and reduced it to N ways.

    Consider string 1 and string 2 are pointers and give the test scenarios... There are many interesting things to learn. let me know your thoughts first.

You are reading an archived discussion.

Related Posts

Let us remember the heroines of the suffragist movement that shook the male citadels in the early 1900s: https://awesomestories.com/assets/al...ries&utm_campaign=March+2012&utm_medium=email
TAG Heuer RACER is the 1st Luxury Phone Crafted for High Performance. This phone is definitely only for the MEGA RICH! 😲 It is swiss engineered for ultimate lightness and...
I was quite baffled to see the product called 'Xperia SmartTags'. It's an accessory really. These SmartTags are used to turn on Wi-Fiâ„¢ or launch news & weather app or...
"one of the most interesting aspects in the 4 years of engineering studies, is the practicals hours, helps to be a crazy engineer. I too had a wonderful experience for...
Hi all.. my gate score in ECE gate 2012 is as follows Marks obtained 37.67. Gate Score 633 AIR 1363 What are the options to consider, cutoff, PSUs job. Please...