我想要一个解决方案。如果我按1并获取字符串的上部情况,并且当我按2时,如果使用开关条件或其他内容,则可以得到较低的情况。我刚刚开始编码并在此字段中进行新的。<<<<<<<<<<<<
我试图通过功能来做这件事,但是也许由于知识湖,id确实得到了结果。
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] != ' ' ; 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] != ' ' ; 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期望当我按1时,然后获取上限,当我按2时,我会在字符串中较低。
1
hello world
2
HELLO WORLD
我想使用开关来做。
您的上面代码是一个真正的混乱和绝对的混乱。阅读有关C的信息并重写代码。我为您创建了一个简单的示例,该示例可以处理您任务的第一部分。此示例不包含任何错误处理,而没有第二部分。如果您了解代码的功能以及如何编写c。
,可以自行添加它#include <stdio.h>
#include <ctype.h>
int main()
{
char str[100];
int i;
printf("Enter the string:nr");
scanf("%s", str);
printf("Plese enter the case:nr");
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("%snr", str);
return 0;
}
您的逻辑似乎是正确的,但是您的语法在某些地方显然是错误的。以下代码是带有正确语法的代码。将此与您发布的代码进行比较。:(
#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]!=' ' ; 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] != ' ' ; 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;
}