codescracker


c

C Strings



« Previous Tutorial Next Tutorial »


In C, a string is simply a null-terminated character array. In other words, a string is actually a single dimensional array of characters, terminated by null character ('\0').

Declare a String in C

Here is an example showing how to declare a string in C:

char str[80];

Here, str can hold upto 80 characters including null terminated character (i.e. '\0').

Note - Whenever declaring a character array that will hold a string, you must have to declare it to be one character longer than the largest string, that it will hold.

Note - The compiler automatically adds the null to the end of the string constant.

A string constant is a list of characters enclosed in double quotes. Here is an example.

"i am string"

C String Example

Here is an example program demonstrating the array of character of simply strings.

/* C Strings - This program illustrates the
 * concept of array of characters or simply
 * strings in C language */

#include<stdio.h>
#include<conio.h>
void main()
{
	char str[40] = "Hello Compiler, I am a String";
	clrscr();

	printf("%s", str);

	getch();
}

Here is the sample output of the above C program:

c strings

Here is another example. This program initialize strings to str entered by the user.

/* C Strings */

#include<stdio.h>
#include<conio.h>
void main()
{
	char str[40];
	clrscr();

	printf("Enter a sentence: ");
	gets(str);
	printf("\nYou entered:\n");
	printf("%s", str);

	getch();
}

Below is a sample run of the above C program:

c string

Functions to Manipulate Strings in C

C provides a lot of functions that manipulates strings, such as strcat(), strchr(), strcmp(), strcoll(), strcpy(), strcspn(), strerror(), strlen(), strncat(), strncmp(), strncpy(), strpbrk(), strrchr(), strspn(), strstr(), strtok() etc. Here the following table lists the most common function that manipulate strings in C:

Function Name Works
strcpy(s1,s2) Copies s2 into s1
strcat(s1,s2) Concatenates s2 onto the end of s1
strlen(s) Returns the length of s
strcmp(s1,s2) Returns 0, if s1 and s2 are same.
Returns less than 0, if s1 is less s2.
Returns greater than 0, if s1 is greater than s2.
strchr(s,ch) Returns a pointer to the first occurrence of ch in s
strstr(s1,s2) Returns a pointer to the first occurrence of s2 in s1

To see in detail about all the functions that manipulate strings and characters, then refer C String and Character Functions.

Now let's take some examples demonstrating strings in C language.

/* C Strings */

#include<stdio.h>
#include<conio.h>
void main()
{
	char str1[80], str2[80];
	clrscr();

	printf("Enter any two strings: ");
	gets(str1);
	gets(str2);

	printf("\nLength of first string is %d\n", strlen(str1));
	printf("Length of second string is %d\n", strlen(str2));

	if(!strcmp(str1, str2))
	{
		printf("\nThe strings are equal\n");
	}
	strcat(str1, str2);
	printf("\nNow the string 1 becomes\n");
	printf("%s", str1);

	getch();
}

Here is the two sample run of the above C program. The first one, when user enters equal string. The second one, when user enters unequal strings.

strings in c

string in c

Here is one more example. This program ask to the user to enter the two string and then a character. Then this programs checks whether the character present in the first string or not. If present then a message will be displayed telling that the character is present in the first string otherwise no message will be displayed. And then this program checks whether the second string is present in the first string or not. If present then a message will displayed saying that this string is present in the first string. Otherwise, no message will be displayed.:

/* C Strings */

#include<stdio.h>
#include<conio.h>
void main()
{
	char str1[80], str2[80];
	char ch;
	clrscr();

	printf("Enter any two strings: ");
	gets(str1);
	gets(str2);
	fflush(stdin);
	printf("Enter a character: ");
	scanf("%c", &ch);

	if(strchr(str1, ch))
	{
		printf("%c is present in the first string\n", ch);
	}
	if(strstr(str1, str2))
	{
		printf("Second string found in the first string\n");
	}

	getch();
}

Here are 4 sample run of this C program given.

c string example

c string programs

c strings example

c strings program

More Examples

Here are some more examples demonstrating strings in C, that you can go for:


« Previous Tutorial Next Tutorial »



Tools
Calculator

Quick Links
Signup - Login - Give Online Test