Home > Back-end >  My teacher wrote a comment on my programming assignment. I need someone to explain what she means?
My teacher wrote a comment on my programming assignment. I need someone to explain what she means?

Time:10-02

it is so that I have received a comment from my teacher about my program where she wrote.

"The function no_special_characters has return type int but does not return anything (so no return statement). If the function should not return anything, there is a special return type for it. The function to_lower returns 0 but this value is never received at the call, is it really necessary for the function to return something? Otherwise, the solution works as it should."

Does she mean that I should change int to_lower and int no_special_character to void and remove return_sats from these functions? If I do that, the program still works.

Ps:Pardon for my bad english,it not my nativie language.

Forgot to write i change my function from int to void!!

My code:

    #include<stdio.h>
#define SIZE 1000
#include <ctype.h>
#include<string.h>

//funktion som kollar om det är en palindrom
int isPalindrome(char inputString[]) {
    int l = 0;
    int r = strlen(inputString) - 1;


    while (r > l)
    {
        // kommer att kontrollera att alla bokstäver är lika med varandra
        if (inputString[l  ] != inputString[r--]) {
            return 0;
        }// retunera 0 om det inte är en palindrom
    }
    

    return 1;

}

// funktion som ignorerar alla icke - bokstäver
void  no_special_characters(char inputString[],char outputstring[])
{
    int temp_index = 0;
    int abc = 0;
    int r = strlen(inputString);
    for (int i = 0; i < r; i  )
    {
        char abc = inputString[i];
        if (isalpha(abc) != 0)
        {
            outputstring[temp_index  ] = abc;
        }
    }
    outputstring[temp_index] = '\0';

    
}

// funktion som konverterar stora bokstäver till to_lower

void to_lower(char inputstring[]) {

    int length = strlen(inputstring);

    for (int i = 0; i < length; i  )
    {
        if (isupper(inputstring[i]))
            inputstring[i] = tolower(inputstring[i]);
        
    }

    
}




    int main(void) {
    
    
        int end_run = 0;
        int try_again = 1;
    
    
        while (try_again == 1) {
            
    
            char inputString[SIZE] = "";
            char outputstring[SIZE] = "";
    
            printf("Enter a string to check if it is a palindrome!\n");
            //Skannanar inmatningssträngen.
            gets_s(inputString, SIZE);
    
            printf("input: %s\n", inputString);
            to_lower(inputString);
            printf("lower: %s\n", inputString);
            no_special_characters(inputString, outputstring);
            printf("speci: %s\n", outputstring); // skriver inte ut mellan slag
            //Sends the string to the isPalindrome function. //If the return value is 1(true), the if statement is executed, otherwise the else statement.
            if (isPalindrome(outputstring)) {
                printf("That is a palindrome!\n");
            }
            else {
                printf("This is not a palindrome!\n");
            }
    
            printf("Do you want to try again: 1 for yes 0 for No?");
            scanf_s("%d", &try_again);
            while(getchar()!='\n');
    
            //Changes the value of running depending on whether you want to continue or not.
            if (try_again != 1) {
                end_run = 0;
            }
    
    
        }   return 0;

CodePudding user response:

If a function doesn't have anything useful to return, then it it is totally OK for the function to be declared as void and not return anything. It even makes the code more confusing if you just add a dummy return 0 just to satisfy a return type of int.

But even if you don't have to return anything that doesn't mean that you can't look for something useful to return, even if the caller doesn't always use the returned value.

For instance, your to_upper function could be defined like this:

char *to_lower(char inputstring[]) {

    int length = strlen(inputstring);

    for (int i = 0; i < length; i  )
    {
        if (isupper(inputstring[i]))
            inputstring[i] = tolower(inputstring[i]);
        
    }

    return inputstring;   
}

Here the pointer to the modified string get passed through, so you have the option to use the function in a more functional way. Depending on what is more readable in the given situation, you could rewrite the following:

to_lower(inputString);
printf("lower: %s\n", inputString); 

into this:

printf("lower: %s\n", to_lower(inputString));

Whatever is more clear is a matter of taste, but by returning inputstring, you now have the option. A lot of functions in the standard libraries follows this template.

CodePudding user response:

Your teacher was only trying to raise your awareness of the possible usefulness of writing functions that return useful values (and then using those values.)

In the following "compacted" version of your code, the two functions (no_spcl() and str_lower()) both return the address of the string that they have processed. Because they return the start address, the output of one function can be used as the input to the next.

I hope this provides you with some useful "learning material".

#include <stdio.h>
#include <ctype.h>
#include <string.h>

int isPalindrome( char str[] ) {
    int l = 0;
    int r = strlen( str ) - 1;

    while( l < r && str[l] == str[r] )
        l  , r--;
    return l >= r;
}

char *no_spcl( char str[] ) {
    for( int i = 0, o = 0; ( str[o] = str[i] ) != '\0'; i   )
        if( isalpha( str[o] ) )
            o  ;
    return str;
}

char *str_lower( char str[] ) {
    for( int i = 0; str[i]; i   )
        str[i] = (char)tolower( (unsigned char)str[i] );
    return str;
}

#define SIZE 128
int main( void ) {
    char str[ SIZE ] = { 0 };

    while( !strpbrk( str, "nN") ) {
        printf( "Enter a string to check if it is a palindrome.\n" );
        fgets( str, SIZE, stdin );
        printf( "input: %s", str );

        if( isPalindrome( str_lower( no_spcl( str ) ) ) )
            puts( "That is a palindrome!\n" );
        else
            puts( "This is NOT a palindrome!\n" );

        printf( "Try again? (no to exit): " );
        fgets( str, SIZE, stdin );
    }

    return 0;
}
Enter a string to check if it is a palindrome!
leVEL
input: leVEL
That is a palindrome!

Try again? (no to exit): y
Enter a string to check if it is a palindrome!
Madam, I'm Adam!
input: Madam, I'm Adam!
That is a palindrome!

Try again? (no to exit): y
Enter a string to check if it is a palindrome!
quick brown fox
input: quick brown fox
This is NOT a palindrome!

Try again? (no to exit): n
  •  Tags:  
  • c
  • Related