LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-23-2011, 10:17 AM   #1
lindaonline15
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Rep: Reputation: Disabled
dividing a string into substrings with three elements in C++


hi,

im trying to receive a string from user, then divide it into groups of strings with 3 elements only.
meaning if input is:
12345 it should give 123 and 45
123456 it should give 123 and 456
1234567 it should give 123 and 456 and 7

anyway what happens based on my code is that it works fine with first two (when it has groups of 3 elements and 2 element, and also when it has groups of all 3 elements) but for the last case (when makes groups of 3 elements and 1 group of 1 element) it shouws as "123 456 7 D"!! meaning it adds one extra character to the last element out of no where!

can anyone tell me what is the problem with my code??


here is my code:
Code:
#include <iostream>
#include <conio>
struct array
{
	char first;
	char second;
	char third;
};
int main ()
{
while(1){
	const int SIZE=256;
	int size=0;
	bool done=0;
	char num[SIZE],arr[3];
	int i=0,j=1,k=2;
	int z=0,x=0;
   array *arrPtr;
	cin>>num;
	for (int i=0;!done && i<SIZE;i++)
		if (num[i]!='\0')
			size++;
		else done=1;
   z=size/3;
	cout<<"Size is: "<<size<<endl;
	if(size%3==0)
   {
   	arrPtr=new array[z];
     	for(int x=0;x<z;i=i+3,j=j+3,k=k+3,x++)
			{
				arrPtr[x].first=num[i];
      	   arrPtr[x].second=num[j];
         	arrPtr[x].third=num[k];
			}
      for(int x=0;x<z;i=i+3,j=j+3,k=k+3,x++)
			{
      		cout<<arrPtr[x].first<<arrPtr[x].second;
	      	cout<<arrPtr[x].third<<endl;
			}
   }
   else
   {
   	z++;
      arrPtr=new array[z];
    	for(int x=0;x<z;i=i+3,j=j+3,k=k+3,x++)
			{
				arrPtr[x].first=num[i];
      	   arrPtr[x].second=num[j];
         	arrPtr[x].third=num[k];
			}
		x--;
      if((size%3)==1)
      	{
         	arrPtr[x].second=' ';
         	arrPtr[x].third=' ';
         for(x=0;x<z;x++)
				{
      			cout<<arrPtr[x].first<<arrPtr[x].second;
		      	cout<<arrPtr[x].third<<endl;
				}
         }
      else
      	{
            arrPtr[x].third=' ';
            for(x=0;x<z;x++)
					{
      				cout<<arrPtr[x].first<<arrPtr[x].second;
	      			cout<<arrPtr[x].third<<endl;
					}
         }

   }

}
	getch();
	return 0;
}
 
Old 06-23-2011, 10:49 AM   #2
dwhitney67
Senior Member
 
Registered: Jun 2006
Location: Maryland
Distribution: Kubuntu, Fedora, RHEL
Posts: 1,541

Rep: Reputation: 335Reputation: 335Reputation: 335Reputation: 335
Quote:
Originally Posted by lindaonline15 View Post
can anyone tell me what is the problem with my code??
Yes... it is overkill for what you need to do.

Consider this:
Code:
#include <string>
#include <iostream>

int main()
{
   std::string input;

   std::cout << "Enter a string of numbers (eg. 12345678): ";
   std::getline(std::cin, input);

   while (!input.empty())
   {
      std::cout << input.substr(0, 3) << std::endl;

      input = input.substr((input.size() > 3 ? 3 : input.size()));
   }
}

Last edited by dwhitney67; 06-23-2011 at 10:57 AM. Reason: simplified code
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Using regexec() to parse for multiple substrings ppmoore Programming 0 11-24-2010 03:38 PM
awk: Using split to divide string to array. How do I find out the number of elements? vxc69 Programming 9 02-09-2008 12:49 PM
Replacing substrings using delimeters eur0dad Linux - General 6 10-12-2006 03:34 AM
substrings and Sed marri Programming 3 01-21-2005 10:34 AM
Substrings in C marri Programming 4 08-24-2004 04:52 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:22 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration