简体   繁体   English

在C中使用函数将字符串转换为大小写

[英]Use functions to convert string to upper and lowercase in C

This is homework and this is the task I was given:这是家庭作业,这是我被分配的任务:
Prints the string in uppercase and lowercase以大写和小写形式打印字符串
Splits the string on the middle and prints the two parts with " - " between Requirements:拆分中间的字符串并在要求之间打印带有“ - ”的两个部分:

Converting the string to upper and lower case must be done in functions.必须在函数中将字符串转换为大写和小写。 These functions must return nothing (void) and be called: string_upper, string_lower.这些函数必须不返回任何内容 (void) 并被调用:string_upper、string_lower。
Do not use strlwr or strupr.不要使用 strlwr 或 strupr。

Note: The string length is always even.注意:字符串长度总是偶数。

Expected output (with the string you receive as input):预期 output (您收到的字符串作为输入):

The string in uppercase is 'ABCDEFGH'大写的字符串是'ABCDEFGH'
The string in lowercase is 'abcdefgh'小写的字符串是'abcdefgh'
The string split in two is 'abcd - efgh'一分为二的字符串是'abcd - efgh'

I have managed to come up with something that works but it dosent use functions as requiered in the task.我设法想出了一些可行的方法,但它使用了任务中所需的功能。 How can you do this with funtions?你怎么能用函数做到这一点?

I have looked around but I cant find any examples of converting strings to upper and lowercase using functions我环顾四周,但找不到任何使用函数将字符串转换为大写和小写的示例

#include<stdio.h>
#include<string.h>

int main() {

    char inputString[100], leftHalf[100], rightHalf[100];
    int length, mid, i, k;

    /* Read input string from user using gets */
    printf("Enter a string\n");
    gets(inputString);
    /* Find length of string using strlen function */
    length = strlen(inputString);

    mid = length/2;
    /* Copy left half of inputString to leftHalf */
    for(i = 0; i < mid; i++) {
        leftHalf[i]= inputString[i];
    }
    leftHalf[i] = '\0';

    /* Copy right half of inputString to rightHalf  */
    for(i = mid, k = 0; i <= length; i++, k++) {
        rightHalf[k]= inputString[i];
    }

    for(i=0;i<=strlen(inputString);i++)
    {
        if(inputString[i]>=65&&inputString[i]<=90)
            inputString[i]=inputString[i]+32;
    }
    printf("String in Lowercase: %s\n",inputString);
    /* To print string in upperCase*/
    for(i=0;i<=strlen(inputString);i++)
    {
        if(inputString[i]>=97&&inputString[i]<=122)
            inputString[i]=inputString[i]-32;
    }
    printf("String in Uppercase: %s\n",inputString);


    /* Printing left and right half of string */
    //printf("Left half : %s\n",leftHalf);
    //printf("Right half : %s\n",rightHalf);
    printf("%s-%s",leftHalf, rightHalf);

    return 0;
}

To create a function, just remove the code (eg the for-loop) to a function.要创建 function,只需将代码(例如 for 循环)删除到 function。 Don't forget to declare auxiliary variables (eg int i).不要忘记声明辅助变量(例如 int i)。 Like so像这样

void string_upper (char *inputString)
{
    int i;
    for(i=0;i<=strlen(inputString);i++)
    {
        if(inputString[i]>=65&&inputString[i]<=90)
            inputString[i]=inputString[i]+32;
    }
}

And then in your main code, you call然后在你的主代码中,你调用

string_upper (inputString);

instead of the for-loop that was there.而不是那里的for循环。

Do the similar for string_lower.对 string_lower 执行类似操作。

You can just put your code to convert the string to upper and lower case in separate functions.你可以把你的代码放在单独的函数中将字符串转换为大写和小写。 like this.像这样。

void string_upper (char* str) {
    for(int i=0; i<=strlen(str); i++) {
        if(str[i]>=97 && str[i]<=122)
            str[i]=str[i]-32;
    }
}

void string_lower(char* str) {
    //...
}

int main() {

    char inputString[100], leftHalf[100], rightHalf[100];
    int length, mid, i, k;

    //...

    /* call function to convert string to upper-case*/
    string_upper(inputString);
    printf("String in Uppercase: %s\n",inputString);

    /* call function to convert string to lower-case */
    string_lower(inputString);
    printf("String in Lowercase: %s\n",inputString);

    //...

    return 0;
}

If you're allowed to use tolower() and toupper() , this works:如果您被允许使用tolower()toupper() ,则此方法有效:

#include <ctype.h>

// note unsigned char!
void string_upper( unsigned char *str )
{
    while ( *str )
    {
        *str = toupper( *str );
        str++;
    }
}

string_lower() is left as an exercise for the reader... ;-) string_lower()留给读者作为练习...... ;-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用C中的函数将大写字符串转换为小写的程序中的问题 - Problem in a program to convert upper case string to lowercase using function in C 大小写函数C - Upper to lowercase function C 将小写的字符串转换为大写? 在C中 - Convert string of lower case to upper case? In C 转换并计算大写和小写字符 - Convert and count upper case and lowercase characters 我正在写 C function 使用 ASCII 将小写字符转换为大写字符,但 Output 不正确 - I am writing C function that convert lowercase char to upper case char with using ASCII but Output is not correct How to convert a unicode string to upper/lower/title case using Python's C API avoiding functions deprecated in Python 3.3? - How to convert a unicode string to upper/lower/title case using Python's C API avoiding functions deprecated in Python 3.3? 将char *字符串转换为大写 - Convert char* string to upper 如何在C中将字符串(char *)转换为大写或小写 - How to convert a string (char *) to upper or lower case in C 编写Linux内存驱动程序以将小写转换为大写 - Writing a Linux memory driver to convert lowercase to upper case 如果编写一个可以将给定字符串中的Volwels转换为较低的C程序,如果它是较低的则为上部和上部? - How to write a C Program which can convert Volwels in the given string to lower if it were upper and upper if it were lower?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM