Solved

Strings and Pointers

Posted on 2003-11-14
11
299 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
Comment Utility
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
Comment Utility
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
Comment Utility
You forgot to increment the pointer.

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

Expert Comment

by:bcladd
Comment Utility
Roger that. Oops

-bcl
0
 

Author Comment

by:marcypark
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 11

Expert Comment

by:bcladd
Comment Utility
#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
Comment Utility
that is homework !!!
0
 

Author Comment

by:marcypark
Comment Utility
Can someone please help me?
0
 
LVL 11

Expert Comment

by:bcladd
Comment Utility
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
Comment Utility
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now