Showing posts with label function. Show all posts
Showing posts with label function. Show all posts

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.

C++(beginner) writing our first functions

In C++ we can write our own functions and we can use them in our code. If we need to do the same action many different times on our code , instead of copy-pasting it we can use functions.


The space I marked red is the place when we are going to write our functions. This is the form of the function

the type of the returned value       name of the function ( parameters list on our function)
{
                       actions;
                 return something;
}


every function does something and has to return something.

example we want to calculate the sum of 2 numbers

int sum(int x, int y)
{
      return x+y;
}
the parameters are x and y. When we will call this function in our code we will have to insert 2 integers in our function and we will get the sum of those numbers. X and Y can't be used outside our function, their life starts when we call our function and ends when our functions stops.

If we want our function to do something but do not return anything, in the place where for writing the return type we are writing a word "void". (just like in our main code. "Main" is a function and we write void main() to say that our main function is not going to return anything.)


Functions make our code shorter, easier for understanding. Of course function can be without any parameters.

IF YOU HAVE ANYTHING TO SAY,ASK, SUGGEST PLEASE LEAVE THAT IN COMMENTS.