2

I just saw that this could technically work, the only mistake I couldn´t resolve was the last ASCII character that gets printed everytime I test it out, I also tested this out without using the name variable, I mean just making a substraction of 32 to any lower case letter in ASCII should give me their upper case one and it does, but I´m curious on why I´m getting an additional char, wich from what I see in screen is apparently Û.

#include <stdio.h>
 main()
{
char name[22];
int i;

fputs("Type your name ",stdout);
fgets(name,22,stdin);


for (i = 0; name[i] != '\0'; i = i + 1)
printf("%c",(name[i])-32);  /*This will convert lower case to upper */
                            /* using as reference the ASCII table*/   
fflush(stdin);
getchar();
} 
Kyle Hale
  • 7,912
  • 1
  • 37
  • 58
Yudop
  • 131
  • 3
  • 12

3 Answers3

4

Perhaps there is a line break character at the end of the string.

You can check the chararacter code, so that you only convert characters that actually are lower case letters:

for (i = 0; name[i] != '\0'; i = i + 1) {
  char c = name[i];
  if (c => 97 && c <= 122) {
    c -= 32;
  }
  printf("%c", c);
}
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • Thanks it definitely is a solution , and as an other alternative, I remembered that fgets() gets from the input the new line after I finish typing the name, I should have added this next line after fgets() ***name[strlen(name)-1] = '\0';*** So it can delete the new line I type when converting and it takes it as a null. – Yudop Nov 14 '12 at 01:24
  • I know that this question is an old one; however, I'd like to enforce everyone to use `unsigned char` in such operations, since character may be sgned on some platforms. – ghostmansd Apr 10 '14 at 21:59
0
void read_chararray(char in_array[], int* Length)
{
    int Indx = 0, Indx2 = 0, Indx3 = 0;  // int declarations for indexs of some loops
    char cinput = { 0 }, word[255] = { 0 }, word2[255] = { 0 }; // declaration of cinput and first char array before punctiation removed

    for (Indx = 0; (cinput = getchar()) != '\n'; Indx++) { // Loop for getting characters from user stop at <enter>
        word[Indx] = cinput;                    // Placing char into array while changing to lowercase
    }   
    
    Indx2 = Indx;                               // Set Indx2 to Indx for loop operation
    for (Indx = 0; Indx < Indx2; Indx++) {      // Loop to check and replace upper characters with lower
        cinput = word[Indx];
        if (cinput >= 65 && cinput <= 90) {     // If cinput is within the ASCII range 65 and 90, this indicates upper characters
            cinput += 32;                       // Add 32 to cinput to shift to the lower character range within the ASCII table
            in_array[Indx] = cinput;            // Input new value into array pointer
        }
        else if (cinput >= 97 && cinput <= 122) // scans if character are lower ASCII, places them in array irraticating punctuation and whitespce
            in_array[Indx] = cinput;            // Input remaining lower case into array pointer
    }
    
    *Length = Indx;                              // final size of array set to Length variable for future use
}
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
Chris
  • 1
-1
#include<stdio.h>

void upper(char);

void main()
{
    char ch;
    printf("\nEnter the character in lower case");
    scanf("%c", &ch);
    upper(ch);
}

void upper( char c)
{
    printf("\nUpper Case: %c", c-32);
}
nha
  • 17,623
  • 13
  • 87
  • 133