C Programming String

C Programming String

C Programming String
In this chapter you will learn about C programming string(string) and its various operations. You will also learn about declaring strings, initializing them and using them in various input/output operations.

Table of Contents

C programming strings

In C programming, an array of characters is called a string. /0The string is terminated by the null( ) character. For example:

				
					"c string tutorial"

				
			

Here, “c string tutorial” is string. When the compiler compiles the string, it /0appends a null( ) character to the end of the string.

C string declaration

Before working with strings, they must first be declared.

Strings are declared in the same way that arrays are declared. The only difference is the string char type.

Using array:

				
					char s[5];

				
			

Using pointers:

				
					char *p;

				
			

C string initialization

Strings can be initialized in different ways in C programming.

Both initialization and declaration are done in the same step for ease and convenience.

Using array:

				
					char c[] = "abcd";

or,
char c[5] = "abcd";

or,
char c[] = {'a', 'b', 'c', 'd', '\0'};

or,
char c[5] = {'a', 'b', 'c', 'd', '\0'};
				
			

Using pointers:

Strings can also be initialized using pointers as follows:

				
					char *c = "abcd";

				
			

Reading string from user

scanf()You can read strings using functions like any other data type . 

However scanf()the function accepts only the first word of the input words. This function terminates when white space or only space is encountered.

Reading/receiving words from the user

				
					char c[20];
scanf("%s", c);
				
			

Example: Reading string using scanf() function

				
					//C program to better understand how to read/receive strings from terminal
#include <stdio.h>
int main()
{
 char name[20];
 printf("Enter your name: ");
 scanf("%s", name);
 printf("Your name is %s.", name);
 return 0;
}
				
			

Output

				
					Enter your name: Azizur Rahman
Your name is Azizur.
				
			

Rahman is ignored in the above program because scanf()the function accepts only one word preceding the space.

Reading a row of text

Each character is read one by one to read/receive an entire line of text.

Example: Reading a string of text using the getchar() function

				
					// C program to better understand how to read one character at a time from the terminal
#include <stdio.h>
int main()
{
 char name[30], ch;
 int i = 0;
 printf("Enter name: ");
 while(ch != '\n') //Ends when user clicks enter button
 {
 ch = getchar();
 name[i] = ch;
 i++;
 }
 name[i] = '\0'; // Add the null character at the end
 printf("Name: %s", name);
 return 0;
}
				
			

getchar()Using the function ch in the above program takes one character from the user each time.

This process continues until the user clicks the enter button. Finally a null character is appended to the end to make it a string.

This is an annoying method for receiving strings.

Using library functions

A simpler approach to receiving and displaying strings in C programming is to use the predefined library functions gets()and respectively.puts

Example: Reading a line of text using standard library functions

				
					//C program to read a single line of text using gets() and puts()
#include <stdio.h>
int main()
{
 char name[30];
 printf("Enter name: ");
 get(name); //Function to read string from user
 printf("Name: ");
 puts(name); //String display function
 return 0;
}
				
			

Both the above programs give the same output as below:

Output

				
					Enter name: Azizur Rahman
Name: Azizur Rahman
				
			

Passing strings through functions

charStrings are similar to arrays in C programming . So they can be passed through functions in the same way as arrays.

Visit the section on passing arrays through functions to learn more .

				
					#include <stdio.h>
void displayString(char str[]);

int main()
{
 char str[50];
 printf("Enter string: ");
 get(str);
 displayString(str); // String s is passed through the function.
 return 0;
}
void displayString(char str[]){
 printf("String Output: ");
 puts(str);
}
				
			

In the above program, the string is spassed from main()the function to the user-defined function . displayString()Function declaration str[]is formal argument (formal argument).

C string handling functions

C programming has many types of string operations that you can perform manually. For example, determining the length of a string, copying a string, concatenation of two strings, etc.

<string.h>But many such library functions are already defined under the header file to make it easier for programmers .

Visit C programming string manipulation chapter to learn more .

Leave a Comment

Your email address will not be published. Required fields are marked *