Click here to Skip to main content
15,884,720 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
This is how I've written as just doing a %20s and string having more the lenghth the less width there is so. Sorry I'm not familiarized with fgets so Im doing this in hopes string hass less than 100 characters. How else to solve this?

What I have tried:

//Shifting the string by 20
#include<stdio.h>
int main(){
	char str[100];
	char blank;
	printf ("Enter a string\n");
	gets(str);
	printf ("\n%19c%s", blank, str);
	printf ("\nYour string was shifted to right by width of 20");
	return 0;
}
Posted
Updated 15-May-22 22:38pm
Comments
Patrice T 16-May-22 4:22am    
And you have a question ?

Just use the printf formatting:
#include <stdio.h>

int main()
{
    char* str = "Hello World";
    printf("12345678901234567890\n");
    printf("%20s\n", str);
    printf("%-20s\n", str);
    printf("%20.20s\n", str);
    printf("%-20.20s\n", str);
    printf("%10s\n", str);
    printf("%-10s\n", str);
    printf("%10.10s\n", str);
    printf("%-10.10s\n", str);
    return 0;
}
Try it and you'll see what I mean.
 
Share this answer
 
v2
C++
char blank;                       // you never initialise this variable
printf ("Enter a string\n");
gets(str);
printf ("\n%19c%s", blank, str);  // you only shift it 19 spaces

So try:
C++
printf ("Enter a string\n");
gets(str);
printf ("\n%20c%s", ' ', str);

But is that really what the question asks you to do?
 
Share this answer
 
Comments
CPallini 16-May-22 4:42am    
"But is that really what the question asks you to do?"
Good question.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900