Hello! I made a program in Dev c++ in windows that does several string functions.It works perfect in windows but when I tried to use it in Linux(ubuntu and CentOs), linux make the executable file which means it has no syntact error or something but when I try the several string functions most of them didn't work and one of them works wrong.It's a university homework that I have to give in 3 hours so please someone help me :( Here is the code:
#include <stdio.h>
#include <stdlib.h>
#define MAX 50 //orismos megethous
void lower_P(char* str); //orismoi synartisewn pou tha xrisimopoihthoun
int startsWith (char* str, char* check);
char* reverse(char* str);
int endsWith(char* str, char* check);
void replaceChar(char* str, char oldChar,char newChar);
int findChar(char* str, char c);
void upper(char* str);
void lower_T(char str[]);
int main () {
int temp = 1, apantisi; //orismoi metavlitwn
char elegxos[MAX];
char str[MAX];
char anazitisi_xar,neos_xar,palios_xar;
while(temp == 1){ //efoson to temp=1 exarxis,to while tha ekteleitai synexeia
printf("\n---------------");
printf("\n- CHOOSE AN OPTION -"); //menou epilogwn pou tha epilexei o xristis
printf("\n---------------\n");
printf("\n1. Convert to Lowercase with Table\n");
printf("2. Convert to Lowercase with Pointers\n");
printf("3. Convert to Uppercase\n");
printf("4. Find Character in String\n");
printf("5. Replace character with other character\n");
printf("6. Reverse String\n");
printf("7. Check if string starts with another string\n");
printf("8. Check if string ends with another string\n");
printf("9. EXIT\n");
printf("\nPlease select function : ");
scanf("%d", &apantisi); //i metavliti apantisi einai o arithmos pou edwse o xristis
fflush(stdin); //to fflush katharizei to input buffer gia na min yparxei provlima enosw pairneis user inputs apo to stdin
if (apantisi != 9){ //efoson o arithmos pou epilegei o xristis den einai to 9 pou einai to exit tha ziteitai to string
printf("Enter input string : ");
fgets(str,MAX,stdin);
fflush(stdin);
}
if(apantisi == 1){ //ean i apantisi einai 1 kaleitai i sinartisi lower_T na kanei to string me mikra grammata mikra mesw pinaka
lower_T(str);
printf("\nThe new string is : %s",str);
}
else if (apantisi == 2){ //ean i apantisi einai 2 kaleitai i sinartisi lower_P na kanei to string me mikra grammata mesw deiktwn
lower_P(str);
printf("\nThe new string is : %s",str);
}
else if (apantisi == 3){ //ean i apantisi einai 3 kaleitai i sinartisi upper na kanei to string me megala grammata
upper(str);
printf("\nThe new string is : %s",str);
}
else if (apantisi == 4){ //ean i apantisi einai 4 kaleitai i sinartisi findChar na mas pei se poia thesi sto string einai to gramma pou exei dwsei o xristis
printf("Please give the character to search : ");
scanf("%c",&anazitisi_xar);
printf("The position of character is : %d",findChar(str,anazitisi_xar));
}
else if (apantisi == 5){ //ean i apantisi einai 5 i sinartisi replaceChar mesw twn metavlitwn palios_xar kai neos_xar antikathista to gramma pou dinei stin arxi o xristis me ena neo pou dinei meta
printf("Please give the old character : ");
scanf("%c",&palios_xar);
fflush(stdin);
printf("Please give the new character : ");
scanf("%c",&neos_xar);
fflush(stdin);
replaceChar(str, palios_xar,neos_xar);
printf("\nThe new string is : %s",str);
}
else if (apantisi == 6) {
fgets(str,MAX,stdin);
fflush(stdin);
printf("The reverse string is : %s \n",reverse(str));
}
else if (apantisi == 7) {
printf("\nPlease give the check string : ");
fflush(stdin);
fgets(elegxos,MAX,stdin);
fflush(stdin);
apantisi = startsWith (str,elegxos);
if(apantisi == 1)
{
printf("\n Check string is the beginning of input string ? Answer is : TRUE\n");
}
else
{
printf("\n Check string is the beginning of input string ? Answer is : FALSE\n");
}}
else if (apantisi == 8) {
printf("\nPlease give the check string : ");
fflush(stdin);
fgets(elegxos,MAX,stdin);
apantisi = endsWith (str,elegxos);
if(apantisi == 1)
{
printf("\n Check string is the ending of input string ? Answer is : TRUE\n");
}
else
{
printf("\n Check string is the ending of input string ? Answer is : FALSE\n");
}}
else if (apantisi == 9){
return 0;
}
}
return 0;
}
void lower_T(char str[]) // metatrepei me pinaka ta grammata apo kefalaia se mikra
{
int a=0;
while (str[a] != '\0')
{
if((str[a] > 64) && (str[a] < 91))
{
str[a] = str[a] + 32 ;
}
++a;
}
}
void lower_P(char* str) // Metatrepei me deiktes ta grammata apo kefalaia se mikra
{
while(*str != '\0')
{
if(*str > 64)
{
if(*str < 91)
{
*str = *str + 32 ;
}
}
*str++;
}
}
void upper(char* str) // Metatrepei ta mikra grammata se kefalaia
{
while(*str != '\0')
{
if((*str > 96) && (*str < 123))
{
*str = *str - 32 ;
}
*str++;
}
}
int findChar(char* str, char c) // Vriskei xaraktira pou dinei o xristis mesa sto sring
{
int i=0;
while(*str != '\0')
{
if(*str == c )
{
return i;
}
*str++;
++i;
}
return -1;
}
void replaceChar(char* str, char oldChar,char newChar) //Antikathista ena xaraktira me allo xaraktira
{
while(*str != '\0')
{
if(*str == oldChar )
{
*str = newChar;
}
*str++;
}
}
char* reverse(char* str) // Antistrefei to string
{
int i,j;
char* my_str = (char*)malloc(sizeof(str));
int length = 0;
while(*(str + length) != '\0')
{
length ++ ;
}
i=0;
for(j=length-2;j>=0;j--){
my_str[i] = *(str+j);
++i;
}
my_str[length]='\0';
return my_str;
}
int startsWith (char* str, char* check) // Elegxei an to string 3ekina me allo string
{
int i=0;
while (*(str+i) != '\0')
{
if((*(check+i) == '\n') || (*(check+i) == 0) || (*(check+i) == 62))
{
return 1;
}
if(*(check+i) != *(str+i))
{
return 0;
}
++i;
}
return 0;
}
int endsWith (char* str, char* check) // Elegxei an to sting teliwnei me allo string
{
char *a;
char *b;
int i = 0;
char my_a[MAX];
char my_b[MAX];
a = reverse(str);
b = reverse(check);
while (*a != '\0')
{
my_a[i] = *a;
++a;
++i;
}
my_a[i] = '\0';
i = 0;
while (*b != '\0')
{
my_b[i] = *b;
++b;
++i;
}
my_b[i] = '\0';
return startsWith(my_a,my_b);
} // Teliwnei to programma