i want to adjust the spacing entered, but...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • WuJianWei
    New Member
    • Mar 2007
    • 10

    i want to adjust the spacing entered, but...

    If you type a string:

    "It's good."

    there's one space between the two words.

    but how can i adjust the string to two spaces or more.

    "it's good."
  • WuJianWei
    New Member
    • Mar 2007
    • 10

    #2
    i want to adjust the spacing entered, but...

    cout<<"\n----------------------------------------------------------------"
    <<"\n3. Adjust the spacing of the input string with a given number.\n"
    <<"----------------------------------------------------------------\n"
    <<"\n- Please enter the spacing: ";
    int space=1;
    cin>>space;

    cin.getline(str ing,150,'\n');
    cout<<"\n- Please enter a string of length < 70:\n";
    cin.getline(str ing,150,'\n');

    int i=0;
    for( char *s = string ; *s!='\0' ; s++, i++ );

    while( i>=70 )
    {
    cout<<"\n- The input string is too long. Please reenter a string of length < 70:\n";
    cin.getline(str ing,150);
    i=0;
    for( char *s = string ; *s!='\0' ; s++, i++ );
    }

    static int x=0;
    int z=space;
    while(string[x]!='\0')
    {
    for(;string[x]!=' ';x++)
    cout<<string[x];

    for(;space>0;sp ace--)
    cout<<' ';
    x++;
    space=z;
    }
    cout<<string<<e ndl<<endl;

    The output is always with something undesirable. Who can help me to debug?

    Comment

    • Indianraja
      New Member
      • Apr 2007
      • 22

      #3
      Originally posted by WuJianWei
      If you type a string:

      "It's good."

      there's one space between the two words.

      but how can i adjust the string to two spaces or more.

      "it's good."

      get the ascii value of spacebar and then set the command if it's cliked then set how many spaces u want..... k... if u do lik this when ever u press the spacebar it give the no of spaces as u set....

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        WuJianWei-

        I have merged these threads as you asked the same question. Please read our Posting Guidelines and abide by them as you agreed to when you joined this community. This includes not double-posting, and using code tags around your code.

        I realize one question was asked well before the other. In cases like that, if you think we have forgotten about your code, after 24 hours, you can reply to your original post with a "bump" message, this will move it back to the top of the forum.

        If you're looking to just add whitespace, I would recommend looking into strtok(). You can use it to pick apart a string at a certain delimiter, and then then use something like strcat() or strcpy() to add the desired number of spaces.

        Comment

        Working...