Click here to Skip to main content
15,881,803 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Given a string S, print the string S removing first and last character till no characters are left.

Boundary Condition :
1 <= Length of string S <= 1000

Input Format:
The first line contains S

Example Input/Output 1:
Input:
water

Output :
water
ate
t


Example Input/Output 2:
Input:
keyboard

Output :
keyboard
eyboar
yboa
bo

What I have tried:

C++
#include <stdio.h>
#include <stdlib.h>

int main()
{
    int i=0;
    char s[1000],temp[1000];
    scanf("%s",s);
    while(s[i+2]!='\0')
    {
        temp[i]=s[i+1];
        i++;
    }
    temp[i]='\0';
    printf("%s\n",temp);
    return 0;
}
Posted
Updated 12-Aug-18 0:55am
v2
Comments
W Balboos, GHB 28-Feb-18 7:53am    
Your homework - that means you should do it.

Solving problems yourself is the whole point of it, at least for those who actually wish to learn.

#include <stdio.h>
#include <stdlib.h>
void main()
{
char a[100];
int n,m,i,j;
scanf("%s",a);
n=strlen(a);
m=n;
for(i=0;i<n;i++)
{
if(m>2)
{
for(j=i+1;j<n-1-i;j++)
{
printf("%c",a[j]);
}
printf("\n");
m=m-2;
}
}
}
 
Share this answer
 
Comments
Patrice T 12-Aug-18 7:25am    
Almost 6 months and your code does not fit the question !
Tip 1: learn programming by using the debugger and make some console output.

Tip 2: You can use the function strncpy to copy the desired amount of chars.

Tip 3:
C++
char *p = (s + 1);
 
Share this answer
 
You need a slightly bigger array (for a 0-terminated string of 1000 characters, you need 1001 of them).
Then you need the length of the string, say len.
Eventually set start = 0 and make a loop while (len >=1) , at each iteration
  • output len characters, starting from start.
  • increment start.
  • decrement twice len
 
Share this answer
 
v3
Comments
Maciej Los 1-Mar-18 3:11am    
5ed!
CPallini 1-Mar-18 4:35am    
Thank you!
This is your homework, so it's part of your task to get it working properly - this is a process called "debugging" and it's a skill that only gets better with use.

I have no idea what system you are using, but you almost certainly have a debugger available, which will let you run your code and look at what is going on at the same time. Google "debugger" and the name of your IDE and you should find instructions.

But I'll give you a hint: how many times do you look at the original string? Have you considered nested loops?
 
Share this answer
 
Comments
W Balboos, GHB 28-Feb-18 11:53am    
An obvious candidate for recursion - but they're having enough problems.

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