String Problem

This is a discussion on String Problem within the C++ Programming forums, part of the General Programming Boards category; Hello, I have a fairly simple problem, that I can't seem to straighten out. Right now, I need to get ...

  1. #1
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44

    String Problem

    Hello, I have a fairly simple problem, that I can't seem to straighten out. Right now, I need to get two inputs, both strings, I can do that without a problem. The problem is, I need to extend the length of the second string input to match the length of the first, example

    Before formatting..

    1st Input: This is a test
    2nd Input: Test

    After formatting..

    1st Input: This is a test
    2nd Input: TestTestTestTe

    See where I'm going? I've tried some methods, but they became over-elaborate, and obviously failed. Are there any simple solutions to this problem out there?

    Thanks a lot
    Bagpipes – putting the fun back in funeral.

  2. #2
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Using char arrays or std::string? I think it would be pretty easy using null-terminated char arrays. I imagine a for loop like this would be a good place to start:
    Code:
    for (int i=end_of_string2,j=0;i<length_of_string1;i++,j++)
    {
      
    }
    (You could use strlen() to get the lengths of the strings)
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  3. #3
    Registered User
    Join Date
    Aug 2005
    Posts
    128
    FWIW, immediate thoughts.
    1. Get length of 1st input string
    2. get length of 2nd input string.
    3. If 2nd is less than 1st, divid length 1 by length 2. If remander, drop remander and add one.
    4. Build new string x times where is the result of above.
    5. use substring to grab the number of characters from #4 as found in #3.

  4. #4
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Piece of cake if you're using the standard string class:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    int main() {
      string a = "This is a test";
      string b = "Test";
      string c;
    
      while (c.size() < a.size())
        c += b;
      c.resize(a.size());
    
      cout << a << '\n' << c << '\n';
    
      return 0;
    }
    It's not so easy with array based strings, but still not bad, you'd have to do it manually:
    Code:
    #include <iostream>
    #include <cstring>
    
    using namespace std;
    
    int main() {
      char a[] = "This is a test";
      char b[] = "Test";
      size_t alen = strlen(a);
      size_t blen = strlen(b);
      char *c = new char[alen + 1];
    
      *c = '\0';
      while (strlen(c) < alen - blen)
        strcat(c, b);
      strncat(c, b, alen - strlen(c));
    
      cout << a << '\n' << c << '\n';
    
      return 0;
    }
    No biggie, but it's harder to get right than the first one.
    Just because I don't care doesn't mean I don't understand.

  5. #5
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    Well if you're just going to give him a solution
    Here's another way:
    Code:
    char a[50];
    char b[50]={0};
    std::cin.getline(a,50);
    std::cin.getline(b,10);
    int lenb = strlen(b);
    int lena = strlen(a);
    for (int i=lenb,j=lenb;i<lena;i++,j++)
       b[i] = b[j%lenb];
    
    std::cout<<a<<std::endl;
    std::cout<<b<<std::endl;
    Although you'd want to check that lenb!=0
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  6. #6
    The Reel Thing
    Join Date
    Jun 2005
    Posts
    44
    Yeah, I am using the string class for this. Thanks Narf! That's exactly what I was looking for, and it works fine.
    Bagpipes – putting the fun back in funeral.

  7. #7
    Nonconformist Narf's Avatar
    Join Date
    Aug 2005
    Posts
    174
    Well if you're just going to give him a solution
    Code is more expedient than a detailed explanation, especially with something trivial like this. Besides, I wasn't giving him the solution because he has to go to the effort of making sure that I'm not an idiot (ie. that the code works like it should) and integrating it into his program.
    Just because I don't care doesn't mean I don't understand.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 03:45 PM
  2. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  3. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 03:40 AM
  4. Compile Error that i dont understand
    By bobthebullet990 in forum C++ Programming
    Replies: 5
    Last Post: 05-05-2006, 10:19 AM
  5. Replies: 4
    Last Post: 03-03-2006, 02:11 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21