0

Here is my code:

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>

int main()
{
    FILE *fp;
    char s[50];
    fp = fopen("line.txt" , "r");

    if( fgets (s, 50, fp)!=NULL ) 
   {
      puts(s);
   }
   fclose(fp);

  return 0;
}

I know how to display a string in all uppercase and lowercase letters using the printf function. But I don't know how to do it using the fgets function. I am reading a string in from a file too, so thats the tricky thing for me.

  • 1
    You cannot read or write a string in all upper-case using `fgets`. You can read a string into a buffer and then manipulate the characters in the buffer to all upper-case, etc.. – David C. Rankin Apr 21 '16 at 02:56
  • Note that if the line is shorter than 49 characters, you will get two newlines output (one from the string read by `fgets()`, one added by `puts()`). It probably won't matter, but you should be aware of it. – Jonathan Leffler Apr 21 '16 at 03:19

3 Answers3

4
if( fgets (s, 50, fp)!=NULL ) 
{
   for( size_t ii = 0; s[ii]; ii++ )
   {
      s[ii] = toupper((unsigned char)s[ii]);
   }
   puts(s);
}
John Zwinck
  • 239,568
  • 38
  • 324
  • 436
  • Technically it should be `toupper( (unsigned char)s[ii] )`, although modern C libraries allow your usage because it's such a common mistake – M.M Apr 21 '16 at 04:18
  • @M.M: Thanks. That's retarded but I updated my answer to reflect it! – John Zwinck Apr 21 '16 at 07:03
2

After reading a string (by whatever means), in addition to the toupper function in ctype.h, you can make case-coversions from lower-to-upper case (and from upper-to-lower) with simple bitwise operations.

Character Values

You will often hear the term 7-bit ASCII. This is due to the ASCII character set being represented by the first 7-bits in each character value. (the 8th-bit is always 0 for standard characters.) The 6th-bit is the case-bit. (for A-Za-z) For A-Z the case-bit is 0 and for a-z it is 1.

char |  dec  |  hex   |  binary
-----+-------+--------+---------
  A  |   65  |  0x41  | 01000001
  B  |   66  |  0x42  | 01000010
  C  |   67  |  0x43  | 01000011
  D  |   68  |  0x44  | 01000100
  ...
  a  |   97  |  0x61  | 01100001
  b  |   98  |  0x62  | 01100010
  c  |   99  |  0x63  | 01100011
  d  |  100  |  0x64  | 01100100

To change from lower-case to upper-case, all that is required is checking to be sure you are operating on a-z and then simply flipping the 6th-bit from 1 to 0.

In your case, reading a string from stdin and then changing any lower-case characters to upper-case can be done as follows:

#include <stdio.h>

enum { MAXC = 512 };

int main (void) {

    char str[MAXC] = "";
    char *p = str;

    printf ("\n enter str: ");
    if (!fgets (str, MAXC, stdin)) {
        fprintf (stderr, "error: invalid input.\n");
        return 1;
    }

    for (; *p; p++)                 /* for each character */
        if ('a' <= *p && *p <= 'z') /* if lower-case      */
            *p ^= (1 << 5);         /* set case bit upper */

    printf (" uc-string: %s\n", str);

    return 0;
}

Example Use/Output

$ ./bin/str2upper

 enter str: This is A String with UPPPER and lower case.
 uc-string: THIS IS A STRING WITH UPPPER AND LOWER CASE.

Look it over and let me know if you have any questions.

David C. Rankin
  • 81,885
  • 6
  • 58
  • 85
0

strupr() function (a non-standard function from Microsoft's C library ) converts all the lowercase characters in that string to uppercase characters. The resultant from strupr() is stored in the same string.

char str1[]="UppEr Case";
puts(strupr(str1));   //Converts to uppercase and displays it.

or

You can write your own function.

int str_upr(char *cstr)
{
    char *str=cstr;
    for (;*str;str++) {
        if (isalpha(*str))
            *str += 'A' - 'a';
    }
    return 0;
}

This implementation will modify your character array.

However, if you just want to print the character array without modifying the original array, you can use this implementation.

void str_upr(char *str)
{
    int c;
    for (;*str;str++) {
        c=*str;
        if (isalpha(c))
            c += 'A'-'a';
        putc(c, stdout);
    }
}
abhiarora
  • 9,743
  • 5
  • 32
  • 57
  • 2
    `strupr()` is not a standard C function. Its presence cannot be relied upon. – John Zwinck Apr 21 '16 at 02:58
  • See [`strupr()` and `strlwr()` in `` — are they part of Standard C?](https://stackoverflow.com/questions/26327812/strupr-and-strlwr-in-string-h-part-are-of-the-ansi-standard) As the linked question notes, despite Microsoft's assertions that they're part of POSIX, they never have been a part of POSIX — they're for Microsoft only (and Microsoft prefers you to write their names with a leading underscore: `_strupr()` and `_strlwr()`). – Jonathan Leffler Apr 21 '16 at 03:24