Arrays and Pointers

I have to write two versions of this function and use array subscripting and pointers and pointer arithmetic.

int stringLength(const char* s);

Determines the length of string s. The number of characters preceding the terminating null character is returned.

I have no idea where to even begin...can someone lead me in the right direction to start this assignment?
"The number of characters preceding the terminating null character is returned."

How would you check the first character in the string?
How would you check the second character in the string?
I have come up with the following code but really have no idea if it's even close to what I'm supposed to be doing.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
int stringlength(char *str)
{
 int counter = 0;
 while(str[counter] != '\0')
          counter++;
 return counter;
}
int main()
{
   char s1[ 100 ];
   char* s2 = "education";
   char* s3 = "school";

   cout << "stringLength(" << s2 << "): "
        << stringLength1( s2 ) << endl;
   cout << "stringLength(" << s3 << "): "
        << stringLength2( s3 ) << endl;
But pointer arithmetic would be more like:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19

#include <iostream>
using namespace std;
int stringLenght(const char*);

int main(){

   char *s = "hello pointer\n"; // You need to end it with a null character.
   cout<<"the lengh is: "<<stringLenght(s)<<endl;
   return 0;
}

int stringLenght(const char* s){

   int c=0;
   for(; *(s+c)!='\n'; c++){}
   return c;
}


I'm using the for loop that way, because I'm used to...
Last edited on
I came up with this code but I am getting several 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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
using namespace std;


int string_length_1(char* s)
{
     int size = 0;

     while(*s != NULL)
     {
          //increment the pointer
          s++;
          size++;
     }

     return size;
}

//Same function using array subscripts
int string_length_2(char s[])
{
     int index = 0;

     //using array subscript
     while(s[index] != NULL)
     {
          index++;
     }

     return --index;
}


int main()
{
	int stringLength1( char *s );
int stringLength2(char *s );

   char s1[ 100 ];
   char* s2 = "education";
   char* s3 = "school";

   cout << "stringLength(" << s2 << "): "
        << stringLength1( s2 ) << endl;
   cout << "stringLength(" << s3 << "): "
        << stringLength2( s3 ) << endl;

  
 
   system("pause");
   return 0; // indicates successful termination
} // end main 
If you're using this function name: string_length_1, then why are you creating a prototype of a function that doesn't exist: stringLength?

From Internet:
Having the prototype available before the first use of the function allows the compiler to check that the correct number and type of arguments are used in the function call and that the returned value, if any, is being used reasonably.


Also, I put those prototypes just to be able to define the function after the main() function, but if you're defining them before the main function, you don't need a prototype function.

So here are the problems of your code:
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
#include <iostream>
#include<cstdlib> // <-- You need this to use system()
using namespace std;


int string_length_1(char* s)
{
     int size = 0;

     while(*s != NULL)
     {
          s++;
          size++;
     }

     return size;
}

int string_length_2(char s[])
{
     int index = 0;

     while(s[index] != NULL)
     {
          index++;
     }

     //return --index;//<-- Why are you putting those minus sings here?
     return index; //<-- Much better
}


int main()
{
        //int stringLength1( char *s );<-- This shouldn't be here
        //int stringLength2(char *s ); <--This neither.

   char s1[ 100 ];
   char* s2 = "education";
   char* s3 = "school";

   cout << "stringLength(" << s2 << "): " //<-- You can use just one cout if you want.
        << stringLength1( s2 ) << endl
        << "stringLength(" << s3 << "): "
        << stringLength2( s3 ) << endl;

  
 
   system("pause");
   return 0; 
} 
Last edited on
closed account (D80DSL3A)
lbgladson The code you posted (in your 2nd post) is nearly correct. You just have some function name issues. After choosing the same name for the function throughout = stringLength I get:
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
#include <iostream>
using namespace std;

int stringLength(char *str)
{
 int counter = 0;
 while(str[counter] != '\0')
          counter++;
 return counter;
}

int main ()
{
    char s1[ 100 ];
   char* s2 = "education";
   char* s3 = "school";

   cout << "stringLength(" << s2 << "): "
        << stringLength( s2 ) << endl;
   cout << "stringLength(" << s3 << "): "
        << stringLength( s3 ) << endl;

    system("pause");// if you must!
    return 0;
}

I got some warnings:

C:\main.cpp||In function 'int main()':|
C:\main.cpp|19|warning: deprecated conversion from string constant to 'char*'|
C:\main.cpp|20|warning: deprecated conversion from string constant to 'char*'|
C:\main.cpp|18|warning: unused variable 's1'|
||=== Build finished: 0 errors, 3 warnings ===|

But the program output is fine:

stringLength(education): 9
stringLength(school): 6

I found that changing the char*'s to const char*'s (in function too) eliminates the 1st 2 warnings. Remove the unused variable s1 from main() and it's perfect!

EDIT: Noticed you need 2 versions. Replace str[counter] with
*(str+counter) for that.
Last edited on
Topic archived. No new replies allowed.