How2Lab Logo
tech guide & how tos..


Pointers in C - Part 3 of 9


Similar to a one dimensional integer array, the name of a character array or string holds the address of the starting element. However, unlike the integer array, a character array is always terminated by a null (\0) terminator. Thus a character pointer can directly be assigned with a string constant. For example, consider the following initialization:

char *city_name = "Jamshedpur - the city to live";

Such a statement however, can only appear outside main(). To incorporate the same inside the main() function, the storage class static should be used as a prefix to the previous statement.

static char *city_name = "Jamshedpur - the city to live";

Since, city_name is a pointer to a character, it can be incremented or decremented to traverse along the string. But it is always a better practice to retain the starting address of the string, because losing that may create difficulty in accessing the complete string. You may use a second pointer to traverse the string.


The following programs illustrate usage of pointers for string manipulation:

Program to compute the length of a string
#include <stdio.h>
main()
{ 
	char string[80], *ptr;
	ptr = string; 

	printf("Enter the string to find it's length\n"); 

	/* Read the string */  
	while((*ptr++ = getchar()) != '\n');
	* --ptr = '\0';

	printf("\n String is: %s\n", string);
	printf("It's length is: %d\n", (ptr - string)); 
}

Program to concatenate two Strings
#include <stdio.h>
#include <malloc.h>
main()
{
	/* s1, s2 and s3 are pointers to characters */ 
	char *s1, *s2, *s3, c;
	int i, j, len = 81;

	/* allocate memory */
	s1 = (char *) malloc(len * sizeof(char));
	s2 = (char *) malloc(len * sizeof(char));
	s3 = (char *) malloc((2 * len - 1) * sizeof(char));

	printf("Enter string 1 (length should not exceed 80 chars)\n");
	gets(s1);
	printf("String entered is: %s\n", s1);

	printf("\nEnter string 2 (length should not exceed 80 chars)\n");
	gets(s2);
	printf("String entered is: %s\n", s2);

	i = 0;
	while((c = *(s1 + i)) != '\0') 
	{
		*(s3 + i) = c;
		i++;
	}

	j = 0;
	while((c = *(s2 + j)) != '\0')
	{
		*(s3 + i + j) = c;
		j++;
	}
	*(s3 + i + j) = '\0';
	printf("\nThe Concatenated string is: %s\n", s3);
}

Exercise

State what the following C function will do.

void fun(char *s, char *t)
{
	while((*s++ = *t++)  != '\0');
}

Lab Work

  1. Write a C function count_substr(string 1, string2) which returns the number of occurrences of string1 in string2. For example if string1 = "aba" and string2 = "cababadeabaf", then this function must return 3.
  2. Write a C program to receive a line of text from the user as input and display it on the screen backwards.
  3. Write a C program to check whether the string provided as input is a palindrome or not. The example of a palindrome is "madam" which reads same whichever way you read, left to right or right to left.

Share:
Buy Domain & Hosting from a trusted company
Web Services Worldwide | Hostinger
About the Author
Rajeev Kumar
CEO, Computer Solutions
Jamshedpur, India

Rajeev Kumar is the primary author of How2Lab. He is a B.Tech. from IIT Kanpur with several years of experience in IT education and Software development. He has taught a wide spectrum of people including fresh young talents, students of premier engineering colleges & management institutes, and IT professionals.

Rajeev has founded Computer Solutions & Web Services Worldwide. He has hands-on experience of building variety of websites and business applications, that include - SaaS based erp & e-commerce systems, and cloud deployed operations management software for health-care, manufacturing and other industries.


Refer a friendSitemapDisclaimerPrivacy
Copyright © How2Lab.com. All rights reserved.