从字符串中删除空格和特殊字符
如何从字符串中删除空格和特殊字符?从字符串中删除空格和特殊字符
我在Google上搜索时找不到一个答案。有很多与其他语言有关,但不是C.大多数人提到了使用正则表达式,这不是C标准(?)。
卸下易空间很简单:
char str[50] = "Remove The Spaces!!";
然后用if语句的简单循环:
if (str[i] != ' ');
输出将是:
RemoveTheSpaces!!
我该怎么加到if语句以便识别特殊字符并将其删除?
我的特殊字符的定义:
Characters not included in this list:
A-Z a-z 0-9
这是可能不是实现这一目标的最有效方式,但它可以相当快地完成工作。
注意:此代码确实需要您包括<string.h>
和<ctype.h>
char str[50] = "Remove The Spaces!!";
char strStripped[50];
int i = 0, c = 0; /*I'm assuming you're not using C99+*/
for(; i < strlen(str); i++)
{
if (isalnum(str[i]))
{
strStripped[c] = str[i];
c++;
}
}
strStripped[c] = '\0';
你忘了strStrippped的NUL终止:'''strStripped [c] ='\ 0';''在循环之后。 – 2013-03-16 01:41:19
噢,是的,谢谢:) – 2013-03-16 01:41:49
如果你假设C99之前,那么'/ /'风格的评论也不支持。 – 2013-03-16 01:43:58
这是ASCII代码范围内
Char:Dec
0:48, 9:57
A:65, Z:90
a:97, z:122
试试这个:
char str[50] = "Remove The Spaces!!";
int i =0;
for(; i<strlen(str); i++)
{
if(str[i]>=48 && str[i]<=57 || str[i]>=65 && str[i]<=90 || str[i]>=97 && str[i]<=122)
//This is equivalent to
//if(str[i]>='0' && str[i]<='9' || str[i]>='A' && str[i]<='Z' || str[i]>='a' && str[i]<='z')
printf("alphaNumeric:%c\n", str[i]);
else
{
printf("special:%c\n", str[i]);
//remove that
}
}
使用您的if语句:
if (str[i] != ' ');
随着一点点的逻辑(字符必须是在范围az或AZ或0-9:
If (!('a' <= str[i] && 'z' >= str[i]) &&
!('A' <= str[i] && 'Z' >= str[i]) &&
!('0' <= str[i] && '9' >= str[i])) then ignore character.
你知道你可以通过删除'!'来简化逻辑,只需用'||'替换'&&'。你已经否定了这个表达:) – Jite 2013-03-16 01:47:24
这是真的大声笑...我只是用一种很自然的方式写它来理解它。出于某种原因,我喜欢并且比我更好......我可能只是很奇怪。 – 2013-03-16 02:15:50
这只是一个愚蠢的建议。
char ordinary[CHAR_MAX] = {
['A']=1,['B']=1,['C']=1,['D']=1,['E']=1,['F']=1,['G']=1,['H']=1,['I']=1,
['J']=1,['K']=1,['L']=1,['M']=1,['N']=1,['O']=1,['P']=1,['Q']=1,['R']=1,
['S']=1,['T']=1,['U']=1,['V']=1,['W']=1,['X']=1,['Y']=1,['Z']=1,
['a']=1,['b']=1,['c']=1,['d']=1,['e']=1,['f']=1,['g']=1,['h']=1,['i']=1,
['j']=1,['k']=1,['l']=1,['m']=1,['n']=1,['o']=1,['p']=1,['q']=1,['r']=1,
['s']=1,['t']=1,['u']=1,['v']=1,['w']=1,['x']=1,['y']=1,['z']=1,
['0']=1,['1']=1,['2']=1,['3']=1,['4']=1,['5']=1,['6']=1,['7']=1,['8']=1,
['9']=1,
};
int is_special (int c) {
if (c < 0) return 1;
if (c >= CHAR_MAX) return 1;
return !ordinary[c];
}
void remove_spaces_and_specials_in_place (char *str) {
if (str) {
char *p = str;
for (; *str; ++str) {
if (!is_special(*str)) *p++ = *str;
}
*p = '\0';
}
}
使用C99指定的初始值设定项。 – 2013-03-16 02:24:44
有数百万种不同的方式可以完成。这里只是一个不使用任何额外的存储空间,并执行“就地”去除不需要的字符例如:
#include <stdlib.h>
#include <stdio.h>
#include <ctype.h>
static void my_strip(char *data)
{
unsigned long i = 0; /* Scanning index */
unsigned long x = 0; /* Write back index */
char c;
/*
* Store every next character in `c` and make sure it is not '\0'
* because '\0' indicates the end of string, and we don't want
* to read past the end not to trigger undefined behavior.
* Then increment "scanning" index so that next time we read the
* next character.
*/
while ((c = data[i++]) != '\0') {
/* Check if character is either alphabetic or numeric. */
if (isalnum(c)) {
/*
* OK, this is what we need. Write it back.
* Note that `x` will always be either the same as `i`
* or less. After writing, increment `x` so that next
* time we do not overwrite the previous result.
*/
data[x++] = c;
}
/* else — this is something we don't need — so we don't increment the
`x` while `i` is incremented. */
}
/* After all is done, ensure we terminate the string with '\0'. */
data[x] = '\0';
}
int main()
{
/* This is array we will be operating on. */
char data[512];
/* Ask your customer for a string. */
printf("Please enter a string: ");
if (fgets(data, sizeof(data), stdin) == NULL) {
/* Something unexpected happened. */
return EXIT_FAILURE;
}
/* Show the customer what we read (just in case :-)) */
printf("You have entered: %s", data);
/*
* Call the magic function that removes everything and leaves
* only alphabetic and numberic characters.
*/
my_strip(data);
/*
* Print the end result. Note that newline (\n) is there
* when we read the string
*/
printf("Stripped string: %s\n", data);
/* Our job is done! */
return EXIT_SUCCESS;
}
我投入了大量的意见在里面,所以希望该代码不需要解释。希望能帮助到你。祝你好运!
#include <stdio.h>
#include <string.h>
main()
{
int i=0, j=0;
char c;
char buff[255] = "Remove The Spaces!!";
for(; c=buff[i]=buff[j]; j++){
if(c>='A' && c<='Z' || c>='a' && c<='z' || c>='0' && c<='9'){
i++;
}
}
printf("char buff[255] = \"%s\"\n", buff);
}
只是一个建议......通过向代码添加注释并可能显示输出,可以改进此答案。 – 2014-05-19 18:57:52
现在我看到,如果行中有多个特殊字符,代码会将它们留在结果字符串中,并始终是该序列中每对两个中的第二个。由于指令“buff [i] = buff [++ j];”那里有一个错误,因为它不假定在两行或更多行中可能有特殊字符。而且变量“i”只有在源的“j”索引中的字符有效时才应增加,而不是一直增加。 – 2014-05-19 19:17:07
所以要更正代码: 1 - 取出else中的指令,即只让“if”; 2 - 在每次迭代结束时(仅限“j”),不要增加“i”。 3 - 在指令“buff [i] = buff [j];”之后增加“if”中的“i”;或者用“buff [i ++] = buff [j];”替换这个intruction。 结果将会是一个代码,与我编写Jonathan Leffler编写的代码时编写的代码很相似,只不过在那一个中,我忘了在“if”的条件中包含源字符串终止符以便复制终止符作为结果字符串的有效字符。 – 2014-05-19 19:22:14
C中的字符串处理并不总是很有趣。把字符串看作只是一个“char”数组。你可以用'b'替换'a',但是没有简单的方法从数组中删除字符索引,所以你最终还是会有一个漏洞。虽然,如果仅用于打印,您可以迭代数组,如果它不在a-zA-Z0-9的'ascii values'范围内,则跳过任何操作并转到下一个字符。在可能的情况下,这往往是最简单的事情。否则,你需要复制到一个新的缓冲区。 – Jite 2013-03-16 01:40:32