| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1900 人关注过本帖, 1 人收藏
标题:求一种去掉字符串中空格以及无效字符的程序
只看楼主 加入收藏
薇儿2333
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2018-4-19
结帖率:66.67%
收藏(1)
已结贴  问题点数:18 回复次数:2 
求一种去掉字符串中空格以及无效字符的程序
The input is two strings(A and B) and a integer(k).
You should write a function to accomplish the following functions.
1、Insert B into A, the integer k is the position of the B.
    Example: A is "World", B is "Hello", k=0, the result is "HelloWorld"
2、if k < 0 or k > strlen(A), print "illegal index"
3、The length of A is less then 2000, the length of B is less than 1000.
Sample Input:
world hello 0
Sample Output:
helloworld

Sample Input:
world hello 10
Sample Output:
illegal index

Sample Input:
helld oworl 4
Sample Output:
helloworld
搜索更多相关主题的帖子: The input World Hello Sample 
2018-04-21 23:43
九转星河
Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19Rank: 19
来 自:长长久久
等 级:贵宾
威 望:52
帖 子:5023
专家分:14003
注 册:2016-10-22
收藏
得分:18 
可以看看这个,输入字符串去掉空格等无效字符可以这样简单处理(不过要注意缓冲区溢出问题,其实正常来说应该是用fgets从缓冲区里面来读取字符的)~

程序代码:
#include<stdio.h>

void fun( char*,const char*,size_t );

int main( void )
{
    char A[2000];
    char B[1000];
    
    int index;
    
    scanf("%s%s%d",A,B,&index);
    
    fun(A,B,index);
    
    return 0;
}

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

void fun( char* A,const char* B,int len )
{
    assert(A);
    assert(B);
    
    {
        const unsigned a_len=strlen(A);
        const unsigned b_len=strlen(B);
    
        if (len<0||len>a_len)
        {
            puts("illegal index");
            
            return ;
        }
        
        memmove(A+len+b_len,A+len,a_len-len+1);
        memmove(A+len,B,b_len);
    }
   
   puts(A);
}


[此贴子已经被作者于2018-4-23 23:37编辑过]


[code]/*~个性签名:bug是什么意思?bug是看上去没有可能的东西实际上是有可能做到的 就是这样~2018-08-08更~*/[/code]
2018-04-22 00:33
薇儿2333
Rank: 1
等 级:新手上路
帖 子:11
专家分:0
注 册:2018-4-19
收藏
得分:0 
其余同学请绕行,谢谢
2018-04-23 16:51
快速回复:求一种去掉字符串中空格以及无效字符的程序
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017456 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved