Monday, May 24, 2010

HELPP! with this c++ program please("Find & Replace"), have my annual lab test tomm!!?

Thanks! and the program i need help with is :


A c++ prog to find a word from a paragraph (both word and para typed in by user) and replace it with a word he/ she specifies. Basically the text editor program for "Find and Replace" function.





(I know i have to use a 2 dimensional arrays but i am stranded midway.. i accept a 2-d char array and a word but how to compare that word in the 2-d array, i mean, how does one distinguish words in arrays??? )





Please be quick!! Thanku so much

HELPP! with this c++ program please("Find %26amp; Replace"), have my annual lab test tomm!!?
I think we need some more information about this problem before we can help you.





First, how are you reading in the paragraph of text? Is the user going to enter it, or do you read it from a file?





Second of all, do you have to use a two-dimensional array? STL strings would be easier (#include %26lt;string%26gt;). The only reason you would need a 2-d array is if you store the paragraph as a matrix of characters (rows and columns). But the drawback to that is that it is a pain to manipulate such data. If you can use stl strings, or even a 1-d array of characters things would be much easier.





Let us know.


Thanks.


--------------------------------------...





Gotcha, a 2-d array will work fine. You need some nested loops to accomplish this task.





I'm going to give you some pseudo-code, and you will need to translate that into C++.





After you read in the martix, you will need a loop to go through each line the matrix. Your ( for i=0 to 9) loop will work for that. Once you get each line with that for loop, you will have to loop through each of the characters in that line in search of the first character in the special word....





for (i= 0 to 9) {


__for (j = 0 to (length of line[i] -1) ) {


____if (line[i][j] == word[0]) {


______/* when we get to this point in the code, we know that


_________we have found the first letter of the word, now all we


_________need to do check to see if the following letters match


_________the word as well


______*/





______bool found = true; // tracks if we found the word


______k = 0; // index for the following loop


______// this while loop compares each letter


______while (k %26lt; (size of word) and found==true) {


________if (find_word[k] == line[i][k])


__________k++; // letter did match, so compare next letter


________else


__________found = false; // happens when letter doesnt match


______}





______// now, if all the letters matched found will equal true,


______// but if one didnt match, found will have been set to false


________if (found == true) {


________// now we need a loop to replace line[i] with one that


________// has the new word in it





________char templine = line[i]; // save old line


________int temp=0;





________// put new word into line


________for (int m= j to (size of word-1) ){


__________line[i][m] = replace_word[temp];


__________temp++;


________}





________// this will put the remaining part of the old line


________// after the new word in the array


________int temp2 = j+(size of replace_word);


________for (int n= (j+size of find_word) to (size of templine-1) {


__________line[i][temp2] = templine[n];


__________temp2++;


________}


______}





____}


__}


}





There are probably some errors in this (for instance, in the while loop you will probably need to check if you have reached the end of line[i]). But in any case, that will work for a basic algorithm for you.





Good luck!


pw
Reply:Use std::string charAarray[12] ;
Reply:import java.util.*;





public class Student


{


private String id;


private String firstname;


private String lastname;


private ArrayList %26lt;String%26gt; letters = new ArrayList %26lt;String%26gt; ();





public Student(String i, String f, String l)


{


id= i;


firstname = f;


lastname = l;


}





public String getID()


{


return id;


}





public String getFirstname()


{


return firstname;


}





public String getLastname()


{


return lastname;


}





public void addGrades(String g)


{


letters.add(g);


}





public double computeGPA()


{


double gpa = 0.0;


for (int i = 0; i %26lt; letters.size(); i++)


{


String ltrgrade = letters.get(i);


if (ltrgrade.equals("A"))


gpa = gpa + 4.0;


else if (ltrgrade.equals("B"))


gpa = gpa + 3.0;


else if (ltrgrade.equals("C"))


gpa = gpa + 2.0;


else if (ltrgrade.equals("D"))


gpa = gpa + 1.0;


else


gpa = gpa + 0.0;


}





if (gpa == 0.0)


{


gpa = 0.0;


}





else


{


gpa = gpa / letters.size();


}





return gpa;





}


}

wedding flowers

No comments:

Post a Comment