-1

Yesterday we had an exam in a C language course and question was something like that:

  1. Write a function called toLowercase which takes a character as a parameter and returns uppercase letters to lowercase. If the character is not upper case, the function should return it unchanged.
  2. write a program that takes arbitrarily many characters from user (until s/he enter "!") and then outputs first and last entered uppercase letters in lowercase form.

    a. If the user enters only one uppercase letter, program should output that uppercase letter twice in lowercase form.

    b. We can assume that user always enters at least one uppercase letter.

Example Inputs and Desired Outputs

lskdjfSFFKAFfkafafkKFAFkaKfgRORELKkjfks! ====> sk Mdsfisjf98948*3jkfHf9059,353953,^+%^+%! ====> mh
sjfhsfR'^^''jfi2jpfj99ejfsdfs ====> rr


Here is my code:

#include <stdlib.h>
#include <stdio.h>

char toLower(char n){
    char m=' ';
    if(n>='A'&&n<='Z'){
        m=n-'A'+'a';
        return m;
    }
    else
        return n;
}

int main(){
    int i=0;
    char ch,chmod,chFirst=' ',chLast=' ';
    scanf("%c",&ch);
    while(ch!='!'){
        chmod=toLower(ch);
        if(chmod!=ch){
            chLast=chmod;
            i++;
        }
        if(i==1)
            chFirst=chmod;

        scanf("%c",&ch);
    }
    if(i==1)
        chFirst=chLast;

    printf("%c%c",chFirst,chLast);



    return 0;
}

I tried this code with three different compilers and it outputs wrong results. There are some strange things. It results the first letter wrong but the second letter correct. If the input contains only one uppercase letter, the output is correct again. Lastly, I checked this code over and over again.

What's wrong with this code?

  • 6
    Please change your title to something meaningful "What's wrong with my code" won't help any other user that has the same problem, because it doesn't describe the problem. – Borgleader May 14 '15 at 15:29
  • 2
    No doubt that the subject needs a change but this question does not deserve negative and close votes. It is perfectly written, OP has tried to solve the problem, given his sample code etc. +1 for counteracting the negative votes given. – Yavar May 14 '15 at 15:46

3 Answers3

1

Set chFirst only if it's stil == ' ' and do it in the first if. This way you can get rid of variable i...

marom
  • 5,064
  • 10
  • 14
1

The problematic logic is here:

while(ch!='!'){
    chmod=toLower(ch);
    if(chmod!=ch){
        chLast=chmod;
        i++;
    }
    if(i==1)
        chFirst=chmod;

    scanf("%c",&ch);
}

Consider the case if the first letter is uppercase. Then, chLast will be changed, i incremented, then chFirst will be changed.

Now if the second letter is not uppercase, i will not be incremented and will still be 1. So chFirst will be changed again, which is not what you want.

A better logic would be as follows:

while(ch!='!'){
    chmod=toLower(ch);
    if(chmod!=ch){
        chLast=chmod;
        if(i==0) {
            chFirst=chmod;
            i=1;
        }
    }
    scanf("%c",&ch);
}

In this improved logic, you are effectively using i as a boolean variable, with 0 indicating that chFirst is unset, and 1 indicating that chFirst is set. Note that your final check of if(i==1) chFirst=chLast; is not necessary.

user12205
  • 2,684
  • 1
  • 20
  • 40
0

As @ace pointed out, there is a slight problem in your logic. The solution would be to bring this code:

  if(i==1)
        chFirst=chmod;

inside the previous if statement, like this:

if(chmod!=ch){
        chLast=chmod;
        i++;
          if(i==1)
        chFirst=chmod;
    }

Correct output

Ayushi Jha
  • 4,003
  • 3
  • 26
  • 43