Showing posts with label pointer. Show all posts

Write a program in c language to compare two strings by using pointers.

#include <stdio.h>
#include <conio.h>

int compare_strings(char*, char*);

int main()
{
    char str1[100], str2[100], result;

    printf("Enter first string: ");
    gets(str1);

    printf("Enter second string: ");
    gets(str2);

    result = compare_strings(str1, str2);

    if ( result == 0 )
       printf("Strings are same.\n");
    else
       printf("Entered strings are not equal.\n");

    getch();
}

int compare_strings(char *str1, char *str2)
{
   while(*str1==*str2)
   {
      if ( *str1 == '\0' || *str2 == '\0' )
         break;

      str1++;
      str2++;
   }
   if( *str1 == '\0' && *str2 == '\0' )
      return 0;
   else
      return 1;
}

Write a program to add two numbers by using pointers in C++ language.

#include <iostream>
using namespace std;

int main()
{
    int a, b, *p1, *p2, sum;
    cout << "Enter both numbers: ";
    cin >> a >> b;
    p1 = &a;
    p2 = &b;
    sum = *p1 + *p2;
    cout << "Sum is " << sum <<endl;
    system("pause");
}

Write a program in c language to reverse a string using recursion.

#include<stdio.h>
#include <conio.h>
char* reverse(char[]); //function declaration

int main()
{

    char number[100],*rev;

    printf("Enter any string/number: ");
    scanf("%s",number);
    rev = reverse(number);
    printf("Reversed of given string/number is: %s",rev);
    getch();
}
//function definition.
char* reverse(char number[])
{

    static int i=0;
    static char rev[100];

    if(*number){
         reverse(number+1);
         rev[i++] = *number;
    }

    return rev;
}

write a Program to Store values of two variables through pointer .


#include<stdio.h>
#include<conio.h>
int main()
{
    int a,b;
    int *x,*y;
    a=12;
    b=2;
    x=&a;
    y=&b;
    printf("the value of a and b is:\n");
    printf("%d and %d",*x,*y);
    getch();
}

write a Program to Store Address of two variables through pointer .


#include<stdio.h>
#include<conio.h>
int main()
{
    int a,b;
    int *x,*y;
    a=12;
    b=2;
    x=&a;
    y=&b;
    printf("memory adress is a and b is  %d and %d:",x,y);
    getch();
}

Write a Program To Add Two Number Through Pointer


#include<conio.h>
#include<stdio.h>
int main()
{
    int a=3;
    int b=5;
    int *y,*z;
    y=&a;
    z=&b;
    int add;
    add=*y+*z;
    printf("enter the value of a and b\n");
    printf("%d+%d =%d",*y,*z,add);// if u put the value of a and b then also nothing happen:
    getch();
}