C Question - 1

Saandeep Sreerambatla

Saandeep Sreerambatla

@saandeep-sreerambatla-hWHU1M Oct 22, 2024
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

Welcome, guest

Join CrazyEngineers to reply, ask questions, and participate in conversations.

CrazyEngineers powered by Jatra Community Platform

  • Harshad Italiya

    Harshad Italiya

    @harshad-ukH5ww Mar 15, 2012

    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

    @saandeep-sreerambatla-hWHU1M Mar 15, 2012

    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

    @saandeep-sreerambatla-hWHU1M Mar 15, 2012

    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

    @saandeep-sreerambatla-hWHU1M Mar 17, 2012

    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

    @simplycoder-NsBEdD Mar 17, 2012

    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

    @saandeep-sreerambatla-hWHU1M Mar 17, 2012

    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

    @saandeep-sreerambatla-hWHU1M Mar 17, 2012

    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.