String calculaton as a Function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
#include <string.h>

using namespace std;

int main()

{
char anywordstring[100];
int size;
cout<<"Please enter anyword\n";
cin.getline(anywordstring,100);
size=strlen(anywordstring);
cout<<"You enterd a word" << "\n" <<size<< " characters in length\n";
cin.get();
cin.get();
return(0);

}


the above is the original code which works fine,ive had a play trying to go along the lines off the other topic with functions but i cant seem to get it to work again, this time the the string lenght calculation has to be the contained function? im going to keep pecking away in the mean time to see if i can come up a bit closer to the answer
What do you want to do exactly now? I really do not get your question.
But yes, strlen is a function here, which returns an integer (unsinged).

As you can persume strlen has a similar declaration to this:

 
unsigned int strlen(const char *str);


in reality it is something like

 
size_t strlen(const char *str);


but this is pretty much the same. The const attribute means, that the input string str will not be changed. And char * means it is a string, more precise a c-string, a pointer to a char, equivalent to a char-array.

Maikel
the idea is for a user to input any word then return the length i.e 3,7 etc. Just modifing the above program so the calculation is the Function, i was reading through a few books and found something similar to the above but couldnt put it together
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
#include <iostream>
#include <string.h>

size_t strlen(const char *str);

int main()
{

char anyword[100];
int size;

cout<<"Please enter any word \n";
cin.getline(anyword,100);

size = strlen (anyword);

cout<< anyword << "Your word lenght is: " << size;

cin.get();
cin.get();
return 0;
}

size_t strlen(const char *str);

{
return (size_t);
}


this is what ive got so far but its pulling up a few build issues
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string.h>

int stringlength(char *str);

int main()
{
char anyword[100];
cout<<"Please enter any word \n";
cin.getline(anyword,100);

stringlength = anyword;
cout<<"Your word lenght is: " << anyword;

cin.get();
cin.get();
return 0;
}
int stringlength(char *str)
{
int length=0;
return length;
}


Ive tried this way but still with build errors
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
#include <iostream>
#include <string.h>

using namespace std;

size_t strlen(const char *str);


int main()

{
char anywordstring[100];
int size;
cout<<"Please enter anyword\n";
cin.getline(anywordstring,100);


size=strlen(anywordstring);

cout<<"You enterd a word" << "\n" <<size<< " characters in length\n";
cin.get();
cin.get();
return 0;

}
size_t strlen(const char *str)
{
  return size_t;
}


forgot to include namespace, only 1 build error now, the return ??
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;
typedef unsigned int uint;

uint stringlength( const char* cstr )
{
	uint length = 0;
	while( *cstr++ ) length++;
	return length;
}

int main()
{
	string in;

	cout << "Word: ";
	cin >> in;
	cout << "Your word lenght is: " << stringlength( in.c_str() ) << endl;

	return 0;
}

No build errors.
Ive not seen it done that way and yeah it works great, i have mine working BUT it returns any name with a 0?
What do you mean 'it returns any name with a 0'? It returned the correct length for me...

Maybe the '0' you're talking about is the program exit code (i.e. the return 0 at the end of main). If your program runs in an external console window, perhaps you should use another 'cin' AFTER printing the length, so that the user has time to see the result before the console shuts down. Then you should see the result.
no your program works fine, the one ive writen dosent come up with errors but if you type in your name (lee, word length is 3) it comes up with 0
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
#include <iostream>
#include <string.h>

using namespace std;

size_t strlen(const char *str);


int main()

{
char anywordstring[100];
int size_t;
cout<<"Please enter anyword\n";
cin.getline(anywordstring,100);


size_t=strlen(anywordstring);

cout<<"You enterd a word" << "\n" <<size_t<< " characters in length\n";
cin.get();
cin.get();
return 0;

}
size_t strlen(const char *str)
{
  int size_t=0;

  return (size_t);
}


this is what i have so far, no errors but the program dosent do what i need it to,im not sure where im going worng
You've defined your own version of strlen, which always returns zero (see lines 28 and 31 in your last post). Can't you just use the "string.h" implementation of strlen, or Duy3's code if you want to make your own?
what is it i would need to alter in the above to get it working?
I would use Duy3's as ive not seen an example like that and its less lines of code but i was attempting to write my own and understand how the functions work, i understand them a bit but obviouslly not enough to get it working lol
id ask my tutor but no offence to him its like asking a choco fire guard lol, its been more of a help on here to be honest :)
Let's analyse what your last posted program is doing. We start at the beginning of main. Memory has already been allocated for your statically-declared char array and int size_t, so the first thing that happens is to print the message and read the input into your char array. I guess you understand that so far...

At line 18, we invoke a function called strlen. Now, NORMALLY, this would invoke the strlen function declared in "string.h" that you have included. In your case, however, you have overridden this function with your own. So, when you call strlen on line 18, it jumps to line 28, sets the local variable size_t to 0, and returns it. It's essentially a useless function, because it always returns 0.

The program then continues to run from line 20.

What do you need to change? If you really want to do the calculation in a separate function, you can 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
#include <iostream>

using namespace std;

// Prototype for your CUSTOM function
unsigned int myStringLengthFunction(const char * str);

int main()

{
	char anywordstring[100];
	int mysize;
	cout<<"Please enter anyword\n";
	cin.getline(anywordstring,100);

	mysize=myStringLengthFunction(anywordstring);

	cout<<"You enterd a word" << "\n" <<mysize<< " characters in length\n";
	cin.get();
	cin.get();
	return 0;

}

// Implementation of your custom string calculation function
unsigned int myStringLengthFunction(const char * str)
{
	return strlen(str);
}


Perhaps this will show you how functions work a bit more. However, be aware that the above code is rather overkill (i.e. you can just call strlen from your main method, you don't need to create your own function which calls it).
aahhh ok, i think i was over complicating it for myself,i kind of started adding other things in and making it less understandable, thanks for that i appreciate the time :), think ill try a few other ways aswell
Topic archived. No new replies allowed.