code a proagram that has individual string functions to do the following:
return the string length copy one string to another(string 2 to string 1) add a string to the end of another(concatenation: string 2 to string 1) reverse a string(using only one string) check a string for the number of occurences of a character
your program should initialize enough strings in main so that you can show each of the functions at work. do nout use any of the standard string library funct- ions. when you need to use the string length, use your own fuction.
i got up to the concatenation thing but when i'm making my own function for strcpy and strcat and its messing up, can you help me. here is what i got.
Um, you are using strlen when they specifically told you to create your own function for that...also, having your functions call themselves is not a good idea...(mstrcat).
how do count the number occurences of letter in that string. Example: how would i start a loop to find out how many c's i got in cracker jack. using a string function.
//returns the number of occurrences of the 'needle' char found in the 'haystack' stringint count_letters (char[] haystack, char needle);
int main (void)
{
char x[] = "Hello World!";
cout << count_letters(x, 'L') << endl;
}
Then it is just a matter of testing whether the letter occurs in the string. You can rely on the mstrlen() function you created to use an integer and access each character of 'haystack' as you would typically do via array syntax. The goal is to determine whether each character in 'haystack' matches 'needle'. If it does, then you would increase a local integer variable found in the function. Here is some "pseudo-C++" to get you started with the function:
1 2 3 4 5 6 7 8 9 10 11 12 13
int count_letters (char[] haystack, char needle)
{
int count = 0;
FOR i = 0 TO mstrlen(haystack)
//assuming it is case-insensitive matching that is required
IF MAKEUPPERCASE(haystack[i]) == needle THEN
count++;
ELSE IF MAKELOWERCASE(haystack[i]) == needle THEN
count++;
END IF
END FOR
return count;
}
micah1983 wrote: > i see it workds with words but will it work with letters.
The idea is to test every letter in the string (every letter in 'haystack') against the single letter that is being searched for ('needle'). If the parameter passed as 'haystack' isn't a string, then it will not work as expected.
i don't know what to do about the letter occurence code. you got two different ones:
1 2 3 4 5 6 7
int count_letters (char[] haystack, char needle);
int main (void)
{
char x[] = "Hello World!";
cout << count_letters(x, 'L') << endl;
}
&
1 2 3 4 5 6 7 8 9 10 11 12 13
int count_letters (char[] haystack, char needle)
{
int count = 0;
FOR i = 0 TO mstrlen(haystack)
//assuming it is case-insensitive matching that is required
IF MAKEUPPERCASE(haystack[i]) == needle THEN
count++;
ELSE IF MAKELOWERCASE(haystack[i]) == needle THEN
count++;
END IF
END FOR
return count;
}
does these two codes go together or are they just two different ways to find the occurences of a letter in the word. sorry for asking all the questions for some reason just not getting it.
i got my program and it gots all the things i need except occurence. tell me what is wrong with my reverse command. also if my functions are correct. i'm getting errors that's saying: strngprgm.cpp(31) : error C2082: redefinition of formal parameter 'string1' strngprgm.cpp(32) : error C2082: redefinition of formal parameter 'string3' strngprgm.cpp(34) : error C2660: 'mstrlen' : function does not take 1 arguments strngprgm.cpp(35) : error C2660: 'mstrlen' : function does not take 1 arguments strngprgm.cpp(40) : error C2082: redefinition of formal parameter 'string1' strngprgm.cpp(41) : error C2082: redefinition of formal parameter 'string3' strngprgm.cpp(48) : error C2082: redefinition of formal parameter 'string4' strngprgm.cpp(49) : error C2082: redefinition of formal parameter 'string5' strngprgm.cpp(54) : error C2447: '{' : missing function header (old-style formal list? what do these errors mean and how can i chang theme. and is there an easier way to write the reverse code with out the rit in it. here is my code so far:
You seem to be getting confused with your function names, prototypes and implementations.
A function prototype is just the declaration of a function that you will implement later. For example your "void mstrlen(char[], char[]);" at line 11. This means that further on in your program you must implement the function (do the actual code for the function) using the same name and parameters as your prototype. So, when you call "mstrlen(string1);" at line 31, the number of parameters does not match your prototye (line 10).
So, for the mstrlen example, you should probably do something like this :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
// Prototype (Returns an int = nb de chars)int mstrlen(char[]);
int main () {
char string1[40] = "Thomas Edison";
// Call function mstrlen()
cout << "String1 : " << string1 << "\t" << mstrlen(string1) << "chars\n";
}
//Implementation (Less beautiful than InLight's version, but easier to understand IMHO).int mstrlen(char[] string) {
int i = 0; //Arrays start at index 0// '\0' is the end of string character.while(string[i]!='\0') {
i++;
}
return i;
}
here is the errors i'm getting now: strngs.cpp(31) : error C2447: '{' : missing function header (old-style formal list?) strngs.cpp(41) : error C2082: redefinition of formal parameter 'string1' strngs.cpp(42) : error C2082: redefinition of formal parameter 'string3' strngs.cpp(49) : error C2082: redefinition of formal parameter 'string4' strngs.cpp(50) : error C2082: redefinition of formal parameter 'string5' strngs.cpp(55) : error C2447: '{' : missing function header (old-style formal list?) looks like everything is alright with the msrtlen except for line 31, but most of the errros are for my mstrcat and mstrcpy. whats wrong with those?
void mstrcpy(char string1[], char string3[])
{
// You can't re-define these. They are parameters already//char string1[40] = "Thomas Edison";//char string3[40] = "Cracker Jack";// This won't work. You are re-calling this function over and over// and over and over and over and over. Until your program crashes.
mstrcpy(string1, string3);
cout << "\nNow String 1 is " << string1 << endl;
}
void mstrcat(char string4[],char string5[])
{
// Again you are re-defining these.//char string4[40] = "How are ";//char string5[40] = "doing ?";// Again you are recalling this function in a loop forever.
mstrcat(string4, string5);
cout << "\nNow String 4 says " << string4 << endl;
} // end of function
{ // start of what? This code below belongs to nothing.
string (string6);
string::reverse_iterator rit;
for ( rit = str.rbegin(); rit < str.rend(); rit++ )
cout << *rit;
return 0;
}
Try this for your concat function: (Edit, not using string functions)