Skip to content

Instantly share code, notes, and snippets.

@roxsula
Created January 23, 2019 15:31
Show Gist options
  • Save roxsula/327dcddce0f25e2da0bca5f3a2b94427 to your computer and use it in GitHub Desktop.
Save roxsula/327dcddce0f25e2da0bca5f3a2b94427 to your computer and use it in GitHub Desktop.
Program in C to compare strings. The strings will be equal if there are white spaces in the tail.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
int strcmpws(const char *s1, const char *s2)
{
char *r1 = strdup(s1);
char *r2 = strdup(s2);
int f = 0;
if (r1 != NULL)
{
char *fr1 = r1 + strlen(s1) - 1;
while( (isspace(*fr1) || !isprint(*fr1) || *fr1 == 0) && fr1 >= r1) --fr1;
*++fr1 = 0;
}
if (r2 != NULL)
{
char *fr2 = r2 + strlen(s2) - 1;
while( (isspace(*fr2) || !isprint(*fr2) || *fr2 == 0) && fr2 >= r2) --fr2;
*++fr2 = 0;
}
for(int i=0; r1[i]!='\0'; i++)
{
if(r1[i] == r2[i]) f = 0; else f = -1;
}
//if (strcmp(r1, r2) == 0) f = 0; else f = -1;
free(r1);
free(r2);
return f;
}
const char *a = " this is a string ";
const char *b = " this is a string";
int main()
{
printf("'%d'\n'", strcmpws(a,b));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment