×

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

Banker’s Algorithm in C

Introduction Before doing a "s-state" check to look for potential activities and deciding if allocation should be allowed to continue, the banker's algorithm, a resource allocation and deadlock avoidance algorithm, tests...

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

Binomial Coefficient Program in C

What is Binomial coefficient? In the given set of n possibilities, the binomial coefficient(n,k) indicates the order of choosing 'K' results from those possibilities. Binomial coeeficient of posistive n and k...

3 minutes read.

Pointer to pointer in C

A pointer to another pointer is another type of multiple indirections and a chain of many pointers. Generally, a pointer consists of the address of the variable. Once a pointer to...

4 minutes read.

C Program to find the Roots of a Quadratic Equation

What is Quadratic Equation: Equations of degree 2 in polynomial form are called quadratic equations. A, B, and C are the coefficient variables in the equation, which is written as ax2...

3 minutes read.

Selection sort in C

In the C standard, the selection sorting technique exists where the smallest among the unsorted elements of the array is selected at each time and is then inserted into its...

3 minutes read.

How to measure time taken by a function in C?

Measuring the time taken by a function is quite a complex task, because numerous methods are frequently not transferable to other platforms, measuring the execution time of a C programs...

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

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.

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.

What are linker and loader in C

Linker and loader are utility programs that have a significant role in executing a program. Linker: A linker is a program that joins the object files produced by the assembler/ compiler...

3 minutes read.

C Program of Fencing the Ground

The college's ground is rectangular. Fencing the ground the management makes the decision to construct a fence around the ground. They planned to wrap a thick rope around the ground...

1 minute 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.

abs() function in C

abs() function is a built-in function present in the stdlib.h header file. It returns an integer value. abs() function converts a negative value into a positive value. For example, if...

4 minutes read.

Pi() Function in C

Pi: Mathematic constants are not defined in the c or c++ programming languages. To use the Mathematical constants firstly, we have to define the _USE_MATH_DEFINES and then declare the cmath and...

3 minutes read.

Malloc function in C

When the user or programmer does not know how much memory space is needed in the application, it a needs dynamic memory allocation during runtime. Dynamic memory allocation helps us to...

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.

Fahrenheit to Celsius in C

Before going into conversion, we have to know what Fahrenheit and Celsius mean, these are both units to measure the temperature.In our daily life, we use both Fahrenheit and Celsius,...

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.