strcspn c code example

Example 1: strcasecmp c

Upon completion, strcasecmp() shall return an integer greater than, equal to, or less than 0, if the string pointed to by s1 is, ignoring case, greater than, equal to, or less than the string pointed to by s2, respectively.

Example 2: strcmp c

// use: strcmp(string1, string2);

string a = "words";
string b = "words";

if (strcmp(a, b) == 0)
{
	printf("a and b match");
  	// strcmp returns 0 if both strings match
}

else
{
	printf("a and b don't match");
  	// strcmp returns anything else if the strings dont match
}

Example 3: strcpy en c

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
int main()
{
    char Temp[255];
    strcpy(&Temp,"Gladir");
    strcpy(&Temp,"ABC");
    strcpy(&Temp,"Gladir.com");
    puts(&Temp);
    return 0;
}

Example 4: strncpy c

char *strncpy(char *dest, const char *src, size_t n)

Example 5: c strcmp

// strCmp implementation
// string1 < string2 => return a negative integer
// string1 > string2 => return a positive integer
// string1 = string2 => return 0
int strCmp(const char* s1, const char* s2) {
    while(*s1 && (*s1 == *s2)) {
        s1++;
        s2++;
    }
    return *s1 - *s2;
}

Tags:

Cpp Example