C program to concatenate strings using pointers

 Ex: Write a C program to concatenate strings using pointers. How to write a C program to concatenate strings using pointers. C program to concatenate strings using pointers.



Input from user:

Enter first string:

codefor

Enter second string:

hunger


Expected output:

Concatenated string:

codeforhunger




  Step by step logic of the given program:


1. Accept two strings from user declare variables say str1 and str2.


2. Run while loop till end of first string:

while(*s1)

     {

  s1++;

     }


3. Run while loop till end of second string and inside the while loop copy second string to first string:

while(*s2)

        {

*s1=*s2;

s1++;

s2++;

}


4. After that end first string with NULL(\0).


5. Last, print concatenated string str1.




  C program to concatenate two strings using pointers:


#include<stdio.h>

#define max 100//declare max size 100

int main()

{

char str1[max],str2[max];

        /*declare pointers for str1 and str2*/

char *s1=str1,*s2=str2;

printf("Enter first string:\n");

gets(str1);

printf("Enter second string:\n");

gets(str2);

while(*s1)/*till end of the first string*/

{

s1++;/*point to the next character*/

}

while(*s2)/*till end of the second string*/

{

*s1=*s2;

s1++;

s2++;

}

/*Make sure that str1 end with NULL*/

    *s1='\0';

    printf("Concatenated string is:\n%s",str1);

return 0;

}



Above program shows the following output:


C program to concatenate strings using pointers



C Program to Find Length of String Using Pointers

Ex: Write a C program to find length of string using pointers. How to write a C program to find length of string using pointers. C program to find length of string using pointers.


Input from user:

Enter the string: 

Hi this is codeforhunger


Expected output:

Length of string= 24



  Step by step logic of the given program:


1. Accept input(string) from user store it in some variable say str.


2. After that use while loop(here you can use any other loop also) to iterate string through last element:

while(*ptr!='\0')


3. Inside the loop increment counter by 1(to calculate length of string). i.e, count++;


4. After that print value of count(length of string) on the output screen.




  Program to find length of string using pointers:


#include<stdio.h>

int main()

{

char str[100];//declare string size 100

char *ptr=str;/*declare pointer which points to string(str)*/

int count=0;

printf("Enter the string:\n");

gets(str);

/*Iterate string through last element*/

while(*ptr!='\0')

{

count++;

ptr++;

}

printf("Length of string= %d",count);

return 0;

}


Above program shows the following output:


C program to calculate length of the string using pointers, what are pointers in C programming language, how to learn c programming pointes

C Program to Print Array Elements Using Pointers

Ex: Write a C program to print array elements using pointers. How to write a C program to print array elements using pointers. C program to print array elements using pointers.


Input from user:

Enter how many elements you are going to enter: 5

Enter elements:

10

20

30

40

50


Expected output:

Array elements are:

10

20

30

40

50




  Step by step logic of the given program:


1. Accept limit from user declare variable say no.


2. Accept elements using pointer variable ptr and move pointer to next location by using ptr++.


3. After that set pointer back to first array element by using:

ptr=arr;


4. Last, print pointed values by using  dereference operator (*):

 *ptr.





  C Program to Print Array Elements Using Pointers:


#include<stdio.h>

int main()

{

int arr[100],i,no;

int *ptr=arr;//declare pointer to arr[0]

printf("Enter how many elements you are going to enter:\n");

scanf("%d",&no);

printf("Enter elements:\n");

for(i=0;i<no;i++)

{

scanf("%d",ptr);

/*move pointer to next array element*/

ptr++;

}

       /* Set pointer back to first array element*/

ptr=arr;

printf("Array elements are:\n");

{

for(i=0;i<no;i++)

{

//print pointed values

printf("arr[%d]=%d\n",i,*ptr);

/*move pointer to next array element*/

ptr++;

}

}

return 0;

}


Above program shows the following output:


C Program to Print Array Elements Using Pointers

C Program to Swap Two Numbers Using Pointers

 Ex: Write a C program to swap two numbers using pointers. How to write a C program to swap two numbers using pointers. C program to swap two numbers using pointers.


Input from user:

Enter first number: 10

Enter second number: 20


Expected output:

Numbers after swapping:

Number1=20

Number2=10



In this program we will use two pointer operators:

1. Reference operator (&)

2. Dereference operator (*)




  Step by step logic of the given program:


1. Accept two numbers from user declare variable say no1 & no2.


2. Use third variable(temp) to swap two numbers, shown as follow:

temp=*ptr1;

*ptr1=*ptr2;

*ptr2=temp;


3. Last print swapped numbers.




  C Program to Swap Two Numbers Using Pointers :


#include<stdio.h>

int main()

{


int no1,no2,temp;      

int *ptr1=&no1,*ptr2=&no2;

printf("Enter first number:\n");

scanf("%d",&no1);

printf("Enter second number:\n");

scanf("%d",&no2);  


         temp=*ptr1;

*ptr1=*ptr2;

*ptr2=temp;


printf("Numbers after swapping:\nNumber1=%d\nNumber2=%d",*ptr1,*ptr2);


        return 0;


}


Above program shows the following output:


C Program to Swap Two Numbers Using Pointers


C Program to Find Maximum Between Two Numbers Using Pointers

Ex: Write a C program to find maximum between two numbers. How to write a C program to find maximum between two numbers. C program to find maximum between two numbers.


Input from user:

Enter first number: 25

Enter second number: 35


Expected output:

Maximum number is 35


C Program to Perform All Arithmetic Operations using Pointers

Ex: Write a C program to perform all arithmetic operations using pointers. How to write a C program to perform all arithmetic operations using pointers. C program to perform all arithmetic operations using pointers.


Input from user:

Enter Number1: 20

Enter Number2: 10


Expected output:

Sum=30

Subtraction=10

Multiplication=200

Division=2.0000


Before learning how to write a C program to perform all arithmetic operations we need to know what is arithmetic operators means.


  What is Arithmetic Operators:

Arithmetic Operators are the operators which are used to perform various mathematical operations such as addition, subtraction, division, multiplication etc.

For Examples: 

1. Plus (+): is used for addition like 5 + 5 = 10, 35 + 35 = 70, 10 + 13 + 25 = 48  etc.

2. Minus Operator (-): is used for subtract one number from another number like 23 - 8 = 15, 65 - 25 = 40.

3. Multiplication Operator (*) : is represented as an asterisk (*) symbol and it returns product of  given numbers like 5 * 5 = 25, 7 * 12 = 84 etc.

4. Division Operator (/) : is denoted by  forward slash (/)  symbol and it divides first number by the second number like 35 / 5 = 7.


5. Modulus Operator (%) : is denoted by percentage (%) symbol and it returns remainder. for ex: 25 / 5 = 0, 45 / 6 = 3 etc.


In below example we have performed addition, subtraction, multiplication and division operations using pointers in C. 


  Quick Links:


  Step by step logic of the given program:


1. Accept two numbers from user store it in variable say no1 and no2.

2. Store address of no1 in pointer variable ptr1 and store address of no2 in variable ptr2 using reference operator (&):

ptr1=&no1;

ptr2=&no2;

3. After that calculate addition, subtraction, multiplication and division using dereference operator(*):

sum=(*ptr1) + (*ptr2);

sub=(*ptr1) - (*ptr2);

mult=(*ptr1) * (*ptr2);

div=(*ptr1) / (*ptr2);

4. Last print sum, sub, mult and div on the output screen.



  C Program to Perform All Arithmetic Operations using Pointers:

#include<stdio.h>

int main()

{

int no1,no2;

int *ptr1,*ptr2;

        int sum,sub,mult;

        float div;   

    printf("Enter number1:\n");

    scanf("%d",&no1);

    printf("Enter number2:\n");

    scanf("%d",&no2);

    

    ptr1=&no1;//ptr1 stores address of no1

    ptr2=&no2;//ptr2 stores address of no2

    

    sum=(*ptr1) + (*ptr2);

    sub=(*ptr1) - (*ptr2);

    mult=(*ptr1) * (*ptr2);

    div=(*ptr1) / (*ptr2);

    

    printf("sum= %d\n",sum);

    printf("subtraction= %d\n",sub);

    printf("Multiplication= %d\n",mult);

    printf("Division= %f\n",div);


    return 0;

}



Above program shows the following output:


C Program to Perform All Arithmetic Operations using Pointers



Feel free to Share your thoughts below in the comment sections.


    You May Also Like:

C Program to Add Two Numbers using Pointers

Ex: Write a C program to add two numbers using pointers. How to write C program to add two numbers using pointers. C program to add two numbers using pointers.


Input from user:

Enter Number1: 10

Enter Number2: 20


Expected output:

Sum= 30





  Step by step logic of the given program:


1. Accept input (numbers) from user.


2. Store address of no1 in pointer variable ptr1 and store address of no2 in variable ptr2 using reference operator (&):

ptr1=&no1;

ptr2=&no2;


3. After that add two numbers using dereference operator(*):

sum=*ptr1+*ptr2;


4. Last print sum on the output screen.




  Program to Add Two Numbers using Pointers:


#include<stdio.h>

int main()

{

int no1,no2,sum;

int *ptr1,*ptr2;

printf("Enter number1:\n");

scanf("%d",&no1);

printf("Enter number2:\n");

scanf("%d",&no2);

ptr1=&no1;// it stores address of no1

ptr2=&no2;// it stores address of no2

sum=*ptr1+*ptr2;// it adds values at address no1 & no2

printf("Sum= %d",sum);

return 0;

}


Above program shows the following output:


C Program to Add Two Numbers using Pointers



C Program to Show How to Handle Pointers

If you have ever found yourself puzzled by pointers, don't worry; you are not alone because pointers might sound tricky. But fear not! In this post, we are going to explore the basics of handling pointers in C through a simple and easy-to-follow program which shows how to handle pointers in C language.


  Step by Step logic of the given program:


1. First declare integer pointer variable a, declare integer variable z assign z=10.


2. Print value of z and address of z using &z.


3. After that assign a with address of z using:

a=&z;


4. Print address of pointer a using %p. It prints the address of z because we already stored address of z in variable a.


5. After that print content of pointer a using *a  it will print value of z=10.


6. After that assign z=30.


7. Print address of a using %p it will print address of z as it is. After that print content of pointer a using *a it will print changed value of z that means it will print 30.


8. After that assign value 5 to the pointer variable *a. Then print address of z and print value of z it will print value 5 because a contain the address of z.





  Program to Show Working of Pointers:


#include<stdio.h>

int main()

{

int* a;

int z;

z=10;

printf("How works pointer and how to handle pointers-C program:\n\n");

printf("Value of z = %d\n",z);

printf("Address of z = %p\n",&z);

a=&z;

//Now, a is assigned with the address of z.

printf("\nNow a is assigned with the address of z:\n");

printf("Address of pointer a = %p\n",a);

printf("content of pointer a = %d\n\n",*a);

z=30;//now value of z is 30

printf("Now, The value of z assigned to 30:\n");

printf("Address of pointer a = %p\n",a);

   printf(" Content of pointer a = %d\n\n",*a);

    *a=5;

   printf("Now, pointer variable a is assigned the value 5\n");

   printf("Address of z = %p\n",&z);

   /*here, a contain the address of z, so *a changed the value of z (now z=7)*/

   printf("Value of z = %d",z);

   return 0;

}



Above program shows the following output:


C Program to Show How to Handle Pointers


You can explore more examples of pointers 👉 Here..

Pointers in C Exercises and Solutions

In this tutorial you will learn pointer syntax, working, advantages and disadvantages with lot of pointer exercises and examples with their solution's.


Definition: In simple words pointer is a variable that stores address (memory location) of another variable's. This means that a pointer holds the memory address of another variable.



There are two main operator's used in pointer:


1. Reference operator & 
2. Dereference operator *



  Exercises and Solutions of Pointers in C:

keep visiting...keep learning... :)

Pointer in C Definition, Syntax & Working with Advantages & Disadvantages

Definition: In simple words pointer is a variable that stores address (memory location) of another variable's. This means that a pointer holds the memory address of another variable.



   There are two main operator's used in pointer:


1. Reference operator & 
2. Dereference operator *


1. Reference operator (&):


 The & is a unary operator that returns the memory address of its operand.



   Syntax of reference operator &:

&variable-name;

 
  • For Example:

If you have any variable var in your program, &var will give you it's memory address.

We have already used address(&) numerous times while using the scanf() function:

scanf("%d",&var);




  Examples program to print address of variable:


Here, %p is format specifier which is used to print the address of a pointer.
 

#include<stdio.h>
int main()
{
// declare variable 
int var;

printf("Address is: %p\n", &var);

return 0;
}


Above program shows the following output:

Pointer in C Definition, Syntax & Working with Advantages & Disadvantages



2. Dereference operator (*):


The * is a unary operator that returns the value of the variable at the address specified by its operand.

   Syntax of dereference operator (*):

*memory-address or pointer-variable;




  Example program of dereference operator (*):


#include<stdio.h>
int main()
{
int var=10;
/* It prints value pointed by (&var)*/
printf("Pointed value is: %d",*(&var));
return 0;
}

Above program shows the following output:

Example program of dereference operator



   Declaration syntax of pointer in C:



This is the general syntax of pointer. 

  • Here, * symbol specifies it is a pointer variable. You must prefix * before declare varible to it is pointer.
  • pointer_variable_name is the name of pointer variable.


Datatype of a pointer must be same as the data type of the variable to which the pointer variable is pointing.


  • For Example:

int *i; // pointer to intiger variable
char *c; //pointer to character variable
float *f; // pointer to float variable



  Example Program of Pointer:


#include <stdio.h>
int main()
{ 

    int num = 10;
    int *ptr = &num;
    // Here,  pointer points the value of num

    printf("Value of num = %d \n", num);   
 printf("Address of num = %x \n\n", &num);

    printf("Value of ptr = %x \n",ptr);
 printf("Address of ptr = %x \n", &ptr);
   

 return 0;

}


In this program %x is used to print hexadecimal values.


Above program shows the following output:

Example program of pointer




  Advantages of using pointer:


1. Pointer provide direct memory access.

2. It reduces the execution time of the program.

3. It reduces the storage space and complexity of the program.

4. Pointer provides an alternate way to access array elements.

5. Using pointers, arrays and structures can be handled in more efficient way.



  Disadvantages of using pointer:


1. In pointer if sufficient memory is not available for the storage of pointers during runtime, the program may crash.

2. Pointers are slower than normal variables.

3. Uninitialized pointers might cause segmentation fault.


Learn Pointers with more Examples....