i made a program to convert lowercase strings to upper case like strupr(); in strings.h ..its printing some ascii code when ever i run the program
#include<stdio.h>
#include<conio.h>
void xstrupr(char string[]);
void main(void)
{
char string[40];
puts("Enter string:");
gets(string);
xstrupr(string);
printf(" %s ",string);
getch();
}
void xstrupr(char string[])
{
int i;
for(i=0;;i++)
{
if ((string[i]>='a')&&(string[i]<='z') )
string[i]+=64;
else
if(string[i]=='\0')
break;
}
}
It looks to me like you’re trying to do this at the most basic level. That being said, you’re making one erroneous assumption.
You don’t get the uppercase version of a letter by adding 64 to it. Moreover, just supplying a magic number is unclear and may be wrong on another character set.
Try changing
string[i] += 64;tostring[i] += 'A' - 'a';. That will work on all character sets where there’s a constant difference between uppercase and lowercase letters.Now, this will fail in some cases. For example, in EBCDIC, the letters are not contiguous, so the range from ‘a’ to ‘z’ is not all alphabetic. This is why, in real code, you use standard features like
isalpha()andtoupper(), but this is good as an exercise.