×

strcat() Function in C

Strings in c:

A string is defined as the set of characters that are enclosed within the double quotations (" "). The String always ends with the null character ("\0"). The null character ("\0") indicates that it is the end of the string. It can also be enclosed with single quotes in c programming.

Syntax:

char variable [size] = "String data";

Example:

We can declare the string in c programming as two types

  • String [5] = {"s", "a", "I", "r", "\0"};
  • String [3] = "Sai";

It will be stored as

SAi\0

String Declaration:

S[0]S[1]S[2]S[3]S[4]S[5]

Above is a string of length 6. So we can store 5 characters and one null character at the last position by default.

String Initialisation:

We can declare or initialise a string in the below ways

Char t[] = “Vijay”;

Char t[] = {‘a’, ’e’, ’I’, ‘o’, ‘u’, ‘\0’};

Char t[5] = {‘a’, ’e’, ’I’, ‘o’, ‘\0’};

Char t [6] = “Om Sai”;

OmSai\0

How to Read a String:

We can read a string using the scanf () function. We will use “%s” for string datatype to read or print the string values.

Example:

//simple program to scan and print string
#include < stdio.h >
#include < conio.h >
int main()
{
char a[10];   //string declaration
clrscr();
printf(“enter the string value: “);
scanf(“%s”, a);       //here we take the string data from the user
printf(“the name is %s.”, a);       //we print the string data
return 0;
}

Output:

Enter the string value: Vijay
The name is Vijay.

String Handling Functions:

We can work more effectively with strings using pre-defined functions known as String handling functions. If we want to use string handling functions in our program, then we must include <string.h> pre-processer. Some of them are

  • Strlen () Function:

The string length function will print or compute the length of the given string.

Syntax: 

strlen(a);

Example:

//program for string length function
#include < stdio.h >
#include < string.h >
#include < conio.h >
int main()
{
int len;   //integer variable declaration
char a[] = {‘v’, ‘I’, ‘j’, ‘a’, ‘y’, ‘\0’};   //string declaration with data
clrscr();
len = strlrn(a);    //finding the length of string a and storing in len varaible
printf(“The size of the given string is %d”, len);  //printing the len of string
return 0;
}

Output:

The size of the given string is 6.
  • strcpy () Function:

The string copy function is used to copy the whole string or the content of one string to another.

Syntax: 

strcpy(a, b);

Example:

//program for string copy function
#include < stdio.h >
#include < string.h >
#include < conio.h >
int main()
{
char c[];     //string declaration
char a[] = “hi”;   //string declaration with data
char b[] = “all”;  //string declaration with data
clrscr();
strcpy (a, c);     //coping one string to another variable
printf(“The copied string is %s”, c);   //printing the copied string
return 0;
}

Output:

The copied string is hi.
  • Strcmp () Function:

The string compare function is used to compare two strings.

Syntax: 

strcmp(a, b);

Example:

//program for string concatenate function
#include < stdio.h >
#include < string.h >
#include < conio.h >
int main()
{
char c[];      //string declaration
char a[] = “Hi ”;   //string declaration with data
char b[] = “all”;   //string declaration with data
clrscr();
c = strcmp(a, b);   //comparing two strings
if(c == 0)
{
printf(“both strings are same”);  //prints if the given condition is true
}
else
{
printf(“both strings are not the same”);  //prints if the given condition is false
return 0;
}

Output:

Both strings are not the same.
  • Strcat () Function:

The string concatenate function is used to join or add or concatenate two strings.

Syntax: 

strcat(a, b);

Example:

//program for string concatenate function
#include < stdio.h >
#include < string.h >
#include < conio.h >
int main()
{
char c[];        //string declaration
char a[] = “Hi ”;   //string declaration with data
char b[] = “all”;    //string declaration with data
clrscr();
c = strcat (a, b);       //concatenating two strings and storing it in c variable
printf(“The concatenated string is %s”, c);  //printing the concatenated string
return 0;
}

Output:

The concatenated string is Hi all

Related Topics

Comma Operator in C

What is a comma operator? The comma operator in the C programming language has the least priority. The comma operator is essentially a binary operator that operates on the first operand...

4 minutes read.

tolower() Function in C

The tolower() capability is characterized in the ctype.h header document. In the event that the person passed is a capitalized letter set, the tolower() capability switches capitalized letters in order...

3 minutes read.

getc() function in C

Getc is one of the file handling technique in C. The Getc() is a C library function gets the next character or new characters  from the specific stream and supports...

4 minutes read.

Type Conversion in the C

The process of changing one data type to another in the C programming language is known as "type conversion." The type conversion method is only executed on data types that...

4 minutes read.

Use of free() function in C

Introduction The free() function uses in C programming language. The free() function in the C programming language uses to release or deallocate the memory blocks; these blocks are previously allocated by calloc(), malloc() or realloc()...

3 minutes read.

Projects on C language in 2024

Introduction Most aspiring programmers learn the C language as their first high-level programming language. The most flexible language utilized in every industry is, without a doubt, C. Even after 50 years...

9 minutes read.

How to Find Square Free Numbers in C?

Introduction Square-free number program is a typical interview and academic question in C Programming. In this section, we will define square-free integers, construct algorithms and C program to print the nth...

11 minutes read.

What is a buffer in C?

A buffer is an area of memory set aside for the temporary storage of data. A data buffer (or just buffer) is a region of a physical memory storage used to...

5 minutes read.

What is required in each C Program?

Each C program must require one function, i.e., main() function. It is because when we execute the C program, C compiler looks for the main() function, and from here only...

5 minutes read.

Flow Chart of For loop in C

This is a flowchart that represents the process of executing the for loop in the C programming language. Generally, as we know there are three main components of for loop:1. The...

3 minutes read.

Why does sizeof(x++) not Increment x in C

Sizeof() is a much-involved operator in C or C++. It is an incorporated time unary administrator which can be utilized to figure the size of its operand. The consequence of...

5 minutes read.

How to return a Pointer from a Function in C?

In the C programming language, pointers are variables that hold the address memory location of another variable. We could also input pointers to a function and return pointers from a...

3 minutes read.

Bit Fields in C

The size of a structure in C is specified in bits. The primary goal of it is to use memory efficiently, once we understand that a bit's value must fall...

3 minutes read.

Write a program that produces different results in C and C++

In this tutorial, we'll explore several programs that, depending on whether they are developed using C or C++ compilers, will produce varying outputs. There are numerous similar programs, but we will just...

2 minutes read.

memcmp() in C

Introduction: In this article we are discuss about memcmp() function in C. This function permits the person to evaluate the bytes of the two characters, as mentioned above. Depending on...

4 minutes read.

Sum of digits in C++

Sum of digits in C++ There are various ways to find the sum of the digits of a number in C++. We can use containers like arrays or other simple cases...

4 minutes read.

How to get ASCII value in C

For text data on computers and the internet, ASCII (American Standard Code for Information Interchange). It is the most widely used character encoding standard. One hundred twenty-eight alphabetic, numeric, special...

3 minutes read.

Type qualifiers in C

Type qualifiers in C: In the C programming language, type qualifiers are the keywords that prepend to the variables to change their accessibility, i.e., we can tell that the type...

4 minutes read.

Range of int in C

In this session we know about the rage of the int in C language with the suitable examples and the programs. Firstly, we can learn the range of int: The int is...

3 minutes read.

Self-referential structure in C

Self-referential structure in C A self-referential structure is a structure that can have members which point to a structure variable of the same type. They can have one or more pointers...

3 minutes read.