Copyright © Programs ++
Design by Dzignine
Tuesday 22 May 2012

Use of string iterator in C++

The following program computes the length of the string using the string iterator

// Program to demonstrate the use of string iterator
#include <iostream>
#include <string>

int main()
{
     using namespace std;
     int counter=0;
     string str("welcome to programsplusplus");
     // declares the string iterator
     string::iterator it;
     for (it=str.begin(); it<str.end(); it++)
     {
          // displays each character that the iterator points to
          counter++;
          cout<<" "<<*it;
     }
     cout<<"\n"<<"Total length of the string : "<<counter;

     return 0;
} // end of main
------OUTPUT------

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Monday 21 May 2012

Methods to initialize string in C++

// Various methods to initialize strings in C++

#include <iostream>
#include <string>

using namespace std;

int main()
{
     string s0("welcome to programsplusplus");
     string s1(s0);
     std::cout<<s1;

     string s2(s0,8,3);
     std::cout<<"\n"<<s2;

     string s4("hello world",4);
     std::cout<<"\n"<<s4;

     string s5(10,'x');
     std::cout<<"\n"<<s5;

     string s6(10,42);
     std::cout<<"\n"<<s6;

     string s7(s0.begin(),s0.begin()+5);
     std::cout<<"\n"<<s7;

     return 0;
}
------ OUTPUT------

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.
Sunday 20 May 2012

C++ implementation of linked list

// C++ implementation of a linked list

#include <iostream>

struct Node // declares the node structure
{
     int info;
     Node *next;
}*start,*ptr,*newptr;

Node *new_list(int x) // creates a new list
{
     ptr = new Node;
     ptr->info = x;
     ptr->next = NULL;
     return ptr;
}

void insert_beg(Node *np) // inserts an element in list
{
     if ( start == NULL )
          start = np;
     else
     {
          Node *save = new Node;
          save = start;
          start = np;
          np->next = save;
     }
}

void display(Node *np) // displays the list
{
     while ( np != NULL )
     {
          std::cout<<np->info<<" -> ";
          np = np->next;
     }
}

int main()
{
     int item,choice;
     do
     {
          std::cout<<" \n1 : Create a new list ";
          std::cout<<" \n2 : Insert at beginning ";
          std::cout<<" \n3 : Display List ";
          std::cout<<" \n4 : Exit\n ";
          std::cout<<"\n Enter your choide : ";
          std::cin>>choice;

          switch(choice)
          {
               case 1 :
                    std::cout<<" \nEnter some value : ";
                    std::cin>>item;
                    start = new_list(item);
                    std::cout<<" List created !!! ";
                    break;
               case 2 :
                    std::cout<<" Enter an item to insert : ";
                    std::cin>>item;
                    newptr = new Node;
                    newptr->info = item;
                    newptr->next = NULL;
                    insert_beg(newptr);
                    break;
               case 3 :
                    std::cout<<" The list is as follows : \n";
                    display(start);
                    break;
          } // end of switch

     }while ( choice != 4 ); // end of do while
     return 0;
}// end of main
------OUTPUT------

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.


Friday 4 May 2012

Namespace example in C++

// Program to demonstrate the use of namespaces

#include <iostream>

namespace other // defines a namesapce
{
          int sum(int,int); // function prototype
}

// function sum() that belong to the other namespace which returns the sum of two //integers
int other :: sum(int x,int y)
{     return x+y;
}
/* another function sum() that does not belong to the namespace 'other' and returns the
   difference of two integers passed as arguments */
int sum(int x,int y)
{   return x-y;
}
 
int main()
{
    int a,b;
    // prompts the user to enter two integer values a and b
    std::cout<<" Enter the value of a : ";
    std::cin>>a;
    std::cout<<" Enter the value of b : ";
    std::cin>>b;
    // calls the function declared under the 'other' namespace
    // notice the other:: is used to call this function
    std::cout<<"\n Sum() function of 'other' namespace : "<<other::sum(a,b);
    // normal sum() function that actually computes the difference of a and b
    std::cout<<"\n Normal Sum() function : "<<sum(a,b);
    return 0;
}// end of main
------ OUTPUT ------








 

Please do comment if you don't understand any part or want to know more or just want to say thanks. I love programming and love to teach my friends. Your suggestions and appreciation will make this blog much better.