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

Recommended Answers

All 8 Replies

NEVER use the word urgent in a forum. There is no urgency as far as we're concerned. And many people will simply ignore your post.

And you need to be more specific than "it doesn't work". If you want help, and fast, the more we have to stare at 300 uncommented lines of code trying to understand it, the longer it will take to get an answer.

It ignores all functions.That's what I meant.The user writes the number of the function and it still asks for that number.It doesn't continue,the selected function should do it's work but it doesn't...

Do you have problem only by calling the necessary functions or there are other problems? (after scanf() do you get the values you want? inside the ifs the non-function-calling parts are running well? First lines of the functions are running?)
It would be easy to test it by writing out some messages + values at the right places (if you can't debug the code)

1) Do you actually know what fflush(stdin); does? Read this

2a) After you execute a function, what do you do with the string?
2b) How do you know it doesn't do it's work?

Follow the code carefully.

3) If you would format the code consistently, other problems might show up that we can't see.

For flushing input stream, there is a nice article in daniweb C++ forum. Check this.

For flushing input stream, there is a nice article in daniweb C++ forum. Check this.

And the suggested solution is also for C++ only, it won't work in C because there's insufficient framework in the stdio library to support it.

Found this in C code snippets.

@ deceptikon

Would this work fine in replacement of fflush() ?

Would this work fine in replacement of fflush() ?

It would work, but I think it's excessively complicated. There's no need to store a string when you're just discarding the character. If portability is a must, and you can't switch over to general line input for some reason, I'd recommend something more like this:

/*
    @description:
        Reads and discards characters from the stream pointed to
        by in, until end-of-file, a stream error, or a character
        matching stop_at is read and discarded.

        An end-of-file state is not preserved unless the stream
        is otherwise in error.
*/
void discard(FILE *in, int stop_at)
{
    int ch;

    do
        ch = getc(in);
    while (ch != stop_at && ch != EOF);

    if (feof(in) && !ferror(in)) {
        /*
            Clear the error state if this action took the stream 
            into end-of-file, but is otherwise error free.
        */
        clearerr(in);
    }
}

But that's still not a perfect replacement for fflush() because it blocks for input when the stream has no pending characters. The superior option is to read full lines so that extraneous garbage in the stream doesn't happen.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.