How to used string Array

Hi

My Problem is Im trying to figure it out how can i used string of array that initialized to a text and how can i read out character by character, i know how to do this using string only and char but is there any way can i used string of array

below is examples on using string and char array

code using char array:

<
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
 int main ()
{
	int aSum=0;
	int pSum=0;
	int hSum=0;
	int gSum=0;
	int rSum=0;

	char storyLine [20] = "parpahpgragph";	
	 
	for(int i=0;i<22;i++)
	{
		
		if(storyLine[i] == 'a')
		aSum=aSum+1;
			else if(storyLine[i] == 'p')
		      pSum=pSum+1;
			else if(storyLine[i] == 'h')
			   hSum=hSum+1;
			else if(storyLine[i] == 'g')
		       gSum=gSum+1;
		    else if(storyLine[i] == 'r')
		       rSum=rSum+1;
		
	}
 cout<<"a = "<<aSum<<endl;
cout<<"p = "<<pSum<<endl;
cout<<"h = "<<hSum<<endl;
cout<<"g = "<<gSum<<endl;
cout<<"r = "<<rSum<<endl;

return 0;
} 


code using char array:
<
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
 int main ()
{
	int aSum=0;
	int pSum= 0;
	int hSum=0;
	int gSum=0;
	int rSum=0;

	string storyLine  = "parpahpgragph";	
	 
	for(int i=0;i<25;i++)
	{		
		if( storyLine[i] == 'a')
		
		aSum=aSum+1;
		else if (storyLine[i]=='p')
		pSum=pSum+1;
		else if (storyLine[i]=='h')
		hSum=hSum+1;
		else if (storyLine[i]=='g')
		gSum=gSum+1;
		else if (storyLine[i]=='r')
		rSum=rSum+1;	
		
	}
   cout <<"a = "<<aSum<<endl; 
   cout <<"p = "<<pSum<<endl; 
   cout <<"h = "<<hSum<<endl;
   cout <<"g = "<<gSum<<endl;
   cout <<"r = "<<rSum<<endl;

return 0;

}

is there anyway using string of array that can do the same thing like the above examples
Last edited on
Hey. Please edit your post and put everything between [code]code tags[/code] - http://www.cplusplus.com/articles/jEywvCM9/


1
2
3
4
string arr[2] = { "Tarik", "Neaj" };

	cout << arr[1][3] << endl; // first [] accesses the entire string. Second [] accesses the individual characters within the string.
	                           // this will print out the letter "j". 
Thank you for your quick reply, my question is how can i read individual characters from this "parpahpgragph" using string array

thanks

Sorry but I dont get your question. You dont have a string array in your code, what I showed you is a string array. Please elaborate on what you want and be specific. Also, as I said in my first post, please edit your post so we can help you the best way possible, it makes the code much more pleasant to read.

To get each individual characters from the "parpahpgragph" You just do storyLine[index] like you've done in your code. What exactly are you asking?
Last edited on
sorry i will try to explain again, what i want is how to count the number of 'a','p','h','g' and 'r' out from this "parpahpgragph" in using STRING ARRAY.

i know how to count individual character in using string only (not string array) and char array but need help on how can i use string array to count character by character.

what modification can i do to change this below code using string array that can count each individual character in "parpahpgragph";

1
2
3
4
5
6
7
8
string storyLine  = "parpahpgragph";	
	 
	for(int i=0;i<25;i++)
	{		
		if( storyLine[i] == 'a')		
		aSum=aSum+1;
         }
cout<<"a = "<<aSum<<endl;
You're not explaining things in the best way, but I might have gotten what you want, although it makes no sense.

Do you mean storing that paragraph in a string array? hmm okay, kinda makes no sense but, in that case you would need nested loops. You create a string array like I showed you in my example, then you use a nested loop.

1
2
3
4
5
6
7
8
9
string arr[2] = { "Tarik", "Neaj" };

for(int i = 0; i < 2; i++)
{
     for(int j = 0; i < arr[i].lenght(); j++}
     {
           // do your stuff here
     }
}
Last edited on
sorry to make you confused, but yes i got your code and tried it out and yes it can print character

but what i want is how can i use your code to count the number of 'a','p','h','g' and 'r' out from "parpahpgragph"

What i know that this can be done using string(not array) and char array but no idea on using string array

using string:

what modification can i do to change this below code using string array that can count each individual character in "parpahpgragph";



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
int main ()
{
	int aSum=0;
	int pSum= 0;
	int hSum=0;
	int gSum=0;
	int rSum=0;

	string storyLine  = "parpahpgragph";	
	 
	for(int i=0;i<25;i++)
	{		
		if( storyLine[i] == 'a')
		
		aSum=aSum+1;
		else if (storyLine[i]=='p')
		pSum=pSum+1;
		else if (storyLine[i]=='h')
		hSum=hSum+1;
		else if (storyLine[i]=='g')
		gSum=gSum+1;
		else if (storyLine[i]=='r')
		rSum=rSum+1;	
		
	}
   cout <<"a = "<<aSum<<endl; 
   cout <<"p = "<<pSum<<endl; 
   cout <<"h = "<<hSum<<endl;
   cout <<"g = "<<gSum<<endl;
   cout <<"r = "<<rSum<<endl;

return 0;

}


output:
a =  3
p = 4
h = 2
g = 2
r = 2




But i want the same thing but doing it using ARRAY

e.g something like this:
string storyLine[] = "parpahpgragph";



Hope you understand what i trying to explain

Sorry about my English and also im beginner in C++

Thanks




Last edited on
But why do you need a string array? A string arrays job is to hold multiple strings. You only have one string, the array is completely useless at that point. In any case, if you want to create an array of only one string, as weird as that is, you can just do it like this



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int aSum = 0;
	int pSum = 0;
	int hSum = 0;
	int gSum = 0;
	int rSum = 0;

	string storyLine[1] = { "parpahpgragph" };

	for (int i = 0; i < storyLine[0].length(); i++)
	{
		if (storyLine[0][i] == 'a')

			aSum = aSum + 1;
		else if (storyLine[0][i] == 'p')
			pSum = pSum + 1;
		else if (storyLine[0][i] == 'h')
			hSum = hSum + 1;
		else if (storyLine[0][i] == 'g')
			gSum = gSum + 1;
		else if (storyLine[0][i] == 'r')
			rSum = rSum + 1;

	}

	cout << "a = " << aSum << endl;
	cout << "p = " << pSum << endl;
	cout << "h = " << hSum << endl;
	cout << "g = " << gSum << endl;
	cout << "r = " << rSum << endl;
Hi TarikNeaj

Thank you very much your time and help, this is what i want, for your information why i want to use this it is the requirement of my assignment to use string array to store the story and count vowel out of it

Thank you again

god bless

Topic archived. No new replies allowed.