C Tutorial

C Tutorial C Language Environment Setup Execution flow of C program C printf and Scanf C Data type C Token Variable in C Operators in C Comments in C Escape Sequence in C C – Storage Classes C Decision control statement Loop Statement in C Break, continue and goto statement in C Type Casting in C Function in C Recursion in C String in C C Array Pointer in C Dynamic memory allocation C –Structure Nested Structure in C Union in C File Handling in C C pre-processor Static Function In C Sizeof In C Selection Sort In C Scope Of Variables In C Runtime Vs Compile Time In C Random Access Lseek In C Queue Implementation In C Pseudo Code In C Prototype In C Pointer To Pointer In C Pointer Arithmetic In C Passing Array To Function In C Null Character In C Merge Sort In C Macros In C Library Functions In C Memory Leak In C Int In C Goto And Labels In C Fibonacci Series In C Fflush In C Derived Data Types In C Data Types In C Const Vs Volatile In C Character Set In C Character Class Tests In C Calloc In C C Pointers Arrays In C Include In C Clrscr In C C Vs Java String Literals In C Types Of Pointers In C Variables In C Volatile In C Why C Is A Middle Level Language Infix To Postfix Program In C Ceil function in C LCM of two numbers in C Quick sort in C Static in C function pointer as argument in C Top Array Keywords in C Add two numbers using the function in C Armstrong program in C using function Array, Declaring Arrays and Array Initialization Limitations of Inline Function in C Merge and Merge sort with example in C Do-While Loop in C For Loop in C While-Loop in C Difference between while and do-while loop in C Array Of Structures in C Data Structures And Algorithms in C Types Of Structures In C How to Avoid Structure Padding in C Use of Structure in C Do WHILE LOOP in C Programming Examples For Loop in C Programming Examples Entry Control Loop in C Exit control loop in C Infinite loop in C Nested loop in C pow() function in C String Handling functions in C Prime Number code in C Factorial Program in C using For Loop Factorial Program in C Using While Loop Fibonacci Series in C Using For Loop Fibonacci series in C using while loop Prime Number Program in C using for Loop While Loop in C programming examples Built-in functions in C Assert() Function C vs Java Strings Call Back Function in Embedded C Else If Ladder fgets() function Ftell() Function getc() function getch() function gets() function Heap Sort Nested if-else statement Pi() Function Positioning of file Write() function abs() function in C Attributes in C C program to find factorial of a number using Recursion Ferror() in c fopen() function in C Fibonacci series program in C using Recursion Formatted Input and output function in C Snake Game in C User Defined Functions in C Beep() function in C Cbrt() function in C Hook() function in C Isalnum() function in C C Program to find the Roots of a Quadratic Equation C Switch Statements Difference between rand() and srand() function in C Difference between while and for loop in C Doubly Linked list in C Example of Iteration in C How to use atoi() function in C How to use floor() function in C How to use sine() function in C How to use Typedef Struct in C Integer Promotions in C C Program to Find Largest Number Using Dynamic Memory Allocation C Program to Find the Largest Number using Ternary Operator C/C++ Program to Find the Size of int, float, double and char Find the Largest Three Distinct Elements in an Array using C/C++ Loop Questions in C Modulus on Negative Numbers in C Multiplication table program in C using For loop Nested Loops in C Programming Examples C Program for Mean and Median of an Unsorted Array Results of Comparison Operations in C and C++ Reverse a Stack using Recursion in C Simple hash() function in C strcat() Function in C Sum of N numbers in C using For loop Use of free() function in C Write a program that produces different results in C and C++

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.



ADVERTISEMENT
ADVERTISEMENT