Ste*_*n R -1 c loops substring
我必须使用C,而不是C++制作金字塔.我要做的就是这个,我有一个字符串"这是一个字符串"(是的,假设空间在那里),我必须从两边"删除"一个字母,并打印出来.像这样
"this is a string "
"his is a string"
"is a strin"
"s a stri"
" a str"
Run Code Online (Sandbox Code Playgroud)
重复此过程,直到没有更多字符.我的老师说要使用子串,C中是否有任何说法
从位置打印(索引0到索引x)
从位置打印(索引1到索引x-1)
我知道我必须使用for循环.
这是家庭作业,所以我会让你开始.
#include <stdio.h>
#include <string.h>
int main(void) {
char src[] = "this is a test string";
char dest[5] = {0}; // 4 chars + terminator
for (int i = 0; i * 4 < strlen(src); i++) {
strncpy(dest, src+(i*4), 4);
puts(dest);
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
this
is
a te
st s
trin
g
Run Code Online (Sandbox Code Playgroud)
因此,对于您的金字塔问题,您将需要获取原始字符串的子字符串.将子字符串的开头设置为原始字符串前面的一个字符,以及strlen(original) - 1结束字符.然后循环该过程.
| 归档时间: |
|
| 查看次数: |
289 次 |
| 最近记录: |