tags:

views:

209

answers:

2

I got this code.. now the challenging part is my prof asked me to make a program which asks the user to input an uppercased word.

The problem is, she wants the program to automatically transform each inputted letter in uppercase even if the user's keyboard is not in capslock mode.. so i don't know what's really wrong with my program... anyone?? help?? i really need it.. thanks..

#include<stdio.h>
#include<ctype.h>
#include<string.h>
typedef char String[100];
main()
{
    char Resp;
    int l, x = 0, asc = 13;
    String s, word1;
    clrscr();
    do {
        printf("\n1st uppercased word: ");
        do {
            s[0] = getch();
            word1[x++] = s[0];
            strupr(s);
            strcat(s, "\0");
            puts(s);
        } while (s[0] != (char) asc);
        strcat(word1, "\0");

        printf("\n\n1st word in lowercase: ");
        for (l = 0; l < strlen(word1); l++)
            putchar(tolower(word1[l]));

        printf("\nDo you want to continue?[Y/N]: ");
        Resp = getche();
    } while (toupper(Resp) == 'Y');

    getch();
    return 0;
}
+12  A: 
  1. Get a letter from the user with getch()
  2. Convert it to uppercase with toupper()
  3. Display it with putch()
  4. Go to 1

You may add a break point --- check if the character is the return key and exit.

Jacob
A: 

BTW, there are some syntax errors in your code.

If your main return 0, you should call it int main.

Your typedef String is useless there ; You should keep the char declaration, it makes your code clearer, and because of some security reasons.

Like Jacob said, it's better if you do something like that :

char c;
do 
{
    c = getch();              // Get from keyboard
    word1[x++] = toupper(c);  // Store
    putch(c);                 // Display

} while (c != '\n');

Maybe a if (x >= 100) break; may be appreciated, because your word1 is an array of 100 chars, and you could crash your program if you override memory limits.

Spl3en
`main` (and all functions really) return `int` by default if no return type is specified. Also, using a typedef or not is not going to help you with buffer overflows.
dreamlax
You may agree that it's easier for a begginer to find a buffer overflow with a char c[100]; than a String c; ... IMHOBTW, (cf 5.1.2.2.1) main must return int, but I find it somehow "lazy" to go beyond it because functions return int by default. Still IMHO.
Spl3en
If no return type for main is provided it will be defined with return type `int`, conforming to the standard.
dreamlax