Tech Study

C String Functions

C String Functions perform certain operations, It provides many useful string functions which can come into action.

The <string.h> header file have to be included in your program to use them:

#include <string.h>

Here we will talk about those functions one by one:-

C String Functions – strlen

Syntax:

size_t rishStr(const char* ristr)

Here size_t is representing unsigned short

the length of the string is returned where  end character (terminating char ‘\0’) is not included.

C String Functions – strnlen 

Syntax: 

size_t rishSte(const char *ristr, size_t mxmmlen)

size_t represents unsigned short where It basically returns length of the string in the where it is less than the specified value for mxmmlen (maximum length) otherwise mxmmlen value will be returned by it.

C String Functions – strcmp

int strcmp(const char *ristr1, const char *ristr2)

Here two strings are compared and an integer value is returned. If the strings come out to be same (equal) then 0 would be returned by this function or else it a negative or positive value based on the comparison might be returned.

If ristr1 < ristr2 OR ristr1 comes out to be a substring of ristr2 then it would result in a negative value. If ristr1 > ristr2 then positive value would be returned by it.

If ristr1 == ristr2 then we will be getting  0(zero) when we use this function for comparing strings.

C String Functions – strncmp

int strncmp(const char *ristr1, const char *ristr2, size_t n)

size_t is for unassigned short

Both strings are compared till n characters or in other words it is going to compare first n characters of both the strings.

C String Functions – strcat

char *strcat(char *ristr1, char *ristr2)

Two strings are concatenated and then returned.

C String functions – strncat

char *strncat(char *ristr1, char *ristr2, int n)

n characters of ristr2 are concatenated to string ristr1. At the end of the concatenated string ,we will find that, a terminator char (‘\0’) will always be appended.

C String functions – strcpy

char *strcpy( char *ristr1, char *ristr2)

The string ristr2 is copied into string ristr1, the end character (terminator char ‘\0’) is included.

C String functions – strncpy

char *strncpy( char *ristr1, char *ristr2, size_t n)

size_t is representing unassigned short and n is a number here.

Case1: If length of ristr2 > n then first n characters of ristr2 is copied by it into ristr1.

Case2: If length of ristr2 < n then all the characters of ristr2 is copied by it into ristr1 and several terminator chars(‘\0’) are appended to accumulate the length of ristr1 to make it n.

C String functions – strchr

char *strchr(char *rishstr, int ch)

String rishstr is searched for character ch 

C String functions – Strrchr

char *strrchr(char *rishstr, int ch)

The similarity to the function strchr can be seen here, the only difference marked here is that it searches the string in reverse order .The extra r in strrchr is for reverse.

C String functions – strstr

char *strstr(char *ristr, char *ri_srch_term)

Similarity to strchr can be seen here as well, except that it searches for string ri_srch_term instead of a single character.

Java Final keyword

Introduction : java final keyword The final keyword present in Java programming language is generally used for restricting the user. …

Read more

C++ Memory Management: new and delete

C++ Memory Management We know that arrays store contiguous and the same type of memory blocks, so memory is allocated …

Read more