1,105,447 Community Members

Inserting a hyphen between digits of a user input

Member Avatar
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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

Member Avatar
VernonDozier
Posting Expert
5,632 posts since Jan 2008
Reputation Points: 2,218 [?]
Q&As Helped to Solve: 768 [?]
Skill Endorsements: 26 [?]
Featured
 
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!
Member Avatar
Prankmore
Newbie Poster
14 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Question Answered as of 2 Years Ago by VernonDozier
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: