本文介绍了填补空间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

抱歉。我还有一个问题

我得到一个包含4个字符的字符串。我想把它放在一个26字符串的字符串中。怎么

我可以填充剩余字符的空间.. ??

是我需要做一个while循环来填充字符串的空间吗?


请帮忙!


谢谢

杰克

解决方案




不确定你想要什么,但是下面的程序可能很好

start。

#include< string.h>


int main(无效)

{

char small [] =" abc" ;; / *由4个字符组成的字符串* /

char large [26] ="" ;; / *字符串由26个字符组成* /


/ *将小字符串复制为大号,终止''\0''排除* /

memcpy(大,小,尺寸小 - 1);

/ *将剩余的字符设置为'' - ''* /

memset(大+ sizeof小) - 1,'' - '',sizeof large - sizeof small);

/ *使用''\0''终止大号以使其成为字符串* /

large [sizeof large - 1] = 0;


/ * large现在包含字符串abc ---------------- ------" * /


返回0;

}





#include< string.h>


/ * ... * /


char shorty [] =" FOO英寸; / *四个字符,包括终止* /

char冗长[26] = {0};


/ * ... * /


strcpy(冗长,短暂);


shorty [3]的字符串终止符将被复制到

lenthy字符串,终止它[3]。你不需要

填写带有任何东西的字符串。


如果你*想要填充字符串的其余部分(元素

,索引4到25),那么memset ()会为你做这个:


memset(&(冗长[4]),''''',21);

冗长[ 25] =''\''';


for-loop将做同样的事情:


int i;

for(i = 4; i< 25; ++ i){

longy [i] =''?'';

}

冗长[25] =''\ 0'';


字符串仍将在索引3处终止,除非

你用其他东西覆盖那个位置的''\ 0''。


-

Andreas K?h?ri



如何



不确定你想要什么,但以下程序可能是一个很好的开始。

#include< string.h>

int main(void)
{char small [] =" abc" ;; / *字符串由4个字符组成* /
char large [26] ="" ;; / *由26个字符组成的字符串* /

/ *复制字符串小到大,终止''\ 0''排除* /
memcpy(大,小,sizeof小 - 1);
/ *将剩余的字符设置为'' - ''* /
memset(大+ sizeof小 - 1,'' - '',sizeof large - sizeof small);
/ *用''\ 0''终止大,使其成为一个字符串* /
大[sizeof large - 1] = 0;

/ * large包含现在字符串abc ---------------------- * /

返回0;
}



sorry . i got one more problem
i got a string with 4 char. i want to put that in a string with 26 char. how
can i fill space on the remain char.. ??
is that i need to do a while loop do fill the space for the string?

please help!

thanks
Jack

解决方案



Not sure what you want exactly, but the following program might be a good
start.
#include <string.h>

int main(void)
{
char small[] = "abc"; /* string consisting of 4 characters */
char large[26] = ""; /* string consisting of 26 characters */

/* copy the string small into large, the terminating ''\0'' excluded */
memcpy(large, small, sizeof small - 1);
/* set the remaining chararacters in large to ''-'' */
memset(large + sizeof small - 1, ''-'', sizeof large - sizeof small);
/* terminate large with a ''\0'' to make it a string */
large[sizeof large - 1] = 0;

/* large contains now the string "abc----------------------" */

return 0;
}




#include <string.h>

/* ... */

char shorty[] = "foo"; /* four chars, including termination */
char lengthy[26] = { 0 };

/* ... */

strcpy(lengthy, shorty);

The string terminator at shorty[3] will be copied over to the
lenthy string, terminating it at lengthy[3]. You don''t need to
"fill out" the string with anything.

If you *do* want to fill the remainder of the string (elements
with index 4 through to 25), then memset() will do this for you:

memset(&(lengthy[4]), ''?'', 21);
lengthy[25] = ''\0'';

A for-loop will do the same thing:

int i;
for (i = 4; i < 25; ++i) {
lengthy[i] = ''?'';
}
lengthy[25] = ''\0'';

The string will still be terminated at index 3 though, unless
you overwrite the ''\0'' at that position with something else.

--
Andreas K?h?ri



how



Not sure what you want exactly, but the following program might be a good
start.
#include <string.h>

int main(void)
{
char small[] = "abc"; /* string consisting of 4 characters */
char large[26] = ""; /* string consisting of 26 characters */

/* copy the string small into large, the terminating ''\0'' excluded */
memcpy(large, small, sizeof small - 1);
/* set the remaining chararacters in large to ''-'' */
memset(large + sizeof small - 1, ''-'', sizeof large - sizeof small);
/* terminate large with a ''\0'' to make it a string */
large[sizeof large - 1] = 0;

/* large contains now the string "abc----------------------" */

return 0;
}



这篇关于填补空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-16 07:28