top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

Write a program in C for changing case of given input?

+2 votes
469 views
Input: My SpAce
Output: mY sPaCE
posted Nov 20, 2014 by anonymous

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button
main()
{
    int i;
    char name[9]="My SpAcE";
    char nam[9];
    printf("INPUT :%s",name);
    for(i=0;i<9;i++)
    {
        nam[i]= islower(name[i])?toupper(name[i]):tolower(name[i]);
       
    }
         printf("\nOUTPUT:%s\n",nam);

   
}

2 Answers

+2 votes
int main()
{
    char buff[100];
    int i = 0;
    printf("enter the input string\n");
    scanf("%[^\n]",buff);
    while(buff[i]){
       if(!(buff[i] ^= 32))
            buff[i] ^= 32;
       i++;
    }
    printf("case converted str :%s\n",buff);

}
answer Nov 20, 2014 by Bheemappa G
+2 votes

Here is my code(Tested).

Note:
You have to substract 32 if you want to change the char form lover to upper case and have to add 32 to that character for upper to lower case.
As ASCII value of A = 65 and Z = 90 and a = 97 and z = 127, and their difference is 32 i.e (a - A) or (z - Z).

#include <stdio.h>

int main()
{
    char str[10] = "My SpAce";
    int i;

    printf("String before change : %s\n", str);

    for (i = 0; str[i]; i++) {
        if (str[i] != ' ') {
           if (str[i] >= 'a' && str[i] <= 'z') {
                str[i] -= 32;
            } else if (str[i] >= 'A' && str[i] <= 'Z') {
                str[i] += 32;
            }
        }
   }

   printf("String After  change : %s\n", str);

   return 0;
}
answer Nov 20, 2014 by Arshad Khan
Similar Questions
+3 votes

Given a 2d array, u need to sort the 2 diagonals independently.
And the elements in diagonal should remain in diagonal only.

INPUT =>
24 2 3 4 7

6 13 8 5 10

11 12 9 14 15

16 21 18 25 20

17 22 23 24 1

OUTPUT =>
1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

16 17 18 19 20

21 22 23 24 25

+1 vote

Write your own rand function which returns random numbers in a given range(input) so that the numbers in the given range is equally likely to appear.

0 votes

Enter character: r
Enter string: programming
Output: Positions of 'r' in programming are: 2 5
Character 'r' occurred for 2 times

...