We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,657 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Inserting a hyphen between digits of a user input

I am trying to write a program that takes a 10 digit phone number and inserts hyphens between the 3rd and 4th digit and the 6th and 7th digit

eg. Enter phone number: 5553459875

Your number is 555-345-9876


I've decided that reading the number as an integer won't work and tried reading each as a char but then it still doesn't print how I want it to

Any help would be appreciated

Thanks

2
Contributors
2
Replies
9 Hours
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0


I am trying to write a program that takes a 10 digit phone number and inserts hyphens between the 3rd and 4th digit and the 6th and 7th digit

eg. Enter phone number: 5553459875

Your number is 555-345-9876


I've decided that reading the number as an integer won't work and tried reading each as a char but then it still doesn't print how I want it to

Any help would be appreciated

Thanks

Seems to me like your process is this...

  1. You need a character array of length 13 or more.
  2. Assume it currently contains the ten digits, plus a NULL terminator at index 10.
  3. You want to stick a hyphen at index 3. That means you need to move the data at indexes 3 to 10 to 4 to 11. Use the memmove function from string.h to move those 8 bytes one byte to the right.
  4. Make index 3 a dash.
  5. Same process for index 7. It needs to be a dash, so you need to move bytes 7 through 11 to bytes 8 thorough 12. Again, use memmove to move those five bytes one byte to the right.
  6. Make index 7 a dash.
  7. Done!
VernonDozier
Posting Expert
5,675 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 738
Skill Endorsements: 18

exactly the kind of help i was looking for. thank you i will get right on it

Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
Question Answered as of 1 Year Ago by VernonDozier

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0620 seconds using 2.66MB