Solved

Strings and Pointers

Posted on 2003-11-14
11
311 Views
Last Modified: 2010-04-01
Hello all!  I am trying to create a program using strings and pointers.  I want the program to ask the user to input a string.  There should be two functions using strlen.  The first array should use array subscripting and the second version should use pointers and pointer arithmetic.  How can I get started?
0
Comment
Question by:marcypark
11 Comments
 
LVL 6

Accepted Solution

by:
GloomyFriar earned 250 total points
ID: 9747122
char string[256];
char *pointer;

pointer = &string[0];

// use array subscripting
for(i=0; i<255; i++)
{
   string[i] = (char)i;
}

//use pointers
for(i=0; i<255; i++)
{
   *pointer  = (char)i;
   pointer++;
}
0
 
LVL 11

Assisted Solution

by:bcladd
bcladd earned 250 total points
ID: 9747598
GloomyFriar's got a good place for you to start. Only suggestion I would have is that you allocate two strings and you don't put '\0' in as the first character if you plan to do anything with the string.

I will give you a slightly different example:

#include <cstring> // gives us std::strlen, std::strcat, etc
using namespace std;

char firstLine[80], secondLine[80];
char * ptr;

strcpy(firstLine, "This is a test of the emergency broadcast system");
strcpy(secondLine, "The quick brown fox jumped over the lazy dog");

/* Now firstLine has the given string and is NULL terminated: The last character is
    '\0' so that strlen and the other C-string handling routines will not see it as
   empty or as run on. Using fixed size buffers for user input is frought with
   danger. See below.
*/

for (int i = 0; i < strlen(firstLine); ++i) {
    cout << firstLine[i] << endl;
}

ptr = secondLine;
while (*ptr != '\0') {
    cout << *ptr << endl;
}


The output of this snippet of code is the contents of the two strings, one character per line. The first loop indexes the array the second uses a char pointer into the array to traverse the string.


Note that if you are using C++ I would urge you to consider using the strd:: string class as it can handle variable lengths of strings, it can be converted to a C-style string at need, and it supports some intuitive operators such as assignment (without using strcpy) and concatenation (addition).

Hope this helps, -bcl
0
 
LVL 3

Expert Comment

by:Sandra-24
ID: 9749795
You forgot to increment the pointer.

while (*ptr != '\0') {
    cout << *ptr++ << endl;
}
0
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 11

Expert Comment

by:bcladd
ID: 9749816
Roger that. Oops

-bcl
0
 

Author Comment

by:marcypark
ID: 9750207
Here is what I have sp far, but the program still does not work right.  It compiles, but just does not work.


#include <iostream>

using std::cin;
using std::cout;
using std::endl;

#include <cstring>
using namespace std;



int main()
{

char string[256];

char *pointer;

char firstLine[100], secondLine[100];

char * ptr;



cout << "Enter a string: ";

cin >> string;



for (int i = 0; i < strlen(firstLine); ++i) {

    cout << firstLine[i] << endl;

}



ptr = secondLine;

while (*ptr != '\0') {

    cout << "According to stringLength1 the string length is: " << *ptr++ << endl;

}

pointer = &string[0];

// use array subscripting

for(i=0; i<255; i++)

{

   string[i] = (char)i;

}



//use pointers

for(i=0; i<255; i++)
{

   *pointer  = (char)i;

   pointer++;

   cout << "According to stringLength2 the string length is: " << *pointer++ << endl;

}

cout << "Hit any key to end. " << endl;

   int any;

   cin >> any;

return 0;

}
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9750936
#include <iostream>

using std::cin;
using std::cout;
using std::endl;

#include <cstring>
using namespace std;



int main()
{

char string[256];

char *pointer;

char firstLine[100], secondLine[100];

char * ptr;



cout << "Enter a string: ";

cin >> string;


### Question: Where is the information entered by the user? It is in string, right?
### BUT the loop following prints out the characters in firstLine...bet you should
### change string in the previous line with firstLine and you should be fine (or at
### least you should get something).

for (int i = 0; i < strlen(firstLine); ++i) {

    cout << firstLine[i] << endl;

}

### Again, what is inside secondLine? Nothing that you put there. Thus it contains random junk
### and should not be used. Try setting ptr to firstLine also, this isn't calcluating a length.

ptr = secondLine;

while (*ptr != '\0') {

    cout << "According to stringLength1 the string length is: " << *ptr++ << endl;

}
### I guess that you've done the work before you get here. Or rather the previous and following sections
### of the program seem to do the same thing. You need to work on your understanding of the flow of
### control in program (I am just guessing there).

pointer = &string[0];

// use array subscripting

for(i=0; i<255; i++)

{

   string[i] = (char)i;

}



//use pointers

for(i=0; i<255; i++)
{

   *pointer  = (char)i;

   pointer++;

   cout << "According to stringLength2 the string length is: " << *pointer++ << endl;

}

cout << "Hit any key to end. " << endl;

   int any;

   cin >> any;

return 0;

}

Hope this helps, -bcl
0
 
LVL 3

Expert Comment

by:Shay050799
ID: 9751902
that is homework !!!
0
 

Author Comment

by:marcypark
ID: 9756501
Can someone please help me?
0
 
LVL 11

Expert Comment

by:bcladd
ID: 9756679
I gave you feedback on your code. You say "it doesn't work". If you expect help you should give us some feel for what you expected, what you got, and how you have worked to find a solution to your problem.

-bcl
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249196
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Split: GloomyFriar {http:#9747122} & bcladd {http:#9747598}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

VMware Disaster Recovery and Data Protection

In this expert guide, you’ll learn about the components of a Modern Data Center. You will use cases for the value-added capabilities of Veeam®, including combining backup and replication for VMware disaster recovery and using replication for data center migration.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question