Solved

Strings and Pointers

Posted on 2003-11-14
11
323 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
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 be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

737 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