¤ Home » Programming » C Tutorial » Pointers in C - Part 3 of 9

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:




comments powered by Disqus


Web Service Offers

Accounting System for Small Business
5,999 4,999 (16.67% off)
per Year

Ecommerce on the Cloud

An online store hosted on the Cloud. Sell unlimited products. Integrated business functions. Well-suited for high traffic website.

39,999 29,999 (25% off)

Ecommerce Premium

An e-commerce website well-suited for companies having a large portfolio of products. Sell unlimited products. Manage both offline and online sales from one system.

40,000 25,000 (37.5% off)

Ecommerce Business

Launch your online business. Sell up to 5000 products. Collect payment via multiple online and offline payment methods.

35,000 25,000 (28.57% off)

Ecommerce Pro

Get a Mobile friendly online store front. Sell up to 1000 products.

35,000 25,000 (28.57% off)

Ecommerce Starter

Start selling your products online. Scale up as your business grows.

35,000 25,000 (28.57% off)

Website on the Cloud

Custom built, responsive corporate website hosted on the cloud. Mirrored on 3 servers. Includes visitor signup with CRM functions/

19,999 9,999 (50% off)

Website Corporate

Custom built, responsive corporate website with CRM. Our most popular website plan.

19,000 9,000 (52.63% off)

Website Business - 1000 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

18,000 8,000 (55.56% off)

Website Business - 500 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,500 7,500 (57.14% off)

Website Business - 100 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

17,000 7,000 (58.82% off)

Website Business - 50 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,500 6,500 (60.61% off)

Website Business - 25 Pages

Affordable website plan for small business. Custom built - quick page download, automatic SEO, mobile & tablet friendly.

16,000 6,000 (62.5% off)

Website Starter - 5 Pages

Get started with your own website at most affordable cost.

15,000 5,000 (66.67% off)


Looking to build a website?
Launch a 5 page website in 1 day at only Rs.499/year. Hosting included.





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 XLRI, industry professionals, and govt. officials.

Rajeev has founded Computer Solutions & WebServicesWorldwide.com, and has hands-on experience of building variety of web applications and portals, that include - SAAS based ERP & e-commerce systems, independent B2B, B2C, Matrimonial & Job portals, and many more.



Copyright © How2Lab.com. All rights reserved.

Refer a friend | Sitemap | Disclaimer | Privacy Policy