×

String Handling functions in C

String :-

The String is the collection of characters. Every String ends with the null character, and the String is enclosed in the double quotations .ie, "javaTpoint". If we see any character in the double quotation, we can say it is a string. To declare a string, we use the char data type.

Ex :-

#include <stdio.h>
int main()
{
    char s[50]="JavaTpoint"; // string declaration 
    printf("The given string is:");
    printf("%s",s);// printing the string
    return 0;
}

Output:-

The given string is: JavaTpoint

String handling functions in c:-

It is also known as string manipulation. To reduce the programmer's time and to make the code more effective in c, we have some in-build functions in that we have string handling functions.

The String handling function is used to perform some operations on strings. All the string handling functions are embedded in string.h library. we can perform String handling functions, By including string.h header file

S.noString functionSyntaxDescription
01 .strlen()str( String_name)It returns the length of the given string
02 .strrev()strrev(String_name)It reverse the given string and returns to the main function.
03 .strcpy()strcpy (string1, string2)With this function the character in the string2 are copied in the string1
04 .strncpy()strcpy(string1, string2, n)With this the first n characters of string2 are copied in the string1
05 .strcat()strcat (string1, string2)It is used to append the characters of string2 into string1
06 .strlwr()strlwr (string1)All the characters in the string1 are converted into lower case letters
07 .strupr()strupr (string1)All the characters in the string1 are converted into upper case letters
08 .strcmp()strcmp (string1, string2)It compares string1 and string2 if both the strings are same then it returns 0, if string1>string2, it returns ASCII value greater than 0,if string1<string2, it returns ASCII value less than 0.
09 .strncmp()strncmp(string1, string2, n)It is used to compare the first n characters of string1 and string2 .
10 .strcmpi()strcmp(string1, string2)It is same as the strcmp() function but the difference is it is not case sensitive .ie,”A” and “a” are treated as same.
11 .strncat()strncat(string1, string2,n)It is used to append the first n characters of string2 in string1.
12 .strchr()strchr(string1, ’a’)The function of this function is to return the pointer to the first occurrence of the character ‘A’ in the string1 .
13 .strrchr()strchrr(string1 , ’b’)The function of this function is to return the pointer to the last occurrence of the character ‘A’ in the string1 .
14 .strstr()strstr(string1, string2)The function of this function is to return the pointer to the last occurrence of the string2 in the string1 .
15 .strset()Strset( string , ’A’)By this functions all the characters in the string1 is converted to ‘A’ .
16 .strnset()strnset(string1 ,’B’ ,n)This function sets the first n characters as ‘B’ .
17 .strdup()string2=strdup(string1)The duplicate characters of string1 are stored in string2 .

Examples:-

//program for strlen()  

#include<stdio.h>
#include<string.h>
int main()
{
	char s[100] = “JavaTpoint”;
	// printing the length of the string
	printf(“The length of the given string is :%d”,strlen(s));
	return 0;
}

Output :

The length of the given string is :10

//program for strrev()

#include <stdio.h>
#include <string.h>
int main()
{
    char s[100] = "JavaTpoint";
	// printing the length of the string
	printf("The reverse of the given string is: is %s",strrev(s));
	return 0;


}

Output :

the reverse of the given string is :tniopTavaJ

//Program for strcmp(),strncmp()

#include<stdio.h>
#include<string.h>
int main()
{
    char s[50],a[50];
    printf("\nEnter the first string:");
    scanf("%s",s);
    printf("\nEnter the second string:");
    scanf("%s",a);
    //comparing both the strings
    printf("%d %d",strcmp(s,a),strncmp(s,a,5));
    
}

Output 1 :

Enter the first string: sophie
Enter the second string: graner
0 0

// program for strcpy() strncpy()

#include<stdio.h>
#include<string.h>
int main()
{
    char s[50]=" ",a[50];
    printf ("Enter the second string:\n");
    scanf("%s" ,a);
    //comparing both the strings
    printf ("The first string is :%s\n",strncpy(s,a,3));
    printf ("The first string is:%s",strcpy(s,a));
    return 0 ;
}

Output :

Enter the second string: JavaTpoint
The first string is : Jav
The first string is : JavaTpoint

// program for strset() strnset(

