C语言怎样删除字符串中的空白字符

如题所述

#include <stdio.h>

int main()

{

char *p="I am Chinese";

char c;

int i = 0;

while((c = p[i++])!='\0'){

if(c!=' ')

putchar(c);

}

}

扩展资料:

字符串的函数应用

1. 连接运算 concat(s1,s2,s3…sn) 相当于s1+s2+s3+…+sn。

例:concat('11','aa')='11aa';

2. 求子串。 Copy(s,I,I) 从字符串s中截取第I个字符开始后的长度为l的子串。

例:copy(‘abdag',2,3)='bda'

3. 删除子串。过程 Delete(s,I,l) 从字符串s中删除第I个字符开始后的长度为l的子串。

例:s:='abcde';delete(s,2,3);结果s:='ae'

4. 插入子串。 过程Insert(s1,s2,I) 把s1插入到s2的第I个位置。

例:s:=abc;insert('12',s,2);结果s:='a12bc'

5. 求字符串长度 length(s) 例:length('12abc')=5。

在ASP中 求字符串长度用 len(s)例: len("abc12")=5

6. 搜索子串的位置 pos(s1,s2) 如果s1是s2的子串 ,则返回s1的第一个字符在s2中的位置,若不是子串,则返回0。

例:pos(‘ab','12abcd')=3

参考资料:

百度百科-字符串

温馨提示:答案为网友推荐,仅供参考
第1个回答  2017-06-19
printf("love");printf("\b ");转义符\b是退格,但是并不删除,因此可以再退格后输出一个空格覆盖最后的e。注意\b是不能退到上一行的,最多退到行首。
第2个回答  2013-12-11
很高兴能回答你的提问`我是简单の快乐。 #include<conio.h>
#include<stdio.h>
void delspace(char * p)
{
int i,j=0;
for ( i = 0;p[i]!='\0';i ++ ) {
if(p[i] != ' ')
p[j++] = p[i];
}
p[j] = '\0';
}
void main()
{
char s[100];
printf("Please input the string!\n");
gets(s);
delspace(s);
puts(s);
getch();
}谢谢采纳 不懂得可以追问本回答被网友采纳
第3个回答  2017-10-11
/* 利用指针 */

#include <stdio.h>
#define SIZE 50

void del_space(const char *str, char *dst)
{
while (*str) // *str != '\0';
{
if (*str == ' ')
str++;

else
*dst++ = *str++; // 将不为空的*str依次赋值给*dst
}
}

int main(int argc, char *argv[])
{
char str[SIZE];
printf("Input:\n");
gets(str);

char dst[SIZE];
del_space(str, dst);
printf("%s\n", dst);

return 0;
}
第4个回答  2017-10-12
#include <stdio.h>
#include <string.h>
void main()
{
int i,j,n;char str[80];
gets(str);n=strlen(str);
for(i=0;i<n;i++)
{
if(str[i]==' ')
{
for(j=i;j<n-1;j++)
str[j]=str[j+1];
str[n-1]='\0';
n--;
}
}
printf("%s",str);
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网