删除第一个和最后一个结束 [英] Remove first and last till end

查看:121
本文介绍了删除第一个和最后一个结束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个字符串S,打印字符串S删除第一个和最后一个字符,直到没有剩下字符。



边界条件:

1< =字符串长度S< = 1000



输入格式:

第一行包含S



示例输入/输出1:

输入:





输出:

water

ate

t





输入/输出示例2:

输入:

键盘



输出:

键盘

eyboar

yboa

bo



我尝试过:



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

解决方案

你需要一个稍微大一点的数组(对于 0 - 终止字符串 1000 个字符,你需要 1001

然后你需要字符串的长度,比如说 len

最后设置 start = 0 并在每次迭代时进行循环( len> = 1

  • 输出 len 字符,从开始开始。
  • increment start
  • 减少两次 len


这是你的作业,所以它是你的任务的一部分,以使其正常工作 - 这是一个称为调试的过程,它是一种只有使用才能变得更好的技能。



我不知道是什么您正在使用的系统,但几乎可以肯定有一个调试器,它可以让您运行代码并同时查看正在发生的事情。谷歌调试器和你的IDE的名称,你应该找到说明。



但我会给你一个提示:你看多少次原件串?您是否考虑过嵌套循环?


提示1:使用调试器学习编程并进行一些控制台输出。



提示2 :您可以使用 strncpy 功能复制所需数量的字符。



提示3:

  char  * p =(s +  1 ); 


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:

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

解决方案

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


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?


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:

char *p = (s + 1);


这篇关于删除第一个和最后一个结束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