Monday, January 31, 2011

Pointers and Arrays in C++

Sorting arrays using pointers.
If this program make any errors please let me know,i will try to help you out.

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

using namespace std;

main()

{
int arraysize=5;
int a[arraysize],i,j,temp;
int *ptr,*ptr1;
cout<<"enter values in array:"<<endl;
for(i=0;i<arraysize;i++)
{
                cin>>a[i];
                }
ptr=&a[0];
ptr1=&a[1];
for(i=0;i<arraysize;i++)
for(j=0;j<arraysize;j++)
{
if(*ptr>*ptr1)
{
          
temp=*ptr;
*ptr=*ptr1;
*ptr1=temp;

}
ptr++;
ptr1++;
}
cout<<"********Sorted array is***********"<<endl;
for(i=0;i<arraysize;i++)
cout<<*ptr++<<"\t";






      getch();
      return 0;
      }

Pointers and Arrays in C++

User-defined copy strings using pointers.
If this program make any errors please let me know,i will try to help you out.

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

main()
{
char name[10],name1[10]={0};
cout<<"Enter the name:"<<endl;
gets(name);
cout<<endl;
cout<<"the name entered is:"<<endl;
puts(name);
char *ptr,*ptr1;
ptr=name;
ptr1=name1;
while(*ptr!='\0')
{
*ptr1=*ptr;
*ptr++;
*ptr1++;
}
cout<<endl;
cout<<"Copied string is:"<<endl;
puts(name1);
getch();
return 0;
}

Pointers and Arrays in C++

User-defined concatenation of stings,using pointers.
If this program make any errors please let me know,i will try to help you out.

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
void myconct(char *,char *);
main()
{
char a[80],b[80];
cout<<"Enter 1st string:";
gets(a);
cout<<"Enter 2nd string:";
gets(b);
char *p1,*p2;
p1=a;p2=b;
myconct(p1,p2);
getch();
      return 0;
      }
void myconct(char *a,char *b)
{
  int l1,l2;
  l1=strlen(a);
  l2=strlen(b);
  int l;
  l=l1+l2;
  int i=0;
   
for(int i=0;i<=l2;i++)

{

a[l1+i]=b[i];

}

cout<<a;

}
                   
                     








    

Arrays in C++

Finding a string length by a user-defined function(instead of strlen())
If this program make any errors please let me know,i will try to help you out.

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;

int mystrlength(char [],int);
main()
{int arraysize=5;
char arr[arraysize];
cout<<"enter the string:";
gets(arr);
int l,x;
l=strlen(arr);
cout<<"The string length by built-in fuction is:"<<l<<endl;
x=mystrlength(arr,arraysize);
cout<<"the string length by my function is:"<<x;

getch();

}
int mystrlength(char arr[],int arraysize)
{
    int count=0;
    while(arr[count]='\0')
    {
    count++;
}
    return count;
}
 

Pointers in C++

Searching an element in a string through pointers.
If this program make any errors please let me know,i will try to help you out.

#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int found(char *);
main()
{const int arraysize=50;
char a[arraysize],letter;
int l,count=0;
cout<<"Enter a string:";
gets(a);
char *p;
p=a;
l=found(p);
if (l==0)
cout<<"letter not found";
else
cout<<"letter is found "<<l<<" times";
      getch();
      return 0;
      }
int found(char *a)
{
    char letter;
    int count=0;
    cout<<"Enter the letter to be find:";
    cin>>letter;
    while(*a!='\0')
    {
    if(*a==letter)
    {
                  count++;
    }
    a++;
    }
    return count;
}

Arrays in C++

Concatenation  of two string arrays.
If this program make any errors please let me know,i will try to help you out.

# include <iostream>
# include <conio.h>
#include<string.h>
using namespace std;
void mystringconc(char [],char []);
main()
{
char a[30],b[30];
int i,j;
i=j=0;
cout<<"Enter the first name:";
gets(a);
cout<<"Enter the second name:"<<endl;
gets(b);
mystringconc(a,b);
getch();
return 0;
}
void mystringconc(char a[],char b[])
{
     int i,j;
     char c[60];
     i=j=0;
while(a[i]!='\0')
{
c[i]=a[i];
i++;
}
c[i]='\0';
while(b[j]!='\0')
{
    c[i]=b[j];
    j++;
    i++;
}
c[i]='\0';
cout<<"your first name is:"<<a<<endl;
cout<<"Your second name is:"<<b<<endl;
cout<<"your full name is :"<<c;                 
}

Pointers and Arrays in C++

Comparing string arrays using pointers.
If this program makes errors please let me know,i will try to help you out..
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
int mycomp(char *,char *);
main()
{
      const int arraysize=50;
      char a[arraysize],b[arraysize];
     
      int l;
      char *p,*p1;
      p=a;
      p1=b;
      cout<<"Enter first string:";
      gets(a);
      cout<<"Enter second string:";
      gets(b);
      l=mycomp(p,p1);
      if(l==0)
      cout<<"Strings are equal";
      else
      cout<<"Strings are not equal";
      getch();
      return 0;
      }
int mycomp(char *a,char *b)
{
      int flag=0;
      if (strlen(a)!=strlen(b))
      flag=1;
      else
      {
          int i=0;
      int j=0;
      while(a[i]!='\0')
      {
      if(a[i++]!= b[j++])
      {
                        flag=1;
                        break;
                        }
                        }
                        }
                        return flag;
                        }