Code: Select all
#include<stdio.h>
#include<string.h>
#include<ctype.h>
int main(void)
{
int stringCompare(char*, char*);
int prefixCompare(char*, char*);
int suffixCompare(char*, char*);
char* commonChars(char*, char*);
char string1[20], string2[20];
int condition;
/* Get strings from user */
printf("Please enter two strings seperated by a space\n\tStrings: ");
scanf("%s", string1);
getchar();
scanf("%s", string2);
if(stringCompare(string1,string2)){
printf("Strings are Equal\n");
return 0;
}
condition = prefixCompare(string1,string2);
if(condition){
printf("Prefix of %d\n", condition);
return 0;
}
condition = suffixCompare(string1,string2);
if(condition){
printf("Suffix of %d\n", condition);
return 0;
}
printf("Characters in common: %s\n", commonChars(string1, string2));
return 0;
}
int stringCompare(char *string1, char *string2)
{
if(!strcmp(string1,string2)){
return 1;
}
return 0;
}
int prefixCompare(char *string1, char *string2)
{
int i, length, length2;
length = strlen(string1);
length2 = strlen(string2);
if(length < length2){
length = length2;
}
for(i=0; i<=length; i++)
if(strncmp(string1,string2,i))
return i-1;
return 0;
}
int suffixCompare(char *string1, char *string2)
{
int i, length, length2;
char rev1[20];
char rev2[20];
length = strlen(string1);
length2 = strlen(string2);
for(i=0; i <= length; i++){
sprintf(rev1,"%s%c", rev1,string1[length - i]);
}
for(i=-1; i <= length2; i++){
sprintf(rev2,"%s%c", rev2,string2[length - i]);
}
printf("\nOriginal String 1: %s\n", string1);
printf("Original String 2: %s\n", string2);
printf("Reversed String 1: %s\n", rev1);
printf("Reversed String 2: %s\n", rev2);
return prefixCompare(rev1, rev2);
}
char* commonChars(char *string1, char *string2)
{
char *pointer = strpbrk(string1, string2);
return &(*pointer);
}
I've attempted to remove the first character of the reversed strings via the following code as stated above, without any success.
Code: Select all
strcpy (rev1, rev1+1);
Code: Select all
Please enter two strings seperated by a space
Strings: goater boater
Original String 1: goater
Original String 2: boater
Reversed String 1: E@dôÿ¿¨óÿ¿ ðóÿ¿Ñóÿ¿retaog
Reversed String 2: ß@@retaob
Characters in common: oater
**DISCLAIMER** This is [obviously] a program for school, so nobody says that I am trying to push my assignment on to others. I have searched for the answer high and low for a long time now (multiple hours, multiple days) and have yet to find an answer, or anything that would make the lightbulb in my head go off to why this may be happening. I hope it is evident that I have tried to do this on my own, and have yet to come up with a way.
I appreciate any help!!




