-2

i want a solution. is it possible that if i press 1 and get the upper case of the string and when i press 2, then got the lower case if the string via using the switch condition or something else.i just started coding and new to this field.

i tried to do this thing with function, but maybe due to lake of knowledge, id did nit get the result.

int main()
char str[100];
  int i;
//   printf("Enter the string: \n");
//   gets(s);
 switch (case) 
int main() 
{ 
    int case;
  printf("Enter the string: \n");
  scanf("%d", &case);
  gets(str);
   switch(case) 
   { 
       case 1:
       for(i = 0; str[i] != '\0' ; i++)
     if(str[i] >= 'a' && s[i] <= 'z')
       str[i] = str[i] - 32;

  printf("\n The string's upper case = %s", str);
   break;
       case 2: 

       for(i = 0; str[i] != '\0' ; i++)
     if(str[i] >= 'A' && str[i] <= 'Z')
       str[i] = str[i] + 32;

  printf("\n The string's lower case = %s", str);

         break;
       default: printf("Choice other than 1, 2 and 3"); 
                break;   
   } 
   return 0; 
}  

m expecting when i press 1 then get the upper case and when i press 2 i get lower case in string.

  1
hello world
  2
HELLO WORLD

i want to do it with the switch.

Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
dev anand
  • 25
  • 1
  • 4
  • 1
    With that many bugs I don´t get the code compiled. So I wonder how do you get any results with this code. – Kampi Jul 30 '19 at 05:58
  • 1
    Sorry, that's all a mess. `int main()` Syntax error in line 2 (; expected). `switch (case)` statement outside of function (-> Error). `int case;` reserved word `case` used as variable identifier. Please, try to fix these issues first... – Scheff's Cat Jul 30 '19 at 05:59
  • the code is what i was trying. it contains a lot of errors. if you guys can fix it, then please help me out. i hope you are getting what i wanna do. – dev anand Jul 30 '19 at 06:02
  • [**Live Demo on coliru**](http://coliru.stacked-crooked.com/a/1bc261feebc26bbb) gcc complains a bit different. (It tries to read lines 1 ... 3 as ancient K&R C function. (Very funny but for all that failing.)) – Scheff's Cat Jul 30 '19 at 06:02
  • If you want to learn, please, get a book and try to fix by yourself. Start with a minimal sample e.g. `int main(void) { return 0; }`. Compile. Enjoy that's working (although it does not much). Then add a bit code. Compile (and fix if necessary) until it's working. Enjoy. Then add a bit code... – Scheff's Cat Jul 30 '19 at 06:05
  • You will want to read carefully [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – David C. Rankin Jul 30 '19 at 06:40

2 Answers2

3

Your above code is a real mess and the absolute chaos. Read something about C and rewrite the code. I have created a simple example for you which handles the first part of your task. This example doesn´t contain any error handling and without the second part. You can add it at your own if you understand the function of the code and how to write C.

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

int main()
{
    char str[100];
    int i;

    printf("Enter the string:\n\r");
    scanf("%s", str);

    printf("Plese enter the case:\n\r");
    scanf("%d", &i);

    switch(i)
    {
        // Upper case
        case 1:
        {
            // Loop over each char
            for(i = 0; str[i] != 0; i++)
            {
                // Replace the lower case chars
                if((str[i] >= 'a') && (str[i] <= 'z'))
                {
                    str[i] = toupper(str[i]);
                }
            }

            break;
        }
        // Lower case
        case 2:
        {
            // Your task
            break;
        }
    }

    printf("%s\n\r", str);

    return 0; 
}
Kampi
  • 1,798
  • 18
  • 26
  • 1
    Using magic numbers makes it more unclear. Would be helpful to use character literals instead – Sami Kuhmonen Jul 30 '19 at 06:28
  • 2
    Consider using `tolower` and `toupper` from `ctype.h`: not everyone is using ASCII. – Pim Jul 30 '19 at 06:35
  • @Pim I do agree in general but do you know a system that is using non-ASCII and still alive? (I mean, outside of a museum.) ;-) – Scheff's Cat Jul 30 '19 at 06:39
  • 1
    @Scheff I am unaware of any system not using at least ASCII for 'simple' characters, but all locale-specific characters fall outside ASCII, although they migth want to be capitalized in this case. – Pim Jul 30 '19 at 06:45
  • 2
    @Scheff N1548 (C11 draft) tells us the following about the `tolower` return value: _If the argument is a character for which isupper is true and there are one or more corresponding characters, as specified by the current locale, for which islower is true, the tolower function returns one of the corresponding characters (always the same one for any giv en locale); otherwise, the argument is returned unchanged._, so the locale should be taken care of. Maybe the locale is not installed on that server. `tolower_l` just specifies the locale to use on function call. – Pim Jul 30 '19 at 07:00
  • @Pim I found my failure. There's a reason why it is called encoding hell... :-( And yes, the locale seems to be missing... [**Live Demo on coliru**](http://coliru.stacked-crooked.com/a/2155fe1cecdb3b17) – Scheff's Cat Jul 30 '19 at 07:01
1

Your Logic seems to be correct however, your syntax is apparently wrong at some places. The following code is your code with correct syntax. Compare this with the code that you have posted. :)

#include<stdio.h>
int main()
{
//Declaring variables
char str[100];
int i;
int cas;

//Taking string input
printf("Enter the string: \n");
scanf("%s",str);
printf("Enter Option 1 for Uppercase and Option 2 for Lowercase"); 
scanf("%d",&cas);

//Using Switch Case 
switch(cas)
{       
    case 1:
        for(i = 0; str[i]!='\0' ; i++)
        if(str[i] >= 'a' && str[i] <= 'z')
            str[i] = str[i] - 32;
        printf("\n The string's upper case = %s \n", str);
    break;
    case 2:
        for(i = 0; str[i] != '\0' ; i++)
        if(str[i] >= 'A' && str[i] <= 'Z')
        str[i] = str[i] + 32;
        printf("\n The string's lower case = %s \n", str);
    break;
    default: printf("Choice other than 1 and 2");    
}
return 0;
}
shivamag00
  • 661
  • 8
  • 13