Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9023967
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:53:28+00:00 2026-06-16T05:53:28+00:00

I’m trying to do a little task which asks to convert numbers to letters

  • 0

I’m trying to do a little task which asks to convert numbers to letters of phone keypad, for example if input is 222 it means phone button “2” ( http://upload.wikimedia.org/wikipedia/commons/7/7d/Telephone-keypad.png ) is pushed 3 times and output should be “C” and etc.
So first thing what i should do is to separate all the sequences, for example 22255-444 into 222 , 55 , – , 444 and then i think figure everything out, but now problem is that my function can’t read last sequence

#include <iostream>
#include <fstream>
using namespace std;
//-------------------------------------------------------------------------

void encode(string text, string &result, int &i)
{
 char keyboard[10][4] = {
    {' ',' ',' ',' '},
    {'.','?','!','.'},
    {'a','b','c','a'},
    {'d','e','f','d'},
    {'g','h','i','g'},
    {'j','k','l','j'},
    {'m','n','o','m'},
    {'p','r','q','s'},
    {'t','u','v','t'},
    {'w','x','y','z'}
  };

  int j;
  for(j = i; j<text.size();j++)
  {
    if(text[i] != text[j] || j == text.size())
    {
        result = text.substr(i, j-i);
        i = j-1;
        break;
    }

  }
  cout << result << endl;

}



int main()
{
  ifstream fd("sms.in");
  string text;
  string result;
  getline(fd, text);
  for(int i = 0; i<text.size();i++)
  {
    encode(text, result, i);
  }
  return 0;
}

as a test now im using this input : 5552-22-27777 , output should be 555 2 – 22 – 2 7777, but for me its 555 2 – 22 – 2 2 2 2 2.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:53:30+00:00Added an answer on June 16, 2026 at 5:53 am

    In this if statement:

    if(text[i] != text[j] || j == text.size())
    

    The second condition (j == text.size()) will never be true because the loop will terminate before that. So when you reach the end of the string, the values of result and i will not be updated correctly.

    What you can do is remove the termination condition from the loop (it’s not necessary to have one because you will break out of the loop anyway). And you will need to reverse the order of the conditions in the if so that you don’t read past the end of the string:

    for(j = i; ;j++)
    {
        if (j == text.size() || text[i] != text[j])
        ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to select an H1 element which is the second-child in its group
I have an array which has BIG numbers and small numbers in it. I
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I used javascript for loading a picture on my website depending on which small
I am trying to render a haml file in a javascript response like so:
I would like to run a str_replace or preg_replace which looks for certain words

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.