-1

I have to make a pyramid only using C, no C++. what I have to do is this, I have a string "this is a string " (yes the space is suppose to be there), and I have to "remove" one letter from both sides, and print that. like this

"this is a string "
 "his is a string"
  "is a strin"
   "s a stri"
    " a str"

This is repeated until there are no more characters. My teacher says to use substrings, is there any thing in C that says

print from location (index 0 to index x)
print from location (index 1 to index x-1)

I know I have to use a for loop.

syb0rg
  • 8,057
  • 9
  • 41
  • 81
Steven R
  • 323
  • 2
  • 6
  • 18
  • Welcome to SO. Please read [ask] and [help] on how to ask a question. What have you tried so far? Nobody here is going to hold your hand and write all your code for you. – OldProgrammer Apr 01 '14 at 01:22
  • 2
    @OldProgrammer It's very much a beginner-level question, but nowhere is he asking for the code to be written for him. – 200_success Apr 01 '14 at 01:47
  • @OldProgrammer Yea i wasnt asking for the code, shouldve put that in the question :( but I got as far as writing a for loop down lol. been banging my head since 2pm today – Steven R Apr 01 '14 at 01:50
  • 1
    Why not replace characters at each with space. Then you just print the line again. Then you don't have to worry about ligning it up each time. – Martin York Apr 01 '14 at 03:40
  • @LokiAstari i would if i could :P i looked everywhere and cant figure out how to do it. :( – Steven R Apr 01 '14 at 03:53

3 Answers3

5

This is homework, so I'll just get you started.

#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);
    }
}

Output:

this
 is 
a te
st s
trin
g

So for your pyramid problem, you will want to take the substring of the your original string. Set the beginning of your substring one character ahead of your original string, and strlen(original) - 1 for the end character. Then loop that process.

syb0rg
  • 8,057
  • 9
  • 41
  • 81
1

Yes, there is a printing facility enabling you to print substrings. Read the spec for printf.

There is a way to give it a parameter (int) which limits how many characters from another parameter (char*) it prints.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
1

To chop off the first character of a string, simply treat the next character as the start of the string. So, instead of puts(str), call puts(str + 1).

To chop off the last character of a string, overwrite the last character with a \0 character. Since this is a destructive operation, it is probably a good idea to operate a copy of the original string.

To solve this problem, then, make a copy of the string. Keep two pointers: one at the front of the copied string walking towards the end, and one at the end walking towards the front.

200_success
  • 7,286
  • 1
  • 43
  • 74
  • I just started learning C like yesterday, how will i go about overwriting the last character with \0 ? and i already have a copy of the string, so im good to destroy it :) – Steven R Apr 01 '14 at 01:48
  • 2
    `str[strlen(str) - 1] = '\0'` – 200_success Apr 01 '14 at 01:49
  • hmph, im getting a segmentation fault(core dumped) error – Steven R Apr 01 '14 at 02:00
  • The technique is sound, I assure you. Start with small steps: 1) Print the whole string. 2) Print a copy of the string. 3) Print it without the first character. 4) Print it without the last character. 5) Write the pyramid loop. – 200_success Apr 01 '14 at 02:03
  • im getting the segmentation error when doing the overwrite method, I got up to removing the first letter. – Steven R Apr 01 '14 at 02:20
  • Would you like to post your code? I suggest a separate question, referencing this one, since your question is now about debugging a segfault. – 200_success Apr 01 '14 at 02:33
  • hm, dont know how without it going all into 2 lines, ill just ask another question. – Steven R Apr 01 '14 at 02:41
  • @StevenR: Overwriting only works if the string is writable. Generally not the case with string-literals. – Deduplicator Aug 24 '14 at 00:03