Tuesday, July 14, 2009

(C++)The output for the sample string would be what?

Given a "C" style string such as char[] s1 = "ka2d23UkLeA2" write a main function program that will count the number of occurrences of the character 'a' and 'A' and the number of occurrences of the character '2'.


The output for the sample string would be:





The number of As is 2.


The number of 2s is 3.


//please help

(C++)The output for the sample string would be what?
Yeah, all you need to do is loop through all of the characters in the array.





char myArr[] = "hello 2nd chascity.";





Create the loop





For(;;)


For(int i = 0; i %26lt;= int(strlen(myArr)); i++)


While (myArr(i) != "/0")





test all the values





If (myArr(i) == "a"){...}


switch (myArr(i)){


Case 'a': ...}





record values


int aFound = 0;


aFound ++;





int i = 0;


int aFound = 0;


for(;;){


if (myArr(i) == "a"){


aFound++;}


}


cout %26lt;%26lt; aFound;








That just test 'a' but you can do the same for the rest
Reply:int num_a = 0;


int num_2 = 0;


int i = -1;


char[] s1 = "ka2d23UkLeA2";





while (s1[i++] != '\0') {


if (s1[i] == 'a' || s1[i] == 'A') num_a++;


if (s1[i] == '2') num_2++;


}





printf("In the string \"%s\" there are %d A or a's, and %d 2's", s1, num_a, num_2);
Reply:google digiu


No comments:

Post a Comment