-5

I don't know why input capital letters in string are changed to random code

#include<stdio.h>
#include<string.h>
#define length 100
int main()
{
   int count;
   char word[length];
   printf("Please input your word =");
   scanf("%s", &word);
   count=strlen(word);
   int i;
   for(i=0;i<=count;i++)
   {
        if(word[i]>=97||word[i]<=122)
        {
           word[i]+= -32;
        }
        printf(" %c",word[i]);
   }
   return 0;
}
Serpiton
  • 3,676
  • 3
  • 24
  • 35
  • 3
    Please use toupper - http://linux.die.net/man/3/toupper – Ed Heal Jun 21 '14 at 06:48
  • 6
    In "if(word[i]>=97||word[i]<=122)" - your condition is true for every value. – Ivan Kuckir Jun 21 '14 at 06:51
  • 1
    If you tagged this as C++, why is your code 'C'? Turning a word to upper case in C++ is a one-line algorithm call. Second, your loop has a bug -- it has `i` going up to `<=count`. Therefore you're messing around with the null terminator when you shouldn't be. – PaulMcKenzie Jun 21 '14 at 06:55
  • use `word[i] >= 'a' && word[i] <= 'z'` instead – phuclv Jun 21 '14 at 07:20

3 Answers3

3

Change:

if(word[i]>=97||word[i]<=122)

To:

if(word[i]>=97 && word[i]<=122)
Mahonri Moriancumer
  • 5,993
  • 2
  • 18
  • 28
2

You should be using AND operator instead of OR operator in condition.

if(word[i]>=97 && word[i]<=122)    //to specify small character region's upper AND lower bound

and may be try using

word[i] -= 32; //for simplicity
Murtaza Zaidi
  • 599
  • 3
  • 14
0

|| -------》 &&

#include<stdio.h>
#include<string.h>
#define length 100
int main()
 {
    int count;
    char word[length];
    printf("Please input your word =");
    scanf("%s", word);
    count=strlen(word);
    int i;
    for(i=0;i<count;i++)
       {
           if(word[i]>=97&&word[i]<=122)
             {
                word[i]+= -32;
             }
           printf(" %c",word[i]);
  }
    return 0;
}
NKWBTB
  • 21
  • 4