我一直试图找出如何使数组中的某些字符串字母全部以小写形式输入,并在下划线(即“ first_last”)输出时在它们之间以及每个开头字母之间加一个空格大写(即“第一倒数”)。
任何有用的想法都会受到赞赏
这是我到目前为止所拥有的:
#include <stdio.h>
#define SIZE 30
int main()
{
char string1(SIZE); //reserve a name of up to 29 characters
//read characters from input into array string 1
printf("%s", "Enter a name:);
scanf("%29s", string1);
}
答案 0 :(得分:0)
您可以按字符处理字符串并修改相关字符,例如:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Capitalise based on your specs.
static void cap(char *str) {
// No actions for empty string.
if (*str == '\0') return;
// Always upper first character.
str[0] = toupper(str[0]);
// Check all but last character (ignore trailing '_').
for (size_t i = 1, count = strlen(str) - 1; i < count; ++i) {
// If underscore, make space and upper next character.
if (str[i] == '_') {
str[i] = ' ';
str[i+1] = toupper(str[i+1]);
}
}
}
// Test driver to do all arguments.
int main(int argc, char *argv[]) {
for (int i = 1; i < argc; ++i) {
printf("'%s'", argv[i]);
cap(argv[i]);
printf("-> '%s'\n", argv[i]);
}
}
您可能要处理极端情况,但由于您尚未为它们指定所需的行为,因此我将其留给读者作为进一步的练习。要考虑的事情是:
multiple____underscores
处理字符串; _
; _
;和这里是一个示例运行,因此您可以查看实际运行情况:
pax$ testprog first_second one one_two
'first_second'-> 'First Second'
'one'-> 'One'
'one_two'-> 'One Two'