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....

String in C Exercises & Solutions

String are mainly defined as an array of characters. In simple words it is one dimensional array of characters which is terminated by null ('/0') character.


C Program to Toggle Case of Each Character of a String

Ex: Write a C program to toggle case of each character of a string. How to write a C program to toggle case of each character of a string. C program to toggle case of each character of a string.

Input from user:


Enter the string:

welcome To Codeforhunger.Com

Expected output:


Toggled string:

WELCOME tO cODEFORHUNGER.cOM





  Step by step logic of the given program:


1. Accept string from user declare varible say str.

2. After that calculate length of string using strlen() function and store it in variable say len.

3. Run for loop from 0 to length-1 of string, as follows:
for(i=0;i<len;i++)

4. Inside the body of loop check condition if character is lowercase alphabet then subtract 32 to make it capital and character is Capital alphabet then add 32 to make it small(lowercase), as follows:
if(str[i]>='a'&&str[i]<='z')
{
str[i]-=32;
}
else if(str[i]>='A'&&str[i]<='Z')
{
str[i]+=32;
   }


5. Last outside the block of for loop print toggled string.





  Program to toggle case of each character of a string:


#include<stdio.h>
#include<string.h>

void main()
{
char str[100];
int i,len;

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

len=strlen(str);//calculate length of string

for(i=0;i<len;i++)
{
if(str[i]>='a'&&str[i]<='z')
{
str[i]-=32;//subtract 32 to make it capital
}
else if(str[i]>='A'&&str[i]<='Z')
{
str[i]+=32;//add 32 to make it small
}
}

printf("Toggled string:\n%s",str);
}


Above program shows the following output:


C program to toggle case of each character of a string


C program to print given string in reverse order without using strrev()

Ex: Write a C program to print given string in reverse order without using strrev(). How to write a C program to print given string in reverse order without using strrev(). C program to print given string in reverse order without using strrev(). 

Input from user:

Enter the string:
codeforhunger

Expected output:

Given string in reverse order:
regnuhrofedoc




  Step by step logic of the given program:


1. Accept string from user declare varible say str.

2. After that calculate length using strlen() function and store it in variable len.

3. Run for loop from length of string to 0 which is looks like:
for(i=len;i>=0;i--)

4. Inside the for loop print string characters one by one.





  Program to print given string in reverse order without using strrev():

#include<stdio.h>
#include<string.h>
#define max 100

void main()
{
char str[max];
int i,len=0;

    /*Accept string*/
printf("Enter the string:\n");
gets(str);

len=strlen(str);

printf("Given string in reverse order: \n");
/*print given string in reverse order*/
for(i=len;i>=0;i--)
{
printf("%c",str[i]);
}
}



Above program shows the following output:


C program to print given string in reverse order without using strrev(), C Program to reverse a String




C Program to Copy one String to Another String without using strcpy() Function

Ex: Write a C program to copy one string to another string without using strcpy() function.
How to copy one string to another string without using strcpy() function. C program to copy one string to another string without using strcpy() function.


Input from user:

Enter the string:
I love codeforhunger

Expected output:

First string: I love codeforhunger

Second string: I love codeforhunger




  Step by step logic of the given program:


1. Accept string from user declare varible say str1.

2. Declare another variable to store copy of first string, varible say str2.

3. Run while loop from 0 to end of string.

4. Inside the while loop for each character in str1 copy to str2 using:
str2[i]=str1[i];

5. After loop make sure the copied string ends with NULL character using: 
str2[i]= '\0';





  Program to copy one string to another string without using strcpy() function:

#include<stdio.h>
#include<string.h>
#define max 100

void main()
{
char str1[max],str2[max];//declare string of max size 100
int i=0;
    

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

/*Iterate loop till end of string*/
 while(str1[i]!='\0')
{
/*copy the string */
str2[i]=str1[i];
i++;
}
/*make sure that string is Null terminated*/ 
     str2[i]='\0';

printf("\nFirst string: %s",str2);
printf("\nSecond string: %s",str2);
}



Above program shows the following output:


C program to print given string in reverse order without using strrev()



C Program to Count Total Number of Words in a String

Ex: Write a C program to count total number of words in a string. How to  count total number of words in a string. C Program to count total number of words in a string.

Input from user:

Enter the string:
This is codeforhunger.com

Expected output:

Number of words are: 3


    Required Knowledge:




  Step by step logic of the given program:

1. Accept string from user declare varible say str.

2. Run while loop till end of the string.

3. Inside the while loop check whether the current character is space or new line using:
if(str[i]== ' '||str[i]=='\n')
{
count++;
}

4. After that Print value of  count on the output screen.




  Program to count total number of words in a string:


#include<stdio.h>
#include<string.h>

void main()
{
char str[100];//declare string of max size 100
int i=0,count=1;

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

/*Iterate loop till end of string*/ while(str[i]!='\0')
{
/* check whether the current character is space or new line*/
if(str[i]== ' '||str[i]=='\n')
{
count++;
}
i++;
}
printf("Number of words are:%d",count);
}


Above program shows the following output:


C Program to count total number of words in a string




C program to separate the individual characters from a string

Ex: Write a C program to separate the individual characters from a string. How to write a C program to separate the individual characters from a string. C program to separate the individual characters from a string.

Input from user:


Enter the string:

codeforhunger

Expected output:


String characters are:

c o d e f o r h u n g e r





  Step by step logic of the given program:


1. Accept string from user declare variable say str.

2. Run while loop till end of string.

3. Print characters of string one by one using spaces.




  Program to separate the individual characters from a string :


#include<stdio.h>
#include<string.h>

int main()
{
char str[100];//declare string of size 100
int i=0;
printf("Enter the string:\n");
gets(str);//accept string
printf("String characters are:\n");
while(str[i]!='\0')
{
printf("%c ",str[i]);
i++;
}
return 0;
}


Above program shows the following output:

Enter the string:         
codeforhunger            
String characters are:  
c o d e f o r h u n g e r 


To learn more string exercises click here...

C program to calculate length of string using strlen() function

Ex: Write a C program to calculate length of string using strlen() function. How to calculate length of string using strlen() function. C program to calculate length of string using strlen() function.

Input from user:


Enter the string:
This is codeforhunger

Expected output:

Length of string is: 21





  Step by step logic of the given program:

1. Accept string from user store it in variable say a.


2. After that calculate length of string using strlen() function and store it in variable length.

3. Print length on the output screen.




  Program to calculate length of string using strlen() function:



#include<stdio.h>
#include<string.h>

int main()
{
char a[100];
int length;

printf("Enter the string:\n");
    gets(a);
    
    length=strlen(a);//calculate length of string

printf("Length of string is: %d",length);

return 0;
}



Above program shows the following output:


C program to calculate length of string using strlen() function

C program to accept string from user & print it on the output screen

Ex: Write a C program to accept string from user and print it on the output screen. How to accept string from user and print it on the output screen. C program to accept string from user and print it on the output screen.

Input from user:

Enter the string:
I love codeforhunger

Expected output:

You entered:
I love codeforhunger






  Step by step logic of the given program:

1. Declare string of size 100, declare variable say str.

2. Accept string using gets() function and store it in variable str.

3. Print string on the output screen.




  Program to accept string from user and print it on the output screen:


#include<stdio.h>
#include<string.h>

void main()
{
char str[100];

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

printf("you entered: %s",str);
}



Above program shows the following output:


C program to accept string from user and print it on the output screen