×

String in C

String is a collection of character or group of characters. In array, string of character is terminated by a null value “\0” and enclose between double quote. We can declare string in two ways:
  1. By char array.
  2. By string literal
Let us consider how to declare of string by char array.
char ch[5]={‘a’, ‘e’,’i’.’o’,’u’,’\0’};  // By char array
char nm[5]=”C -Tutorial” ;    // By  string literals
There are various types of string functions supported by C language.
String Function Description
strcpy(a1, a2); It is used to copy the string a2 into string a1.
strcat(a1, a2); It is used to concatenate the string a2 onto the end of string a1.
strlen(a1); It is used to find the length of string..
strcmp(a1, a2); It is used to compare twostring and returns 0 if both strings are equal.
strrev() It is used to reverse the sting .
strstr(a1, a2); It is used to return a pointer to the a2 in string a1.

Let us consider an example of string functions.  Example 1:  strcpy()
#include 
#include 
int main () {
   char a1[10] = "Hello";
   char a2[10];
   strcpy(a2, a1);
   printf(" Value of Second sting is a2:  %s\n", a2);
   return 0;
}
Example 2:  strcpy()
#include 
#include 
int main () {
   char a1[10] = "Hello";
   char a2[10]="world";
   strcat(a2, a1);
   printf(" Concatenates a1 and a2:  %s\n", a2);
   return 0;
}

Example 3:  strlen()
#include 
#include 
int main () {
   char a1[10] = "Hello";
   int len=strlen(a1);
   printf("Find the length of a1:  %d\n", len);
   return 0;
}
Example 4:  strcmp()
#include 
#include 
int main () {
   char a1[10] = "Hello";
   char a2[10] = "Hello";
   if(strcmp(a1,a2)==0){
   printf("String are equal");
   }else{
   printf("String are not equal");
   }
   return 0;
}
Example 5:  strrev()
#include 
#include 
int main () {
char a1[10] = "DOG";
printf("\n Reverse String is:%s",strrev(a1));
return 0;
}
Example 6:  strstr()
#include 
#include 
int main () {
   char a1[50] = "This is c tutorial form tutorial and example" ;
   char *ptr;
  ptr =strstr(a1,"tutorial");
   printf("substring is : %s", ptr);
   return 0;
}

Related Topics

Pseudo Code in C

Pseudo code in C can be referred to as a simple way or method to write programming code or program algorithm in English. A pseudocode is an informal representation of...

4 minutes read.

C Data type

Data Types in C with Examples Data types are used to define the type of data that a variable can store to perform a specific operation.ANSI C provides three types of...

2 minutes read.

Difference between C and Java

C programming and Java programming are two of the earliest programming languages. C programming follows a procedural approach whereas Java programming follows an object-oriented approach. Java programming is a part...

3 minutes read.

For Loop in C

The Syntax of For Loop for (initialization statement; test expression; update statement) {     /* main body of the FOR loop */ } How does For loop work? In for loop, the initialization command is...

3 minutes read.

Garbage in C

Garbage in C The C programming language is a perfect fit for embedded systems as it provides low level control, structured programming, and portability. On the contrary, it does not provide...

3 minutes read.

Bit Stuffing Program in C

What is a Bit Stuffing? The process of inserting one or more extra bits (0) into data is known as bit stuffing. To provide signalling information to a recipient, these are...

3 minutes read.

Tower of Hanoi in C

What is Tower of Hanoi? The Tower of Hanoi is a gaming problem that was created in 1883 by a French mathematician named Édouard Lucas. The Tower of Hanoi temple in...

4 minutes read.

C Token

C Token and Keyword The C tokens are the basic buildings blocks in C language which are constructed together to write a C program. Each and every smallest individual unit in the C program is...

2 minutes read.

Binary Search in C with Best and Worst Time Complexity

What is Binary Search? Binary search is an algorithm that is used to find an element in a sorted array efficiently. It has a time complexity of O(log n). It means...

3 minutes read.

Data Structures And Algorithms in C

C language is one of the most flexible and simple languages. So, it is always better to learn any concept in C language compared to other languages. Let us look...

13 minutes read.

Remove an element from an array in C

A collection of objects or pieces of the same data type stored in a single memory block is known as an array. A data structure called an array is used...

3 minutes read.

Static in C

Static is a keyword that is used in the C programming language. It can be used both as variables and as functions. In other words, it can be declared both...

3 minutes read.

Find the Largest Three Distinct Elements in an Array using C/C++

In this tutorial, we will demonstrate how to use a C/C++ programme to locate the highest three different elements in an array. C/C++ Program to Find the Largest Three Different Elements...

3 minutes read.

Difference between Pre-increment and Post-increment in C

Increment operators are the kind of operators that are used to increment the cost of the operand by way of 1. In different words, the increment operator is an operator...

4 minutes read.

C Language Environment Setup

To compile C program, we must have GCC compiler installed on our machine. In this C tutorial, all the examples are compiled and tested using GCC compiler. Although we can...

3 minutes read.

Evaluation of Arithmetic Expression in C

We can use the "eval" function in C to evaluate an arithmetic expression stored as a string. However, using "eval" is generally considered bad practice and can lead to security...

4 minutes read.

C Math Library

C Math Library The <math.h> header defines various mathematical functions and one macro. Many functions are available in this library to take double as an argument and then return double as...

4 minutes read.

Hexadecimal to Binary in C

What is hexadecimal? Hexadecimal defines a sequence of numbers centered on-16, i.e., before assigning the next number to a new location, it describes a numbering scheme that includes 16 sequential numbers as basic...

2 minutes read.

Inline Function in C

Inline Function in C A normal function will become an inline function when the function prototype of that function is prepended with the keyword "inline". Inline functions are the function where...

4 minutes read.

Snake Game in C

Snake game in C is basic desktop console program which was created in 1970’s. It is an old classic game which was played by every child across the world. This...

4 minutes read.