Tuesday, 3 February 2015

#include<iostream>
int main()
{
  std::string s="aabbcccccdddddeeeeee";
  std::string output;
  char out[15]={'\0'};

  int len=s.length();

  int repeatedCharacter=1;

  int outcount=0;

  char c;



  for(int i=0; i<len; i++)

  {

    repeatedCharacter=1;

    c=s[i];



    while(c == s[i++])

    { 

      repeatedCharacter++;

    } 



    std::cout<<c;

    std::cout<<repeatedCharacter;



    out[outcount++]=(char)c;

    out[outcount++]='0'+repeatedCharacter;



  }



  std::cout<<"\nFinal: "<<out;

  return 0;




}


#include<iostream>

int main()
{
  std::string s="HAI HOW ARE YOU !";
  int pos=s.find(" ");
  while(pos != std::string::npos)
  {
    s.erase(pos,1);
    s.insert(pos,"20");
    pos=s.find(" ");
  }
  std::cout<<s;
  return 0;
}
#include<iostream>

bool toLowercase(std::string &p_str)
{
  for(int i=0; i<p_str.length(); i++)
  {
    if(p_str[i] >='A' && p_str[i]<='Z')
    { 
      p_str[i]= p_str[i]+32;  
    } 
  }
}

bool toUppercase(std::string &p_str)
{
  for(int i=0; i<p_str.length(); i++)
  {
    if(p_str[i] >='a' && p_str[i]<='z')
    { 
      p_str[i]= p_str[i]-32;
    }
  }
}

int main()
{
  std::string inp="Hai How Are You?";
  toLowercase(inp);
  std::cout<<"\nTo Lower case: "<<inp;
  toUppercase(inp);
  std::cout<<"\nTo Upper case: "<<inp;
  return 0;
}

Anagrams are strings that are formed by re arranging the charcters from another word.
Eg: Creative and reactive.


#include<iostream>
#include<string>

int main()
{
  std::string s1="creative",s2="reactive";

  if(s1.length() != s2.length())
  {
    std::cout<<"Not a anagram";
    return false;  
  }

  int len=s1.length();
  int i;
  for( i=0; i<len; i++)
  {
    int pos=s2.find(s1[i]);

    {
      s2.erase(pos,1);
    }
    else
    {
      std::cout<<"Not matched!!";
    }
  }

  if(s2.length() == 0)
  {
    std::cout<<"String is anagram";
  }

  return 0;
}