user379888
user379888

Reputation:

cant convert lowercase to uppercase a string by preprocessor directive

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;
 }

}

Upvotes: 2

Views: 6251

Answers (4)

chisophugis
chisophugis

Reputation: 799

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.

Upvotes: 2

Byron Whitlock
Byron Whitlock

Reputation: 53901

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.

Upvotes: 3

Borealid
Borealid

Reputation: 98509

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.

Upvotes: 6

James McNellis
James McNellis

Reputation: 355187

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.

Upvotes: 4

Related Questions