how to find positions of chars in a string and add them into arrays?

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 827 Views - Last Post: 30 November 2016 - 02:41 PM Rate Topic: -----

#1 cbatir88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 16

how to find positions of chars in a string and add them into arrays?

Posted 26 November 2016 - 07:48 PM

How to find positions of chars in a string then add the positions into an array.


lets say i have a string sample= ATTATATACCAGGGAGGACCGGAGAGA which will be inputted by the user.

find the char 'A' in the string and count it then add into arrays...


int countA=0;
for(int i=0;i<length;i++)
{
if(sample[i] == 'A')
{
countA++;
for(countA=0;countA<=i;countA++)
{
int posA[]=sample.find(char A,0)


}
}






/*
 * main.cpp
 *
 *  Created on: Nov 25, 2016
 *      Author: Caner
 */
//            DNA, RNA, Protein analyzer.

#include <iostream>
#include <string>
#include <algorithm>
#include <cstddef>
#include <cctype>


using namespace std;

int main()
{

	cout << "==================================================================" << '\n'<< endl;
	cout << "|                  WELCOME TO CABA DNA ANAYLYZER!                |" << '\n'<< endl;
	cout << "|   THIS PROGRAM WILL ALLOW THE USER TO ANALYZE A DNA SAMPLE BY  |" << '\n'<< endl;
	cout << "|  FINDING THE LENGTH, GENES, AND TRANSLATE THE DNA TO RNA. THIS |" << '\n'<< endl;
	cout << "|  PROGRAM WILL ALSO FIND ANY AMINO ACIDS THAT MAKE A PROTEIN... |" << '\n'<< endl;
	cout << "==================================================================" << '\n'<< endl;

	string sample;
	cout << "PLEASE ENTER THE SAMPLE OF DNA SEGMENT YOU WOULD LIKE TO ANALYZE:" << '\n'<< endl;

	//get user input = string sample
	getline(cin,sample,'\n');


	//describe bases as characters
	char A = 'A';
	char T = 'T';
	char G = 'G';
	char C = 'C';

	//find the length of the sample
	int length = sample.size();

	//counters for the bases A , T , G , and C
	int countA=0;
	int countT=0;
	int countG=0;
	int countC=0;

	for(int  i = 0;i < length;i++)
	{
	    if(sample[i] == A)
	    {
	    	countA++;
	    }
	    if(sample[i] == T)
	    {
	    	countT++;
	    }
	    if(sample[i] == G)
	 	{
	 	    countG++;
	 	}
	    if(sample[i] == C)
	 	{
	 	    countC++;
	 	}

	}


	//find base positions in the sample and put them in arrays.
		//int *pointer=NULL;
		//int size=sample.size(); //input
		//pointer = sample[size];
		//int temp;

		//for(int count=0; count <= size ; count++)
		//{
			//temp = sample.find(A,size);



		//}













	cout <<"The DNA Sample you provided is " << length << " bases long." << '\n'<<endl;
	cout <<"There are "<< countA << " Adenine's" << '\n'<<endl;
	cout <<"There are "<< countT << " Thymine's" << '\n'<<endl;
	cout <<"There are "<< countG << " Guanine's" << '\n'<<endl;
	cout <<"There are "<< countC << " Cytosine's"<< '\n'<<endl;
	return 0;
}






Is This A Good Question/Topic? 0
  • +

Replies To: how to find positions of chars in a string and add them into arrays?

#2 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6400
  • View blog
  • Posts: 21,956
  • Joined: 05-May 12

Re: how to find positions of chars in a string and add them into arrays?

Posted 26 November 2016 - 07:54 PM

Considering that your for loop over lines 50-69 already hits every position within the string and the variable i is giving you the position, it seems like overkill to have to call find(). Why not simply record the positions into your positions array as you encounter them (instead of explicitly trying to find them)?
Was This Post Helpful? 1
  • +
  • -

#3 cbatir88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 16

Re: how to find positions of chars in a string and add them into arrays?

Posted 26 November 2016 - 08:13 PM

View PostSkydiver, on 26 November 2016 - 07:54 PM, said:

Considering that your for loop over lines 50-69 already hits every position within the string and the variable i is giving you the position, it seems like overkill to have to call find(). Why not simply record the positions into your positions array as you encounter them (instead of explicitly trying to find them)?



so something like this
for(int i=0;i<length;i++)
{
if(sample[i] == A)
{
count++;
arrayA[]+= i;
}
}



do you think setting my arrayA += i would solve my problem?
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3800
  • View blog
  • Posts: 13,785
  • Joined: 08-August 08

Re: how to find positions of chars in a string and add them into arrays?

Posted 26 November 2016 - 08:22 PM

This is C++, so why not use a map?
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
	string sample = "ATTATATANCCAGGGAGGACCGGAGAGA";
	map<string,int> counter = {{"A",0}, {"T",0}, {"G",0}, {"C",0}};

	for(long i = 0; i < sample.length(); i++) {
		std::map<string,int>::iterator it= counter.find(sample.substr(i,1));
		if (it != counter.end()) {
			it->second++;
		} else {
			cout << "Not found: " << sample[i] << endl;
		}
	}

	for (std::map<string,int>::iterator it=counter.begin(); it!=counter.end(); ++it)
		std::cout << it->first << " => " << it->second << '\n';

	return 0;
}


Was This Post Helpful? 0
  • +
  • -

#5 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6400
  • View blog
  • Posts: 21,956
  • Joined: 05-May 12

Re: how to find positions of chars in a string and add them into arrays?

Posted 26 November 2016 - 08:27 PM

Actually, what the OP is looking for is something along these lines:
map<char, vector<int>> basePositions;



Or better yet:
enum Base { Adenine = 'A', Cytosine = 'C', Guanine = 'G', Thymine = 'T' };
map<Base, vector<int>> basePositions; 



For each base, he wants to record the positions where that base can be found in the gene. This also happens to provide the information of how many times that base occurs by simply getting the number of items in the vector.
Was This Post Helpful? 0
  • +
  • -

#6 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3800
  • View blog
  • Posts: 13,785
  • Joined: 08-August 08

Re: how to find positions of chars in a string and add them into arrays?

Posted 27 November 2016 - 07:22 AM

Ah, well that's simple enough, except for the enums. I don't see the advantage of switching from char to enum and then back again later on.
Was This Post Helpful? 0
  • +
  • -

#7 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6400
  • View blog
  • Posts: 21,956
  • Joined: 05-May 12

Re: how to find positions of chars in a string and add them into arrays?

Posted 27 November 2016 - 08:03 AM

That's why I set the enum values to map to their character equivalents -- make it easier to do casts where appropriate.

The idea was to try to make self documenting code by indicating that the key values in the map should only be those 4 values -- not any random character.
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3800
  • View blog
  • Posts: 13,785
  • Joined: 08-August 08

Re: how to find positions of chars in a string and add them into arrays?

Posted 27 November 2016 - 08:15 AM

Yes, but that seems like too much effort. This seems easier to me:
	string sample = "ATTATATANCCAGGGAGGACCGGAGAGA";
	char viableValues[5] = {"ACGT"};

	vector<long> Positions;
	map<char, vector<long>> basePositions;
	for(int i = 0; i < strlen(viableValues); i++) basePositions[viableValues[i]] = Positions;



Note: I use long because on my system vector lengths and string lengths are long. Many systems still use int.

This post has been edited by CTphpnwb: 27 November 2016 - 08:20 AM

Was This Post Helpful? 1
  • +
  • -

#9 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6400
  • View blog
  • Posts: 21,956
  • Joined: 05-May 12

Re: how to find positions of chars in a string and add them into arrays?

Posted 27 November 2016 - 09:48 AM

Good point about the string lengths! It should be a vector<size_t> so that the code can be as compiler agnostic as possible.

The code above assumes that Positions is already pre-populated, and so it makes perfect sense to set things up that way. But to build up the position values efficiently, the primary loop will be indexing over the input gene, not over the bases. I would show some code here, but that would be solving the problem for the OP. CTphpnwb: I'll send some PM after I get some lunch going for the kids.
Was This Post Helpful? 0
  • +
  • -

#10 jimblumberg   User is offline

  • member icon

Reputation: 5553
  • View blog
  • Posts: 17,188
  • Joined: 25-December 09

Re: how to find positions of chars in a string and add them into arrays?

Posted 27 November 2016 - 10:23 AM

Quote

Note: I use long because on my system vector lengths and string lengths are long. Many systems still use int.

Actually vector and string lengths are an implementation defined unsigned type. You really should be using a size_t, which will be the correct type no matter what system you happen to be using.

By the way, why are you using the C-string for viableVariables instead of a std::string?


Jim
Was This Post Helpful? 0
  • +
  • -

#11 CTphpnwb   User is offline

  • D.I.C Lover
  • member icon

Reputation: 3800
  • View blog
  • Posts: 13,785
  • Joined: 08-August 08

Re: how to find positions of chars in a string and add them into arrays?

Posted 27 November 2016 - 11:28 AM

Yeah, I should have used size_t. I used C-string for efficiency, assuming that I only need an array of characters. I suppose that should have been const.

Actually, Positions is just an empty vector that I use to populate/initialize the map. That's what the loop is for.
Was This Post Helpful? 0
  • +
  • -

#12 cbatir88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 16

Re: how to find positions of chars in a string and add them into arrays?

Posted 28 November 2016 - 10:39 AM

View PostCTphpnwb, on 26 November 2016 - 08:22 PM, said:

This is C++, so why not use a map?
#include <iostream>
#include <string>
#include <map>

using namespace std;

int main()
{
	string sample = "ATTATATACCAGGGAGGACCGGAGAGA";
	map<string,int> counter = {{"A",0}, {"T",0}, {"G",0}, {"C",0}};

	for(long i = 0; i < sample.length(); i++) {
		std::map<string,int>::iterator it= counter.find(sample.substr(i,1));
		if (it != counter.end()) {
			it->second++;
		} else {
			cout << "Not found: " << sample[i] << endl;
		}
	}

	for (std::map<string,int>::iterator it=counter.begin(); it!=counter.end(); ++it)
		std::cout << it->first << " => " << it->second << '\n';

	return 0;
}


I tried to attach something like this to code and i get an error saying

"in C++98 'counter' must be initialized by constructor, not by '{...}' "

did i get this message because im using C++98.. how can i check which c++ edition im using.. im using eclipse to write c++...I'm also using MinGW if that helps...

i'd like to learn more about maps but dont know where to start...
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg   User is offline

  • member icon

Reputation: 5553
  • View blog
  • Posts: 17,188
  • Joined: 25-December 09

Re: how to find positions of chars in a string and add them into arrays?

Posted 28 November 2016 - 11:19 AM

Quote

did i get this message because im using C++98

Yes. You need to compile using the C++11 or higher version of the standard. This link may be of some help for setting the standard version.

Quote

i'd like to learn more about maps but dont know where to start...

Have you used your favorite Internet search engine to research std::map? Perhaps you may want to start by finding some documentation for this class?

Jim
Was This Post Helpful? 1
  • +
  • -

#14 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6400
  • View blog
  • Posts: 21,956
  • Joined: 05-May 12

Re: how to find positions of chars in a string and add them into arrays?

Posted 28 November 2016 - 12:21 PM

As a quick aside, the use the std::map<> and std::vector<> makes this assignment very easy. If you were getting paid for this work and I'm your employer, I would expect you to go down that route because it's part of an industry standard library, and you need to finish this assignment and move on to other work. On the other hand, if you are a student and I'm a teacher trying to teach you programming, data structures, and problem solving, I would steer you away from using these until you understand what is going on underneath and why it is convenient to use them in the future. The use of these containers is not required, but it sure does make things so much easier.
Was This Post Helpful? 1
  • +
  • -

#15 cbatir88   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 03-November 16

Re: how to find positions of chars in a string and add them into arrays?

Posted 30 November 2016 - 08:48 AM

Thanks for all the input guys really appreciated!
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2