将字符串更改为Pascal大小写

时间:2016-10-07 05:29:49

标签: c

处理一些代码。我是c的初学者,所以我可能不会理解超级复杂的语法。正如问题所述,我从用户那里读取了一个字符串。 "猫狗"并且程序将其更改为pascal case。 "猫狗"如您所见,每个单词的首字母大写,并删除空格。这是我遇到麻烦的地方,我无法弄清楚如何删除空格。我想放入一个临时数组,但由于范围问题,我无法返回新的字符串数组。提前致谢。此外,我必须留在该功能内,不能创建新的。

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

char toUpperCase(char ch){ //changes char to uppercase
return ch - 'a'+ 'A';
}

char toLowerCase(char ch){//changes char to lower case
return ch -'A'+'a';
}


void PascalCase(char* word){//"cat dog" "CatDog"
/*Convert to Pascal case
 It is safe to assume that the string is terminated by '\0'*/
char temp[100];//do not know how to implement
int i;
if (word[0] >= 97 && word[0] <= 122) {
    word[0] = toUpperCase(word[0]);
}
    for (i = 1; i < strlen(word); ++i) {
        if (word[i] >= 65 && word[i] <= 90) {
            word[i] = toLowerCase(word[i]);
        }
        if (word[i] == ' '){
            ++i;
            if (word[i] >= 97 && word[i] <= 122) {
                word[i] = toUpperCase(word[i]);
            }
        }
    }

}



int main(){
 char word[100]; 
 printf("Enter phrase:");
 fgets(word, 100, stdin);

 /*Call PascalCase*/
 PascalCase(word);

 /*Print new word*/
 printf("%s\n", word);
 return 0;
}

4 个答案:

答案 0 :(得分:0)

您可以尝试下面的代码:

  inline char toUpperCase(char c){
    if('a'<=c && c<='z') return c-'a'+'A';
    else return c;
  }
  inline char toLowerCase(char c){
    if('A'<=c && c<='Z') return c-'A'+'a';
    else return c;
  }
  void toPascalCase(char *str){
    int i,j=0; bool first=true;
    for(i=0;str[i];i++){
      if(str[i]==' ') {first=true; continue;}
      if(first) {str[i]=toUpperCase(str[i]); first=false;}
      else str[i]=toLowerCase(str[i]);
      str[j++]=str[i];
    }
    str[j]='\0';
  }

由于删除空格不会增加字符串长度,因此可以在适当的位置进行操作。此外,我将案例检查移到toUpperCase函数中,因此使用起来更方便。使其内联将实现更快的实现。我尝试了不同的输入,例如&#34;猫狗&#34;,或&#34;猫狗&#34;并且代码总是给你&#34; CatDog&#34; (帕斯卡尔案)。布尔变量first指示当前字符是否是空格后的第一个字符(应该大写的单词的开头)。

答案 1 :(得分:0)

这是一种利用现有功能的方法。不知道&#34;我必须留在这个功能中,不能创建新的&#34;应该意味着,希望它不会使方法无效。选择的字符串应该是诙谐的。

编辑:正如cdlane所指出的那样,strlwr是一个非标准函数,可能无法使用。我已经添加了另一种选择。

#include <stdio.h>
#include <stdlib.h>

void str2lower(char *input)
{
    int i=0;
    while (input[i])
    {
        input[i] = tolower(input[i]);
        ++i;
    }
}

// returns a new string - caller responsible for freeing the memory
char *pascalCase(char *input)
{
    // create a copy of the string, since strtok modifies its input
    char *tmp = strdup(input);
    // any char in this sring will be used to split the input
    const char *delims = " ";

    // get some memory - same length as original string.
    // **this will be too much** we dont need memory for the sapces
    // that will be removed.
    int len = strlen(input);
    char *result = (char*)calloc(len, 1);

    // return a string that contains chars up to the first
    // char found in the delims string
    char *curWord = strtok(tmp,delims);
    while (curWord != NULL)
    {
        // make the whole word lower-case
        //strlwr(curWord);
        str2lower(curWord);

        // capitalize the first letter
        curWord[0] = toupper(curWord[0]);
        // tack it onto the end of our result
        strcat(result, curWord);
        // get the next word
        curWord = strtok(NULL, delims);
    }
    // dont need this anymore
    free(tmp);
    return result;
}

int main()
{
    // http://www.ee.ryerson.ca/~elf/hack/realmen.html
    char *result = pascalCase("real programmers don't use pascal");
    printf("%s\n", result);
    free(result);
}

答案 2 :(得分:0)

尝试处理所有规范并保持简单:

#include <string.h>
#include <ctype.h>

/* Convert to Pascal case */
/* It is safe to assume that the string is terminated by '\0' */

void PascalCase(char *string) { // "cat dog" -> "CatDog"

    char *pointer = string;

    while (*pointer != '\0') {
        if (pointer == string) { // first character in string
            *pointer = toupper(*pointer);
        } else {
            *pointer = tolower(*pointer); // character not beginning a word
        }

        while (*pointer == ' ') {
            (void) memmove(pointer, pointer + 1, strlen(pointer)); // remove space from string
            if ((*pointer = toupper(*pointer)) == '\0') { // capitalize if a letter replaces space
                return; // not documented if memmove() leaves original '\0' so CYA
            }
        }

        ++pointer;
    }
}

字符串中第一个字符的测试在循环内移动,以便在函数传递空字符串后'\0'测试后它也会掉落。

答案 3 :(得分:0)

使用第二个缓冲区会更容易,但这是另一种方法。 有两个指向缓冲区的指针然后遍历字符串。 isspace是标准运行时函数,用于查看字符是否为空格。

char word[] = "dog cat";

char* q = word;  // set pointer to buffer, use this for moving string forward
char* p = word;  // finding the non space characters

while (*p && *q)
{
  if (isspace(*p))  // skip any spaces
  {
    ++p;
  }
  else // find non-space, make first one capital then move it
  { 
    *p = toupper(*p); 
    while (!isspace(*p) && *p) *q++ = *p++;
  }
}

*q = '\0';

puts(word);