i got a program here

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.

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
//strngprgm
//Micah Standing

#define _CRT_SECURE_NO_DEPRECATE 1

#include <iostream>
using namespace std;

void mstrcpy(char[], char[]);
void mstrlen(char[], char[], char[], char[], char[])
void mstrcat(char[],char[]);

int main ()
{
	char string1[40] = "Thomas Edison";
	char string3[40] = "Cracker Jack";
	char string4[40] = "How are ";
	char string5[40] = "doing ?";
	
	cout << "String 1 is " << string1 << endl;
	cout << "String 3 is " << string3 << endl;
	cout << "String 4 is " << string4 << endl;
	cout << "String 5 is " << string5 << endl;
	
void mstrlen(char[], char[], char[], char[], char[])	
	(	
		char string1[40] = "Thomas Edison";
		char string3[40] = "Cracker Jack";
		char string4[40] = "How are ";
		char string5[40] = "doing ?";
		
		cout << "\nThe Length of string 1 is " << strlen(string1);
		cout << "\nThe Length of string 3 is " << strlen(string3);
		cout << "\nThe Length of string 4 is " << strlen(string4);
		cout << "\nThe Length of string 5 is " << strlen(string5);
		cout << "\n" << endl;
	}
void mstrcpy(char[], char[])
  {
	char string1[40] = "Thomas Edison";
	char string3[40] = "Cracker Jack";
	
	cout << string1 << endl;
	cout << string3 << endl;
	mstrcpy(string1, string3);
	cout << "\nNow String 1 is " << string1 << endl;
  }	
void mstrcat(char[],char[])
	{
		char string4[40] = "How are ";
		char string5[40] = "doing ?";
		
		cout << string4 << endl;
		cout << string5 << endl;
		mstrcat(string4, string5);
		cout << "\nNow String 4 says " << string4 << endl;
     }
}
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).
what do i do about about the mstrcat and mstrcpy thing, how do i make my own strlen function not quit understanding it, sorry
how do i revese a string.
how would i start a function for checking the number occurences of a character.
Missing semicolon after
void mstrlen(char[], char[], char[], char[], char[])

Local function definitions are bad. Do it like this:
main()
{
}

function1()
{
}

function2()
{
}

Fix '(' to '{'
Your recursive function 'mstrcpy' has no end condition, this function will run until the stack runs out - crash.


To get you started
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
#define _CRT_SECURE_NO_DEPRECATE 1

#include <iostream>
using namespace std;

int _strlen(char Str[]){
  int i;
  for( i = 0; Str[i]; ++i ) ;
  return i;
}

void _strcpy(char Dest[], char Src[]){
   int i;
   for( i = 0; Src[i]; ++i )
     Dest[i] = Src[i];
   Dest[i] = 0;
}

int main(){
  char Test[100] = "Testing string";
  char Copy[100]= "This should be copied";
  cout<<"The length of '"<< Test << "' is " << _strlen(Test) <<endl;
  cout<<Test<<" "<<Copy <<endl;

  _strcpy(Test,Copy);
  cout<<Test<<" "<<Copy <<endl;

  char a;
  cin>>a;
  return 0;
}
Last edited on
what does it mean when it says mstrlen: function does not take 1 arguments.
Your mstrlen() function seems to take 5 arguments, though I'm not sure why:
void mstrlen(char[], char[], char[], char[], char[]);

It should only take one argument:
1
2
// should be unsigned because a string never has a length less than 0
unsigned int mstrlen (char[]);
Last edited on
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.
Last edited on
You would use something like this:
1
2
3
4
5
6
7
8
//returns the number of occurrences of the 'needle' char found in the 'haystack' string
int 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;
}
Last edited on
i see it workds with words but will it work with letters.
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 hope that answers your question. ^_^

rpgfan
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.
Last edited on
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:
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
//strngprgm
//Micah Standing

#define _CRT_SECURE_NO_DEPRECATE 1

#include <iostream>
#include <string>
using namespace std;

void mstrcpy(char[], char[]);
void mstrlen(char[], char[]);
void mstrcat(char[],char[]);

