Solved

Strings and Pointers

Posted on 2003-11-14
11
316 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

821 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