Showing posts with label String. Show all posts
Showing posts with label String. Show all posts

Thursday, December 19, 2013

SPOJ 13043. The Mirror of Galadriel

Here is the problem statement.
http://www.spoj.com/problems/AMR12D/

The problem is very simple if observed correctly. You only need to check if the given string is palindrome or not. Here is why. The palindrome is a string which if reversed will be the same string. Let's try to prove the reversed point. If the string contains all the reversed substrings of his own that eans that he is a palindrome. which is very easy to prove by just taking the string himself as a substring :D . The reversed of himself can exist in himself if he reads from the left and right the same way .

       DOWNLOAD THE FREE SOURCE CODE 

Tuesday, July 9, 2013

Java (beginner) language basics










































Java is almost the same as C++. The basic operators, data types are the same. The same loops, datas, operators. important differences

We can't write
bool a;
instead of bool we write boolean.
The others are the same except for System.out.print
There are also some difficulties in reading data , we will learn about it later.
one important difference is that we don't have to import(include) String class, it is a default class.


Sunday, June 30, 2013

C++ (beginner) different ways of reading string type

As we all do we write cin>>s; (s is our string type)
what if we need to read a whole line which can include spaces;
cin will read the string until it reaches a space bar or enter. For that we can use a special function called getline..
The getline function can be called in 2 ways
getline(cin,s);
cin.getline(s);

getline function, as the name suggests, this will get everything on that line.

another way is to read a set of characters.
suppose we have char a[10000];
string s;
we can use scanf function to read the characters
scanf("%c",a);
this will read all he text in an array of characters. now we can write this
s=a;
though a is an array and s is a string but you can write thath . NOTE that you can't write the reverse version
a=s; this one is not legal.

Saturday, June 29, 2013

C++(beginner) string type and its important functions

String is a set of characters.It can contain any character.We declare a string variable like this "string s;" here s is our string.For declaring a string we must first include the string class (look in an intermediate section to learn  classes) it by writing it in the top of our code. ( #include <string>)
First important thing is to know how to call a character from a string.There are a few ways to do that.First is all known [] operator.
Imagine we have string s="ABCDEF";
s[1] will be 'B'.(note that the numerating of characters is starting from 0 not 1 in other words s[0]=A, s[1]=B, s[2]=C;}
other way of calling a character is the function called "at".We write it like this s.at(int b); As we can see this function requires a parameter.
b is the parameter and it is the index of the character we want to call.this function returns character so we need to have a separate character to use this function.(char c; c=s.at(1); here c will be equal to 'B';).This way is much safer than the previous one.
Another way is to go with a loop if we need all of the characters and use the iterators to call them.This is the safest and the fastest way.(Learn about the iterators in C++ intermediate section)
Adding character to the string.
we have string s.We can add a character to the end of the string buy using a very useful and fast function called push_back.It looks like this s.push_back(char c).c must be a character.Sometimes people get errors which are very common. note that when you use a character you write it with this symbol '. With strings " symbol.char c='a' but string s="aaaa".It is also possible to add a character to the end of string by just writing s=s+c;(c is a character).It is also possible to add a character from other places of the string.We have a useful function called insert. We can use insert function to add a character or even a group of characters in the middle of a string.
This is it's construction s.insert(int index,string str), here we are adding str starting from index in our string .For example we have
s="AAAAAAA"; and we have str="BB"; If we write s.insert(1,str); now s will be ABBAAAAAA;
We have a function for checking is our string empty or not.It looks like this s.empty() (note that this function has no parameters and is a boolean function which returns true or false).
function erase.
function erase looks like this s.erase(index1,number)those are two integers which is needed to say from what point we want to erase and how many symbols we want to erase.Example string s="ABCCCDA"; s.erase(1,3), now our string will be "ACDA";
function find.
this is one of the most important functions .This function is searching for a string in a string.Example we have string s="ABCCDAA";
and string str="BCC"; now if we want to find str in s we write s.find(str); if program finds str in s it will return the index the starting point of the string found in s.
String type and its functionsAnd for checking how long our string is we write function called size. int b=s.size(); it returns an integer a number of characters in the string.