String in c

If you want to know string in c programming language then read the complete article.


What is a string in c ?

String in c is array of charactrs. We can access the string like how we access the array. In c programming language there is no datatype for string. But string is implemented as array of characters. That character array which forms a string is to have its last character as '\0'.

c string is an array of characters.

How to declare string in c programming language?

Simply use the array of characters as follows.

char example_string[ 15 ];

In c programming string is immutable. The reason is simple because its simple array of characters we can access any element is an array by its index.


How to initialize a string in c

char example_string[ 15 ]="Hello world";

You can also simply assign each character by character to the array index then terminate it with null character.

char example_string[ 15 ]={ 'H','e,'l', 'l','o',','w','o','r','l','d','\0'};

If you dont know what is the length of the string that you are assigning then simply follow method as below:

char example_string[ ]="Hello world";

Note you cannot assing string directly. So use strcpy function.

Checkout the example program

Example program

#include<stdio.h>
int main(int argc, char const *argv[])
{
	char str[15]= "Hello World";
	printf("\n%s\n",str);
	return 0;
}
string in c

How to input and output a string in c ?

We can simply use printf( ) and scanf( ) functions to output and input respectively.

%s is the format specifier for string in c ‘.

Checkout below program to read input string and output the same string in c.

We can aslo use fgets( ) and fputs( ) functions to input and output respectively.

#include<stdio.h>
#include<string.h>
int main(int argc, char const *argv[])
{
	char string[100];
	printf("Enter a string to print it using scanf and printf\n");
	scanf("%s",string);
	printf("\nEntered string is:\n");
	printf("%s\n",string);
	return 0;
}
input and output a string in c

How to read a line to string in c ?

If you want read a line then simply use regular expression in format specifier like below.

% [^\n]%*c. It is a regular expression which means accept n number of characters until it encounters new line i.e. \n.

example program

#include<stdio.h>
int main(int argc, char const *argv[])
{
	printf("Read a line in c\nEnter a string\n");
	char line[100];
	scanf("%[^\n]%*c",line);
	printf("%s\n",line);
	return 0;
}
How to read a line in c string

Basic String functions in c

C provides various functions to manipulate a string i.e. character array.

These functions are defined in string header file. So you have to include string header file.

#include <string.h>

Function nameDescription
strcatIt appends string s2 to string s1 passed in parameter
strchrIt finds first occurence of a character passed in parameter
strlenIt returns the size of the string array.
strncatIt appends n characters of string s2 to string s1 passed in parameter
strcmpIt compares the string s1 with s2.
strcpyIt copies string s2 to string s1 provided in parameter.
strrchrIt finds last occurence of a character passed in parameter
strncpyIt copies n characters of string s2 to string s1 provided in parameter.
strstrIt returns first occurence of s2 in s1.

Basic String functions in c example program

#include<stdio.h>
#include<string.h>
int main(int argc, char const *argv[])
{
	char str1[100]="Hello world";
	char str2[100]="hello world";
	//now lets compare the string
	int comparisn_value= strcmp ( str1,str2 );
	if(comparisn_value==0)
	{
		printf("\nBoth strings are equal.");
	}
	else if(comparisn_value>0  )
	{
		printf("\nString 1 is greater than string 2.");
	}
	else if(comparisn_value<0  )
	{
		printf("\nString 2 is greater than string 1.");
	}
	//lets copy str2 to str 1
	strcpy(str1,str2);
	printf("\n%s",str1);
	//lets print the string length
	printf("\nString1 length  is %d ",(int)strlen(str1));
	//strncpy
	strncpy(str2,str1,5);
	printf("\n String 2 after copying 5 elements %s",str2);
	//lets concatenate the stringd
	strcat(str1,str2);
	printf("\nstring after concatenating:\n%s",str1);
	printf("\nOther functions try it yourself.\n");
	return 0;
}
String functions in c

Strings and pointers in c

After creating the array of characters. You can simply assign the character by character to that pointer like shown below.

#include<stdio.h>
#include<string.h>
int main(int argc, char const *argv[])
{
	char str1[100]="Hello world";
	//create a pointer.
	char *str_ptr=str1;
	//lets print each character by using pointer
	printf("0=>%c\n1=>%c\n2=>%c\n",*(str_ptr),*(str_ptr+1),*(str_ptr+2));
	//you can also input a character using pointer using scanf 

	//scanf("%c",str_ptr);
	printf("Try doing something with pointers to string\n");

}
Strings and pointers in c

How to dynamically allocate a string in c?

Inorder to dynamically allocate a string we can nake use of malloc() function defined in stdlib header then assign the last index as '\0'

see the below example.

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(int argc, char const *argv[])
{
	char *str=(char*)malloc(sizeof(char)*11);
	str[10]='\0';
	//lets scan the string
	printf("\nEnter a string\n");
	scanf("%s",str);
	printf("You have entered:\n%s\n",str);
}
dynamically allocate a string in c

Program to check if a string contains a character in C

We can also pass string to the functions as a normal array. Or we can pass by the pointer.

Look at the example below:

#include<stdio.h>
#include<string.h>
void print_string( char str[] )
{
	printf("\nString passed to this function is:\n");
	printf("%s\n",str);
}
int main(int argc, char const *argv[])
{
	char string[100];
	printf("Enter a string to pass it to a function\n");
	scanf("%[^\n]*c",string);
	print_string( string );
	return 0;
}
Strings and functions in c

People also ask

What is string in C programming?

String is set of characters. In c programming language there is no datatype for string. But string is implemented as array of characters. That character array which forms a string is to have its last character as '\0'.

What is string and example?

String is a datatype in programming languages such as c c++ java python etc. It contains one or more characters.
Example: “Scholar soul”

Why strings are used in C?

In c programming language string is used for storing a word or text.

How are strings represented in C?

Strings in c are represented as character array with ending character as \0 .

How do you read a string?

We can read a string with help of scanf by specifying %s as format specifier. Or we can make use of other character reading functions.

How do you check if a string contains a character in C?

It is simple after performing input of a string you can go on comparing each index of the character array with key element. If that character is found you can stop. If you want the index of that character you can simply initialize a counter and increment it for each character comparisn. Look at the example program below, or else you can simply use the value of the index variable specified in for loop.
Check below program to check if a string contains a character in C

Is string a datatype?

String is considered as a datatype, but its implementation in different language is different. In c programming language it is array of characters terminated by a null character.

What is string syntax in c?

Basically as I explained above, in c programming syntax for string is char *string;
char string[];

I want to know length of string in C. How can I find it?

You can use built in strlen function to find length of a string in c programming. If you dont want to use built in string function then start counting from zeroth character until you find the null character.

How to print the middle letter of a string in C?

Simply find the length of the string print the character of middle element. Example program is shown below.

program to check if a string contains a character in C

#include<stdio.h>
#include<string.h>
int main()
{
 char str[10];
 //input string
 scanf("%s",str);
 //character to find in a string
 char find='c';
 for( int i=0 ; i<strlen(str) ; i++ ) 
 {
  if(str[i]==find)
  {
   printf("Character found");
   return 0;
  } 
 }
 printf("Character not found");
 return 0; 
}

Program print the middle letter of a string in C?

#include<stdio.h>
#include<string.h>
int main()
{
 char str[10];
 //input string
 scanf("%s",str);
 printf("Middle character is: %c",str[(strlen(str)/2)]);
 
}

People also read