#include <stdio.h>
#include<string.h>
int main() {
    char s[100]="JavaTpoint";
    printf ("Before strset() function: %s",s);
   // setting all the characters as A
    printf("\nstring after strset() function:%s",strset(s,A'));
   // setting first five charecters as B
    printf("\n string after strnset() function %s",strset(s,5,'B'));
    return 0 ;
}

Output :-

Before strset() function: JavaTpoint
After strset() function: AAAAAAAAAA
Atrer strnset() function: AAAAApoint

//program for strcat() and strncat()

include <stdio.h>
#include<string.h>
int main() {
    char s1[100]=" ";
    char s2[100]="JavaTpoint";
    printf ("Before strcat() function:\n s1=%s\n s2=%s",s1,s2);
    // Appending first 5 characters of s2 into s1  
    strset(s1,s2,5);
    printf (" After strncat() function:\n s1=%s\n s2=%s",s1,s2);
    // Appending all characters of s2 into s1
    strcat(s1,s2);
    printf (" After strcat() function:\n s1=%s\n s2=%s",s1,s2);
    return 0 ;
}

Output :-

Before strcat() function:
S1= 
S2 =JavaTpoint
After strncat() function :
S1=JavaT
S2=JavaTpoint
After strcat() function :
S1=JavaTpoint
S2=JavaTpoint

// program for strupr() ans strler()

#include<stdio.h>
#include<string.h>
Int main()
{
char s[100] = ”Javatpoint”;
printf(“string before strupr() function :\ns=%s”, s);
// printing upper case string
printf(“string After strupr() function :\ns=%s”, s);
// printing lower case string
printf(“string After strlwr() function :\ns=%s”, s);
return 0;
}

Output :-

String before strupr :
s= JavaTpoint
string After strupr() function :
s= JAVATPOINT
string After strlwr() function :
s= javatpoint

Applications  of String :-

1 .Information Retrieval :-

String handling functions can be used to retrieve data from an unknown source.

2 .plagiarism detector :-

By using strings, the computer can identify the percentage of the content, code matching with the content That already exists in the database or websites.

3. Cipher Text Generator:-

The transformation of data from one source to another source without any interruption and any third-party attack or loss of data can be done by the strings.

4 .Spam detection :-

Currently, if we open Gmail, we have a separate section called spam. In that, we have unimportant mails, advertisement mails  ,spam mails this mails are identified by the help of strings.

5. Search Engines:-

If we search anything in the search engine, it recommends the data and websites that are close to the content which we have searched. The search algorithm compares the given String to the strings which are present in the database and displays the result.

6. Bioinformatics :-

In the field of bioinformatics, to match the DNA sequences, string matching modules .it are also used to find the DNA patterns and genetic sequences.

Disadvantages of strings:-

1. In c, the strings are static. The size of the strings is defined at the beginning of the program. So we can't perform strings dynamically in c.
2 . The string performance is slow while performing operations like input and output.
3 . The String is immutable in java. We can't change the data which is present in the String.


Related Topics

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.

Pointers in C

Pointers in C: In the C programming language, a pointer is a pointer variable that points to the address of the other variable. It also stores the address of the...

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

C Program to find the size of a File

Before knowing about the Program, you need to understand what the size of a file is. After that, you should know how it is going to work with the given...

4 minutes read.

How to convert a number to words in C

There are many ways available for converting an integer or a number to its word form. For example, consider a number as 12345. This number can be represented in two...

3 minutes read.

Addition of Matrix in C

The mathematical operation of includingor greater matrices is called the addition of matrices. A matrix is a square series of numbers, symbols, words, letters, and different things. In this article,...

3 minutes read.

How to create a binary file in C?

Creating a binary file in C language is very simple, requiring only some specific functions to be implemented. There are also other binary modes: read, and write, which will be...

7 minutes read.

Use of fflush(stdin) in C

Use of fflush(stdin) in C Usually, fflush() is only used for the output stream. The purpose is to clean (or flush) the output buffer and transfer the buffered data into...

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

Advantages of Dynamic Memory Allocation in C

Dynamic Memory Allocation Dynamic memory allocation is a process of assigning or allocating memory to a variable during the execution of a program. Dynamic memory allocation provides storage according to the...

3 minutes read.

Macros in C

In the C programming language, the macro is defined as a segment of the code replaced by the macro defined value at the beginning. ‘#define’ is the directive that defines...

3 minutes 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.

Dynamic memory allocation

Dynamic memory allocation is used to allocate the memory at runtime. It has following function within <stdlib.h> header file. Function Syntax malloc() malloc (number *sizeof(int)); calloc() calloc (number, sizeof(int)); realloc() realloc (pointer_name, number * sizeof(int)); free() free (pointer_name); malloc() function The process...

2 minutes read.

Escape Sequence in C

An escape sequence is a sequence of character that used inside the string literal or character.  All the escape sequence is used with the backslash (\) symbol. The list of an...

1 minute read.

How to include graphics.h in C?

How to include graphics.h in code blocks? Graphics .h is a header that allows drawing lines, rectangles, ovals, arcs, polygons, pictures, and strings on a graphical window by giving access to...

6 minutes read.

Functions in C

What is function? Functions are defined as the set of announcements which take inputs, perform operations and provide results. The operation of a function only performs when the function gets called....

4 minutes read.

How to find square root in C Language

Before understanding the square root program in C language, one must know about the square root of a number. A square root is a mathematical term. The square root of a...

5 minutes read.

LCM of two numbers in C

LCM is a mathematical term which stands for Least Common Multiple. LCM of any two numbers is the smallest positive value(number) which is evenly divisible by the two given numbers. Consider...

4 minutes read.

GCD program of two numbers in C

GCD stands for Greatest Common Divisor, which is also known as Highest Common Factor, which can be abbreviated as HCF. Mathematically it can be defined as any two positive integers...

3 minutes read.

Example of Iteration in C

The iterations in the C language are the statements which are executed number of times until a certain condition is reached. These iterations are regarded as “Loops” in C language....

3 minutes read.