• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Remove one or two middle letters from a word

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need the shortest possible c++ code to solve this problem, with an explanation of each line of code. Here is the task: the word is given. If its length is odd, then delete the middle letter, otherwise - two middle letters.

I found solutions to this task on the Internet, but the solutions are huge and without explanation, and I need to learn it and it should be as short as possible in c++ code with an explanation of each line of code. So I found solutions:
 
Bartender
Posts: 15743
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch!

Finding ready solutions on the internet won't do. Instead, tell us how YOU would solve the problem. What are you stuck on?
 
Marshal
Posts: 80866
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch (again).

Peter Kennedy wrote:. . .

Please tell us where you found that. Did they explain what that bit of code does? Did they tell you it is an abomination against the conventions of good programming? There are all sorts of things on the Internet and it is difficult to tell whether you have found something very good or very bad.
 
Rancher
Posts: 510
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I would have thought your best bet would be a single memmove(), no loops. Just (obviously) need to work out its arguments, but without actually trying it, I wouldn't have thought that was too difficult...
 
John Matthews
Rancher
Posts: 510
15
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Corrected the indentation of the original post:
 
Peter Kennedy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Welcome to CodeRanch!

Finding ready solutions on the internet won't do. Instead, tell us how YOU would solve the problem. What are you stuck on?



I wanted to ask you for help, I myself know C++ terribly.
 
Peter Kennedy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I found the code on some site, I do not remember exactly which one. Code snippet, no. No.
 
Peter Kennedy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Kennedy wrote:

Peter Kennedy wrote:I need the shortest possible c++ code to solve this problem, with an explanation of each line of code. Here is the task: the word is given. If its length is odd, then delete the middle letter, otherwise - two middle letters.

I found solutions to this task on the Internet, but the solutions are huge and without explanation, and I need to learn it and it should be as short as possible in c++ code with an explanation of each line of code. So I found solutions:






And why fix the otsup, I asked for a solution to this problem, I need the shortest possible C ++ code with an explanation of each line of code for that task.
 
Marshal
Posts: 28424
102
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Kennedy wrote:I asked for a solution to this problem, I need the shortest possible C ++ code with an explanation of each line of code for that task.



What would you need that for?
 
Campbell Ritchie
Marshal
Posts: 80866
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Peter Kennedy wrote:I found the code on some site, I do not remember exactly which one. . . . .

Shame. You could have blacklisted it.

Please don't push quote to reply. Push reply. Quotes often add nothing to the discussion, and in your post you had my words outside the quoted area.
 
Peter Kennedy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul Clapham , I need to write this code in college.
 
Campbell Ritchie
Marshal
Posts: 80866
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, you have to write it yourself.
 
Peter Kennedy
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Campbell Ritchie,and why I turned to this site then, I myself don’t know C++ well.
 
Campbell Ritchie
Marshal
Posts: 80866
505
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Start by turning your computer off. It is getting in your way. Then write some words on paper; you need an eraser too because you will have to destroy the evidencecorrect any mistakes. Go through the individual letters and write out the logic for copying them to a new location or not copying them.
Remember: if you are using a char[] for a word with n letters, you will need space for n + 1 letters because of the null character at the end.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic