Tutorial :strupr(); made by me not working



Question:

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


Solution:1

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; to string[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() and toupper(), but this is good as an exercise.


Solution:2

This is horrible, if you don't mind me saying. Don't take this personally, of course.

  • Don't rely on ASCII values, never hardcode actual character values as literal numbers.
  • Don't use char variables as loop indices; they're typically too small.
  • Use islower() to check if a character is lower-case.
  • Use toupper() to convert it to upper-case.
  • Realize that code like this doesn't work for the majority of languages
  • Using wide characters is not enough either; some languages have words that change length (charactercount) when going from lowercase to upper.

My suggested implementation in this style would be:

#include <ctype.h>    void xstrup(char *string)  {    for(; *string; string++)      *string = toupper((unsigned char) *string);  }  

Note that this is just a reasonable (in my opinion) implementation of what it looked like you wanted to achieve, it's not by any means perfect and does not address all of my complaints.


Solution:3

You're returning an integer (either 0 or i-64) and treating it like it was a pointer to a null terminated string. I'm not surprised it prints junk.


Solution:4

Actually in ASCII Chart Ranges are as follows:

  • A to Z: 65 to 90
  • a to z: 97 to 122

Your edited program is like that:

#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]-=32;      else          if(string[i]=='\0')          break;      }  }  

See the ascii table below:

ASCII Table http://www.unitechnical.info/Products/ASCII-Chart.jpg


Sources:

Image is taken from http://www.unitechnical.info/


Solution:5

The function exits when a lowcase character is encountered, instead it shall replace the current character.

Also the type returned by strupr() is wrong, it shouldn't be char. And it does not return the right thing. So it certainly outputs garbage.

[I don't give you all the details since you seem to be learnig, feel free to ask for more].


Solution:6

First, the difference in ASCII value between uppercase and lowercase letters is 32, not 64.

Second, and most important, the C library provides tolower() and toupper() functions, which work not only on ASCII, but in whatever encoding you are currently using.


Solution:7

  1. First, your function returns char. This is irrelevant.
  2. return would break from the function, not replace a character.
  3. This is completely wrong. Upr cannot be done on per-character basis. Example. Upr of the german eszett 'ß' is two characters, aka "SS". Assumping your char* input does not contain utf-8 is a 'text crime'.

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »