根据c中的大字母和小写字母拆分字符串(单词)

时间:2016-03-18 15:40:38

标签: c string

我需要创建一个带有大写和小写字母的字符串的程序,程序将所有低位字母和高位字母连接起来并将它们连接到一个单词。 例如 : str =' SHaddOW' upperLetters将 - ' SHOW' lowerLetters将 - '添加'

#include <stdio.h>
#include <string.h>
#define LEN 8

void newStrings(char firstString[LEN]);

int main()
{
    char str[] = "SHaddOW";
    char smallStr[LEN], bigStr[LEN]; // Fill in the array lengths
    strncpy(smallStr , str, LEN);
    strncpy(bigStr, str, LEN);
    int i = 0;
    newStrings(str);
    for (i = 0; i < LEN-1; i++)
    {
        if (smallStr[i] < 'a' || smallStr[i] > 'z')
        {
            smallStr[i] += 32;
        }

        if (bigStr[i] < 'A' || bigStr[i] > 'Z')
        {
            bigStr[i] -= 32;
        }

    }

    puts(smallStr);
    puts(bigStr);
    system("pause");
    return 0;
}

void newStrings(char str[LEN])
{
    int i = 0, bigLength = 0, smallLength = 0 ;
    char bigLetters[LEN] , smallLetters[LEN];
    for (i = 0; i < LEN-1; i++)
    {
        if (str[i] >= 'A' || str <= 'Z')
        {
            bigLetters[bigLength] = str[i];
            bigLength++;
        }
        else{
            smallLetters[smallLength] = str[i];
            smallLength++;
        }
    }
    bigLetters[bigLength] = '\0';
    smallLetters[smallLength] = '\0';
    printf("\n\n%s\t\t", bigLetters);
    printf("%s\n\n\n\n", smallLetters);
} 

我的代码有一些问题而且很长,所以我想找到另一种方式,因为我的方式不起作用。 打印:

SHaddOW


shaddow
SHADDOW

3 个答案:

答案 0 :(得分:1)

  1. 您应该有大小写字母的单独计数器,而不是共享lengthNewString

  2. 您需要使用字符'\0'终止字符串:在打印之前,请执行:

    bigLetters[bigLength] = '\0';
    smallLetters[smallLength] = '\0';
    
  3. 条件应为

    if (str[i] >= 'A' && str[i] <= 'Z')
    
  4. 您错过了#include <stdlib.h>

答案 1 :(得分:1)

#include <ctype.h>
void splitstring(const char *src, char *up, char *lo, char *xx) {
    /* assume up, lo, and xx point to large enough non-overlapping memory areas */
    while (*src) {
             if (isupper((unsigned char)*src)) *up++ = *src;
        else if (islower((unsigned char)*src)) *lo++ = *src;
        else                                   *xx++ = *src;
        src++;
    }
    *up = *lo = *xx = 0; /* terminate strings */
}

并使用这样的功能

char bigStr[1000], smallStr[1000], noletter[1000];
splitstring("SHaddOW", bigStr, smallStr, noletter);
splitstring("The quick brown fox #$@!&&!&@#%# --nocarrier", bigStr, smallStr, noletter);

答案 2 :(得分:0)

你可以试试这个:

#include <stdio.h>

#define MAX_LEN 8 //max length of a string including null byte

char bigstr[MAX_LEN];  // strings for upper and lower case chars
char smallstr[MAX_LEN];

void parse(char * word){
  char sc = 0; //index for smallstr
  char bc = 0; //index for bigstr

  while (*word){  // for every char in the word, till the null char
    if (('a' <= *word) && ('z' >= *word) && (sc < MAX_LEN - 1)) // check if its lowercase and if there's space left in 'smallstr'
      smallstr[sc++] = *word;  // add lower case char to 'smallstr'
    else if (('A' <= *word) && ('Z' >= *word) && (bc < MAX_LEN - 1))  // check if its uppercase and if there's space left in 'bigstr'
      bigstr[bc++] = *word;  // add upper case char to 'bigstr'
    word++;  // advance to the next char
  }
  smallstr[sc] = 0;  //null-terminate each string
  bigstr[bc] = 0;
}

int main(){
    char s[] = "SHaddOW";
    parse(s);  // parse the string
    printf("%s\n", bigstr); 
    printf("%s\n", smallstr);

    return 0;
}

但是只考虑了字母。如果达到特定字符串的(MAX_LEN - 1),则丢弃该字符串的任何新字符。