在C中制作金字塔

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循环.

syb*_*0rg 5

这是家庭作业,所以我会让你开始.

#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结束字符.然后循环该过程.

  • 哦,我的天哪,我想我可以搞清楚!非常感谢!!!!!我只需要朝着正确的方向迈进 (3认同)