2

i made a program which converts lower case to upper case a string.i know how to convert a char to upper case via preprocessor directives but i dont know how to do it for a string.

#include<stdio.h>
#include<conio.h>
#include<ctype.h>
#define UPPER([])  ([]-32)
void fstring_convert(char string[]);
void main(void)
{
char string[40];
printf("Enter a string:");
gets(string);
fstring_convert(string);
printf("%s",string);
getch();
}

void fstring_convert(char string[])
{
    int i;
 for(i=0; ;i++)
 {
    if(string[i]==' ')
    {
        string[i]=string[i+1];
    }
    if(isdigit(string[i]))
    {
    string[i]+=1;
    }
    UPPER('string[i]');
    if(string[i]=='\0')
    break;
 }

}
2
  • 7
    IS that your homework assignment? To convert a string to uppercase with macros? Really? Commented Jul 9, 2010 at 19:51
  • 1
    Can't imagine that your bizarre attempt to define a macro named UPPER runs through the preprocessor. Please only post code that compiles. Commented Jul 9, 2010 at 20:38

4 Answers 4

6

Preprocessors do not have loops.

Thus, for a string of arbitrary length, you cannot convert all characters to upper case with a preprocessor macro.

The code you have above is buggy because your macro should look like:

#define TOUPPER(x) x = (x>='a' && x<='z')?(x-32):x;

And then call TOUPPER(string[i]) in your for loop.

But I don't see what the point of the macro is.

Sign up to request clarification or add additional context in comments.

Comments

4

You should use the C standard library function toupper() on each character of the string (in a loop). This function has the advantage of correctly handling non-alphabetic characters.

4 Comments

i am told to use preprocessor directives
@fahad: That is a most unusual requirement. There's always the option of #define my_toupper(x) toupper(x).
you mean i can call a built in function within the preprocessor?
@fahad: Do you know what the preprocessor does? If not, you should consult your C book.
3

This homework assignment is to teach you about ascii and type conversion.

Loop through the string one letter at a time. For each letter, lookup the ascii code for the letter, (find the offset to uppercase do this once while coding and store in a constant), then add the offset to the letter.

Hint: a char can be cast as an int.

Comments

2

If you're assuming that you're dealing with ASCII, then you might as well take advantage of how the characters are laid out. To convert to upper case, do c & ~0x20. To convert to lower case, do c | 0x20. To toggle between upper and lower case, c ^ 0x20. These basically amount to adding or subtracting 32 (== 0x20), but they are better in that applying them repeatedly does what you expect e.g. toupper(toupper(c)) is upper case, instead of c - 64 i.e. garbage.

Check out http://www.kernel.org/doc/man-pages/online/pages/man7/ascii.7.html, especially the hex table near the end. It clearly shows the relationship between different characters. There are some nice patterns, but I suspect that for historical reasons there are some unfortunate incongruences. For example, to convert between [ and ] or { and } you can just do 'c ^ 0x6', but between ( and ) it's different, and its different for < and > as well. However, it is still nonetheless possible to define a branch-free (i.e. no ifs and such) expression to compute the matching delimiter of any given delimiter.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.