×

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

File Handling in C

File Handling is used to open, read, write, search and close file.  C files I/O functions handles data on secondary storage device. File handling function: There are varioushandling functions in C. Function Operation fopen() It is...

4 minutes read.

Goto and Labels in C

Introduction Goto in C: The goto statement is known as the jump statement in C. The goto is used to transfer the control of a program to a predefined label. The goto...

3 minutes read.

Program to Find Mode of an Array in C

Mode is the highest occurring value or number in a given set of data elements. In a group of data values, a value that occurs most frequently is considered to...

3 minutes read.

Call Back Function in Embedded C

Callback function are one the most remarkable systems in C. A callback function is any code that is passed as a contention to another code, so this is the last...

3 minutes read.

Recursion in C

Recursion is a programming technique that allows the programmer to call the function within the same function. The function which calls the same function is known as recursive function but a...

1 minute read.

GCD program in C

C language : Dennis Ritchie developed the general-purpose computer language C at Bell Laboratories in 1972. Despite being an ancient language, it is extremely popular. It is among the most widely used...

4 minutes read.

Types of Array in C

What is an Array? One value can be stored in a variable at once. How many variables will you need if you have 100 values? Well, the answer isn't 100. It...

14 minutes read.

Header files in C

Header files in C In the C programming language, header files are present, which have an extension of ‘.h’, and it consists of macro definitions, declarations, and so on that are...

4 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.

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.

Limitations of Inline Function in C

The compiler is unable to conduct inlining in two instances. It just accepts the inline definitions and produces space for the function in the same way it does for a...

3 minutes read.

Cbrt() function in C

Introduction: The Cbrt is a function used in C programming language. The cbrt() function is a math function. Using the cbrt function, we can do the cube root of a function. This...

4 minutes read.

C printf and Scanf

Input-Output functions in C Programming In C Language, the printf() and scanf() are inbuilt library functions that used for input and output. It is defined in the header file“<stdio.h>”. printf() Function: In C...

2 minutes read.

Pure Virtual Function in C

Before knowing about the pure virtual function some of the important points about the virtual function in C++ are given below. In c++ the member function that is defined in the...

4 minutes read.

C Programming Tutorial

What is C Programming C is most popular and widely used computer programming language. It was developed by Dennis Ritchie in 1972 at the Bell Lab. It is used to develop system application software. There...

8 minutes read.

Multilevel Feedback Queue Scheduling (MLFQ) CPU Scheduling

In this article, you will learn how to initialise an array to 0 in C In C, an array is declared as: char ZEROARRAY[2022]; The global scope changes at runtime to all zeros....

3 minutes read.

clrscr in C

clrscr function in C clrscr stands for: clr = clear scr= screen When the clrscr() function is called in a program everything currently displayed in the console(output of previous programs, output of current program,...

3 minutes read.

C pre-processor

The C processor is a macro processor which is used to compile the source code of the program (step by step) . It is not a part of the compiler....

4 minutes read.

Errors in C

Errors in C Errors are nothing but problems or faults that pretty much occur in all the programming languages. Errors make the behavior of the program seem abnormal, and even the...

4 minutes read.

Fseek Function in C

The fseek function is a function in the C standard library that changes the position of the file pointer in a stream-oriented file. It is typically used to move the...

3 minutes read.