i had try to print the following pattern but i only able to decrease the letters from the one side but not from both the side
Please someone help me with this : to print this pattern
this is the code that i use:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Programming";
int len = strlen(str);
for (int i = 0; i < len; i++) {
for (int j = i; j<len; j++) {
printf("%c", str[j]);
}
printf("\n");
}
return 0;
}
Using pointer arithmetic, you can advance the start position of the printing, while specifying a length to printf()
can be helpful, if you don't want to modify the string (in this case, we shorten the length by two, while advancing the pointer one position:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
/* for each command line parameter passed to the program */
for (int i = 1; i < argc; i++) {
/* use a copy of the argv[i] pointer */
char *s = argv[i];
/* ... and print */
for (int l = strlen(s); l > 0; l -= 2) {
printf("%.*s\n", l, s++);
}
}
}
In the case you want a minimum code:
#include <stdio.h>
#include <string.h>
int main()
{
const char *s = "Programming";
for (int l = strlen(s); l > 0; l -= 2)
printf("%.*s\n", l, s++);
}
or a pattern_print routine:
void print_pattern(const char *s)
{
for (int l = strlen(s); l > 0; l -= 2)
printf("%.*s\n", l, s++);
}