?
Solved

Strings and Pointers

Posted on 2003-11-14
11
Medium Priority
?
334 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
11 Comments
 
LVL 6

Accepted Solution

by:
GloomyFriar earned 1000 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 1000 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering 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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

777 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