Combine numbers in string

This is a discussion on Combine numbers in string within the C++ Programming forums, part of the General Programming Boards category; hi, can someone help me on the problem that i had: Problem: the string output is:4488118417, and i split it ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    71

    Combine numbers in string

    hi, can someone help me on the problem that i had:

    Problem:

    the string output is:4488118417,
    and i split it to two: 44 88 11 84 17,
    and how can i make 44 in one string, usually 44 is str[0] and str[1].

    for example

    str[0]=44
    str[1]=88
    str[2]=11
    str[3]=84
    str[4]=17

    This is the code that i stuck with:

    Code:
        stringstream numbers;
    
    /* Insert integer into stringstream*/
        numbers <<numLowerCaseLetter(name)<<sumAscii(name)<<lastValue<<counter<<sum;
    
    /* Convert stringstream into string.*/
        string str = numbers.str();
    
        if(str.size() % 2 == 0)
        {
            for(int i = 0; i < str[i]; i=i+2)
            {
                    cout<<str.substr(i,2)<<" ";   
            }
        }    
        else
        {
            
            str.size()+1;
            str.append( 1, '0' );
            
            for(int i = 0; i < str[i]; i=i+2)
            {
                cout<<str.substr(i,2)<<" ";
            }

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    Try This
    Code:
    #include<iostream>
    #include<string>
    using namespace std;
    int main()
    {
    	
    	string str="12345678";
    	string array[5];
    	int i=0;
    	int k=0;
        while(i<str.size()-1)
        {
            array[k]=str.substr(i,2);
    		cout<<array[k]<<endl;
    		i+=2;
    		k+=1;
    	}    
    	return 0;
    }

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    71
    wow.. thank you very much,,, u are such a genius.... very appreciate on what u done...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Custom String class gives problem with another prog.
    By I BLcK I in forum C++ Programming
    Replies: 1
    Last Post: 12-18-2006, 02:40 AM
  2. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. the definition of a mathematical "average" or "mean"
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 12-03-2002, 10:15 AM
  5. Again Character Count, Word Count and String Search
    By client in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 11:40 AM

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