Click here to Skip to main content
15,920,217 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Description
Write a function that takes one string and, capitalize the last character
of each word in uppercase and the rest in lowercase.

A word is a section of string delimited by spaces/tabs or the start/end of the
string. If a word has a single letter, it must be capitalized.

A letter is a character in the set [a-zA-Z]


I keep getting errors and failures, which I cannot figure out

What I have tried:

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

char* rcapitalize(char* str) {
    int i = 0;
    while (str[i] != '\0') {
        if (isalpha(str[i])) {
            str[i] = tolower(str[i]);
            if (!isalpha(str[i + 1])) {
                str[i] = toupper(str[i]);
            }
        }
        i++;
    }
    return str;
}

Below is the error message I get when I try to push gandalf on qwassar. Although, case 0,1 and 2 were successful and case 3 and 4 failed.
               REPORT        

 Status             FAILURE     
 Execution Runtime  0.002138    
 Score              [===--] 3/5 

Checks detail Report:

      TEST CASE 0              SUCCESS        

 Input                  "a FiRSt LiTTlE TESt" 
 Expected Output        ""                    
 Expected Return Value  A firsT littlE tesT   
 Output                 ""                    
 Return Value           "A firsT littlE tesT" 

      TEST CASE 1       SUCCESS 

 Input                  ""      
 Expected Output        ""      
 Expected Return Value          
 Output                 ""      
 Return Value           ""      

      TEST CASE 2                      SUCCESS                  

 Input                  "SecONd teST A LITtle BiT   Moar comPLEX" 
 Expected Output        ""                                        
 Expected Return Value  seconD tesT A littlE biT   moaR compleX   
 Output                 ""                                        
 Return Value           "seconD tesT A littlE biT   moaR compleX" 
      
      TEST CASE 3                      FAILURE                

 Input                  "   But... This iS not THAT COMPLEX" 
 Expected Output        ""                                   
 Expected Return Value     but... thiS iS noT thaT compleX   
 Output                 ""                                   
 Return Value           "   buT... thiS iS noT thaT compleX" 

      TEST CASE 4                      NOT TESTED                               

 Input                  "     Okay, this is the last 1239809147801 but not    the least    t" 
 Expected Output        ""                                                                    
 Expected Return Value       okay, thiS iS thE lasT 1239809147801 buT noT    thE leasT    T   
 Output                 ""                                                                    
 Return Value
Posted
Updated 2-Jan-24 6:12am
v3
Comments
Dave Kreskowiak 31-Dec-23 2:08am    
Without you showing what the EXACT error messages and/or describing the failures, it's impossible to tell you what's going on.

I have looked at test case 3. My test program looks like this:

C
char testcase3[] = " But... This iS not THAT COMPLEX";
// Expected Return Value " but... thiS iS noT thaT compleX"

char* data = testcase3;
char* result = rcapitalize(data);
printf("%s\n", result);

As the isalpha() function evaluates one (or more) points as false, the T of But is capitalized. If this result is not desired, the question would be which rule should apply. If the last letter at the end of a sentence should always be capitalized, but not if two or more dots follow, a rule would be required, which is missing here.

//edit:
Obviously you have to take care of full stops and commas.
C
char* rcapitalize(char* str)
{
    int i = 0;
    while (str[i] != '\0') {
        if ((str[i] == '.' || str[i] == ',') && i > 0) {
            // TODO: Handle a full stop or a comma preceded by a letter
        }
        else {
            // current character is a letter
            if (!isalpha(str[i + 1]) || str[i + 1] == '\0') {
                str[i] = toupper(str[i]);
            }
            else {
                str[i] = tolower(str[i]);
            }
        }
        i++;
    }
    return str;
}

Instead of the while loop, I would recommend a for loop.
C
for (int i = 0; str[i] != '\0'; i++) { ...
 
Share this answer
 
v3
Comments
xcoin 1-Jan-24 14:30pm    
I have done all I could but still not working out for me.
Gandalf doesn't allow the int main function
merano99 1-Jan-24 18:29pm    
I have no idea what Gandalf has to do with the problem. The description of the task lacks information on what exactly should happen with a full stop or a comma, for example.
I have expanded my solution a little and assume that you can now do the rest yourself.
xcoin 2-Jan-24 6:20am    
Thank you very much, you have done well for me
merano99 2-Jan-24 9:25am    
I am pleased that I was able to contribute to the solution. Please don't forget to award the stars and mark my suggestion as solution so that the question is displayed as solved.
If I run your code:
C
#include <stdio.h>
#include <ctype.h>

char* rcapitalize(char* str) 
    {
    int i = 0;
    while (str[i] != '\0') 
        {
        if (isalpha(str[i])) 
            {
            str[i] = tolower(str[i]);
            if (!isalpha(str[i + 1])) 
                {
                str[i] = toupper(str[i]);
                }
            }
        i++;
        }
    return str;
    }
int main()
    {
    char data[] = "Hello World";
    printf("%s\n", rcapitalize(data));
    return 0;
    }
Then I get what I expect:
Result
hellO worlD
Which is what the assignment asks for - sort of.

So check how you are calling your function - that may be your problem!
Then read the assignment again: What should be the result if I test "My name is Mike Hyphenated-Surname - can you read this?"?
 
Share this answer
 
Comments
xcoin 1-Jan-24 13:22pm    
Below is the error message I get when I try to push gandalf on qwassar. Although, case 0,1 and 2 were successful and case 3 and 4 failed.
REPORT

Status FAILURE
Execution Runtime 0.002138
Score [===--] 3/5

Checks detail Report:

TEST CASE 0 SUCCESS

Input "a FiRSt LiTTlE TESt"
Expected Output ""
Expected Return Value A firsT littlE tesT
Output ""
Return Value "A firsT littlE tesT"

TEST CASE 1 SUCCESS

Input ""
Expected Output ""
Expected Return Value
Output ""
Return Value ""

TEST CASE 2 SUCCESS

Input "SecONd teST A LITtle BiT Moar comPLEX"
Expected Output ""
Expected Return Value seconD tesT A littlE biT moaR compleX
Output ""
Return Value "seconD tesT A littlE biT moaR compleX"

TEST CASE 3 FAILURE

Input " But... This iS not THAT COMPLEX"
Expected Output ""
Expected Return Value but... thiS iS noT thaT compleX
Output ""
Return Value " buT... thiS iS noT thaT compleX"

Your Stderr:
exit 0

TEST CASE 4 NOT TESTED

Input " Okay, this is the last 1239809147801 but not the least t"
Expected Output ""
Expected Return Value okay, thiS iS thE lasT 1239809147801 buT noT thE leasT T
Output ""
Return Value
OriginalGriff 2-Jan-24 1:57am    
Look at the assignment again, as I suggested. The 3rd test case specifically checks that you did exactly what the question asked for, but ... you didn't do that.
Look at the difference between your code output and the expected output - how do they differ? Why do they differ? What part of your code is responsible for that?
This is basic debugging: looking at what the code generates and what it should. If you wrote the code, it should be obvious exactly what is wrong, and how to fix it - and if you didn't write the code then it's a very good example of why you shouldn't copy and paste code blindly and hope it will work ... :D
I was able to solve the problem.

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

char* rcapitalize(char* param_1) {
    int len = strlen(param_1);

    for (int i = 0; i < len; i++) {
        if (islower(param_1[i])) {
            if (i == len - 1) {
                param_1[i] = toupper(param_1[i]);
            } else if (param_1[i + 1] == ' ' || param_1[i + 1] == '\t') {
                param_1[i] = toupper(param_1[i]);
            }
        } else if (isupper(param_1[i])) {
            if (i != len - 1 && param_1[i + 1] != ' ' && param_1[i + 1] != '\t') {
                param_1[i] = tolower(param_1[i]);
            }
        }
    }

    return param_1;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900