int main ()
{
	char string1[40] = "Thomas Edison";
	char string3[40] = "Cracker Jack";
	char string4[40] = "How are ";
	char string5[40] = "doing ?";
	char string6[40] = "Butter Dish";
	cout << "String 1 is " << string1 << endl;
	cout << "String 3 is " << string3 << endl;
	cout << "String 4 is " << string4 << endl;
	cout << "String 5 is " << string5 << endl;
}
void mstrlen(char string1[], char string3[])	
{	
		char string1[40] = "Thomas Edison";
		char string3[40] = "Cracker Jack";

                   cout << "\nThe Length of string 1 is " << mstrlen(string1);
	   cout << "\nThe Length of String 3 is " << mstrlen(string3);
		cout << "\n" << endl;
}
void mstrcpy(char string1[], char string3[])
{
	char string1[40] = "Thomas Edison";
	char string3[40] = "Cracker Jack";
	
	mstrcpy(string1, string3);
	cout << "\nNow String 1 is " << string1 << endl;
}	
void mstrcat(char string4[],char string5[])
{
		char string4[40] = "How are ";
		char string5[40] = "doing ?";
		
		mstrcat(string4, string5);
		cout << "\nNow String 4 says " << string4 << endl;
}
{		
                                string (string6);
		string::reverse_iterator rit;
		for ( rit = str.rbegin(); rit < str.rend(); rit++ )
		cout << *rit;
		return 0;
}
Last edited on
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;
}
Last edited on
sorry about bothering you with this problem. here is what i got now.
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//strngprgm
//Micah Standing

#define _CRT_SECURE_NO_DEPRECATE 1

#include <iostream>
#include <string>
using namespace std;

void mstrcpy(char[], char[]);
void mstrcat(char[],char[]);
int mstrlen(char[]);

int main ()
{
	char string1[40] = "Thomas Edison";
	char string3[40] = "Cracker Jack";
	char string4[40] = "How are ";
	char string5[40] = "doing ?";
	char string6[40] = "Butter Dish";
	cout << "String 1 is " << string1 << endl;
	cout << "String 3 is " << string3 << endl;
	cout << "String 4 is " << string4 << endl;
	cout << "String 5 is " << string5 << endl;
}
int mstrlen(char[], string) 
{	
	char string1[40] = "Thomas Edison";
	cout << "String 1 : " << string1 << "\t" << mstrlen(string1) << "chars\n";
} 
{
	int i = 0; 
	while(string[i]!='\0') 
	{
		i++;
	}
  	return i;
}
void mstrcpy(char string1[], char string3[])
{
	char string1[40] = "Thomas Edison";
	char string3[40] = "Cracker Jack";
	
	mstrcpy(string1, string3);
	cout << "\nNow String 1 is " << string1 << endl;
}	
void mstrcat(char string4[],char string5[])
{
		char string4[40] = "How are ";
		char string5[40] = "doing ?";
		
		mstrcat(string4, string5);
		cout << "\nNow String 4 says " << string4 << endl;
}
{		
                                string (string6);
		string::reverse_iterator rit;
		for ( rit = str.rbegin(); rit < str.rend(); rit++ )
		cout << *rit;
		return 0;
}

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?
Last edited on
strngs.cpp(31) : error C2447: '{' : missing function header (old-style formal list?)
what exactly does this mean.
Ok. I will highly some problems for you:

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
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)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>

using namespace std;

char* mstrcat(char str1[], char str2[]) {
  char *cNew = new char[strlen(str1) + strlen(str2) + 1];   

  sprintf(cNew, "%s%s", str1, str2);  
  return cNew;
}

int main() {

 char cname[] = "my name is ";
 char cname2[] = "Zaita";
 
 
 char *pRet = mstrcat(cname, cname2); 
 cout << pRet << endl;
 delete [] pRet;

}
Last edited on
thank you i got it now. 1 more thing, what about the occurences function. for some reason i'm having the most trouble with that. can you help me
Topic archived. No new replies allowed.