Code Puzzles

1. Reverse a string

void reverseString()
{
    char r[12]="reversethis";
    char *start = r;
    char *end = r;
    while (*end!=0)end++;
    end--;
    while(start!=end)
    {
        //swap
        char t = start[0];
        *start = *end;
        *end = t;

        start++;
        end--;
    }
    printf("%s",r);
}

2. Traverse a matrix (m X n) spirally

void goSpiral()
{
    int arr[5][6] = {{1,2,3,4,5,50},
                    {6,7,8,9,10,100},
                    {11,12,13,14,15,150},
                    {16,17,18,19,20,200},
                    {21,22,23,24,25,250}
                    };

    int i=0, j=0,ilim=5,jlim=6;
    int till=0;
    int doing = 1;
    while(doing)
    {
        doing =0;
        for (i=till,j=till;j<jlim-till;j++)    doing = printf("%d ",arr[i][j]);
        if(doing==0) break;
       
        doing =0;
        for(j--,i++;i<ilim-till;i++)    doing = printf("%d ",arr[i][j]);
        if(doing==0) break;
       
        doing =0;
        for(j=jlim-till-2,i=ilim-till-1; j>=till+0; j--) doing = printf("%d ",arr[i][j]);
        if(doing==0) break;
       
        doing =0;
        for(j=till+0,i=ilim-till-2; i>=till+1; i--) doing = printf("%d ",arr[i][j]);
        if(doing==0) break;
        till++;
    }

   
}

3. Sort an array with just 2 types of elements

void sortJustTwoThings()
{
    int a[10]={1,0,1,0,1,0,1,0,1,0};
    int ptr1 = 0; //start of the array
    int ptr0 = 9; //end of the array
    while(ptr1 != ptr0)
    {
    if(a[ptr1]!=0)
    {
        while(a[ptr0]!=0) ptr0--;
        if(ptr1>ptr0) break;
        int t = a[ptr1];
        a[ptr1] = a[ptr0];
        a[ptr0] = t;
        ptr0--;
        if(ptr1==ptr0) break;
    }
    ptr1++;
    }
   
    for(int i=0;i<10;i++)
    {
        printf("%d ",a[i]);
    }
    printf("\n");

}

4. Concatenate 2 strings and remove the overlap. For eg, if string1 is PRAVARABC and string 2 is ABCAKHYA resultant string should be PRAVARAKHYA.

void removeOverlap()
{
    char str1[]="ABCDE";
    char str2[]="DECDGEFGHIJK";
    char strout[500];
    char temp[20]="";
    //first copy the first string
    strcpy(strout,str1);
    int l = strlen(str1);
    int r = 0;
    bool overlap = false;
    for(int i=1;i<=strlen(str2);i++)
    {
        strncpy(temp,str2,i);
        char *s = strstr(str1,temp);
        if (s!=NULL)
        {
            r = l-(int)(s-str1);
            if (i==r)
            {
                for (int m=i; m<strlen(str2);m++)
                    strout[l++] = str2[m];

                strout[l]='\0';
                overlap = true;
                break;
            }
        }
    }

    if(!overlap)
        strcat(strout,str2);

    printf("%s",strout);
}

5. reverse the words in a sentence.

void reverseSentence()
{
    char sent[]= "sun rises in the east and sets in the west";
    int s = strlen(sent);
    for (int i=0; i<s/2; i++)
    {
        char c = sent[i];
        sent[i] = sent[s-1-i];
        sent[s-1-i] = c;
    }
    int cur =0;
    char *c = sent;
    int j =0;
    while (*c != '\0')
    {
        j =0;
        while (*c!=' ')
        {
            if (*c=='\0') break;
            j++;
            c++;
        }
        for(int k = cur, m=0; k<cur + j/2 ; k++,m++)
        {
            char l = sent[k];
            sent[k] = sent[cur+j-m-1];
            sent[cur+j-m-1] = l;
        }

        if (*c=='\0') break;
        c++;
        j++;
        cur += j;
    }


    printf("%s",sent);
}

 

 

2 comments:

  Anonymous

12/22/2006 1:08 PM

Thank You for codes!

  Anonymous

12/27/2009 1:22 PM

I can not with you will disagree.