这是其中一项任务,我想知道为什么没有基本的C99功能 名称为safeafe(char * in)的地方就是这样做的。这样会很好 在一个完美的世界里,对于用UTF-8希伯来语或 也许希腊但是假设我被七位ascii卡住了然后我试过了 以下内容:
/* We may even go so far as to ensure that any apostrophe, or hyphen
* or period may only appear as single entities and not as two or three
* in a row. This does not, however, protect us from total non-sense
* such as D'Archy Mc'Clo.u. d.
*
* walk the first_name string and throw away everything that is
* not in the range A-Z or a-z, with the exception of space char which
* we keep. Also a single hyphen is allowed.
*
* This all seems smart but we are not protected from stupidity such
* As a name with spaces and dashes or hypens intermixed with letters
* or from the artist formally known as 'Prince'.
*/
char buffer[256];
j = 0;
for ( k=0; k<strlen(first_name); k++ ) {
/* Accept anything in the a - z letters */
if ( ( first_name[k] >= 'a' ) && ( first_name[k] <= 'z' ) )
buffer[j++] = first_name[k];
/* Accept anything in the A - Z letters */
if ( ( first_name[k] >= 'A' ) && ( first_name[k] <= 'Z' ) )
buffer[j++] = first_name[k];
/* reduce double dashes or hyphens to a single hyphen */
while (( first_name[k] == '-' ) && ( first_name[k+1] == '-' ))
k++;
if ( first_name[k] == '-' ) /* do I need this ? */
buffer[j++] = first_name[k];
/* reduce double spaces to a single space */
while (( first_name[k] == ' ' ) && ( first_name[k+1] == ' ' ))
k++;
if ( first_name[k] == ' ' ) /* do I also need this ? */
buffer[j++] = first_name[k];
}
/* we may still yet have terminating spaces or hyphens on buffer */
while ( ( j > 1 ) && (( buffer[j-1] == ' ' ) || ( buffer[j-1] == '-' )) )
j--;
buffer[j] = '\0';
/* Accept this new cleaner First Name */
strcpy ( first_name, buffer );
只要输入名称缓冲区不超过255,似乎工作得很好 chars的长度。然而,在第一关,我想知道如何摆脱领先的空间 和破折号和连字符以及可能的撇号混合的噪音?
所以问题是......如何使这更好,而且,我是否需要这些线 在哪里我问(first_name [k] ==' - ')和空格相同吗?我已经做了 走过缓冲区寻找重复,并应该落在连字符上 或单个空间。对?
答案 0 :(得分:1)
纯粹将其视为如何清理代码的抽象编程问题,您可以使用isalpha()
来确保缓冲区仅包含字母,单个连字符和单个空格的字母:
for (k = 0; k < strlen(first_name); k++) {
if (isalpha (first_name[k])
buffer [j++] = first_name[k++];
else if (( first_name[k] == '-' ) && (isalpha (first_name [k+1])))
buffer [j++] = first_name[k++];
else if (( first_name[k] == ' ' ) && (isalpha (first_name [k+1])))
buffer [j++] = first_name[k++];
else
k++;
}
这只是一个草案。我实际上没有试过这个,所以没有保证。此外,这不会处理像“约翰 - 保罗”这样的名字在连字符之前和之后用空格写出的情况;你最终会得到一个空格而不是一个连字符。如果你想捕获这样的边缘情况,你可以输入几个额外的“else”子句。
那就是说,作为一个具体的现实世界的解决方案,我同意将名称完全视为输入处理更好。我自己有一个不同寻常的名字,我厌倦了向人们解释是的,这真的是我的名字,不,你不能改变它以适应你可接受的名字的想法。