Sunday, 13 July 2014

Arrays and Strings : Write a method to replace all spaces in a string with ‘%20’.

#include <iostream>
#include <cstring>
#include <conio.h>

char* replace1(char *c){
    if(c == NULL) return NULL;
    int len = strlen(c);
    if(len == 0) return NULL;
    int cnt = 0;
    for(int i=0; i<len; ++i)
    {
        if(c[i] == ' ')
            ++cnt;
    }
    char *cc = new char[len+2*cnt+1];
    int p = 0;
    for(int i=0; i<len; ++i)
    {
        if(c[i] == ' ')
        {
            cc[p] = '%';
            cc[p+1] = '2';
            cc[p+2] = '0';
            p += 3;
        }
        else
        {
            cc[p] = c[i];
            ++p;
        }
    }
    cc[p] = '\0';
    return cc;
}
int main(){
    const int len = 100;
    char c[len] = "abcd efgh";
    cout<<replace1(c)<<endl;
    getch();
    return 0;
}

Arrays and Strings : Write a method to decide if two strings are anagrams or not.

Two strings are anagrams if both strings contain the same characters could be in different orders . For example "abcd" , "dcba" , "bcda" all are anagrams . 
In the given question we have to check whether two strings are anagrams or not 

#include<iostream.h>
#include<conio.h>
#include<cstring.h>
bool isAnagram(string s1 , string s2)
 {
   if(s1=="" || s2=="") return false ;
   if(s1.length()!=s2.length()) return false ;
   int c[256] ;
   memset(c,0,sizeof(c));
   int len = s1.length() ;
   for(int i=0;i<len;++i)
    {
      ++c[(int)s1[i]] ;
      --c[(int)s2[i]] ;
    }

    for(int i=0 ; i<256;i++)
     {
       if(c[i]!=0) return false ;

     }
    return true ;
 }
int main ()
{
  string s1 = "abcd" ;
  string s2 = "abdc" ;
  bool ans = isAnagram(s1,s2) ;
  cout<<ans ;
  getch();
}

this problem has a very simple trick all you have to do is use a buffer int array and initialize every element to 0 . Now in a loop increment the respective ascii position of character of string s1 and at the same time decrement for s2 .  If the array elements are all 0 that means they are anagrams .
That simple :)

Arrays and Strings : Design an algorithm and write code to remove the duplicate characters in a string without using any additional buffer. NOTE: One or two additional variables are fine. An extra copy of the array is not.



#include<iostream.h>
#include<conio.h>
#include<cstring.h>
string remove(string s)
{
    int len = s.length();
    if(len < 2) return s;
    string str = "";
    for(int i=0; i<len; ++i)
    {
        if(s[i] != '\0')
        {
            str += s[i];
            for(int j=i+1; j<len; ++j)
                if(s[j]==s[i])
                    s[j] = '\0';
        }
    }
    return str;
}
int main ()
{
  string s1 = "abcdefgha" ;
  cout<<remove(s1) ;
  getch();
}

In this code we pick a character from the string assign it and then compare every Ith character with each and every I+1th character till the last character if we encounter a match we null that position .
In the end we return the string
 

Arrays and Strings : Write code to reverse a C-Style String. (C-String means that “abcd” is represented as five characters, including the null character.)

As the question itself explains what a C-Style string is "C-String means that “abcd” is represented as five characters, including the null character."


lets start the code :-

#include<iostream.h>
#include<conio.h>
void swap(char &a, char &b)
 {

    a = a^b;
    b = a^b;
    a = a^b;
 }
void reverse(char *s)
 {
   char *p = s , *q = s ;
 while(*q)
  q++ ;
q-- ;
while(p<q)
swap(*p++ , *q--) ;
 }
int main ()
 {
   char s[] = "abcdefghijkl" ;
   reverse(s) ;
 cout<<s<<endl ;
 }

Here i did nothing but used pointers ,  reverse function receives a character pointer and assign its value to two other pointers . pointer q iterates till the end and after that q has the address of the last character and  p contains the address of the first .
nothing much important left after that all you have to do is swap first character with the last one .  Increment p's value and decrement q's value at the same time
 

Arrays and Strings : Implement an algorithm to determine if a string has all unique characters.

This problem can be easily solved by using a buffer array of the type bool which will act as a check parameter to see if the character already occurred in string or not .

lets start coding :-


#include<iostream.h>
#include<conio.h>
#include<cstring.h>

bool checkUnique(string s)
{
 bool a[256];
 memset(a,0,sizeof(a)) ;
 int len = s.length();
for(int i=0;i<len;i++)
 {
             int pos =  (int)s[i] ;
             if(a[pos]) return false ;
            a[pos] = 1  ;

}
return true ;
}
int main (){
  string s1 = "Hello this is my first post" ;
  string s2 = "qwertyuiopasdfgghjklzxcvbnm" ;
 cout<<checkUnique(s1)<<endl ;
 cout<<checkUnique(s2)<<endl  ; 
 return 0 ;
}

Here what i used is a for loop in which i pick a character convert it into integer (its ascii value) and check at that location what array "a" holds .
if its "1" that means the character is repeated and its not unique otherwise continue the loop .