need more help with pointers and strings

This is a discussion on need more help with pointers and strings within the C++ Programming forums, part of the General Programming Boards category; If anyone has any free time whatsoever, I would love to email them my short program. I'm having a ton ...

  1. #1
    Registered User
    Join Date
    Mar 2002
    Posts
    8

    need more help with pointers and strings

    If anyone has any free time whatsoever, I would love to email them my short program. I'm having a ton of trouble getting my strings and pointers to agree. I'll be glad to email them my goal for the project, and my code...and I'm hoping someone can email me back showing me my problems (there may be a ton of them). I need someone to help point me on the right track.

    So if you understand pointers and strings (This is my second class of c++ programming, so it shouldn't be difficult) and have some free time, please drop me a line.

    I've written it in visual c++ v6.0 but I can just copy and paste into the email if you'd like.

    Much thanks in advance,

    Dave
    bgbfflochp@aol.com

  2. #2
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    I'll be glad to email them my goal for the project, and my code...and I'm hoping someone can email me back showing me my problems
    Post your code here, the point of this message board is so everyone can benifit from the answers your given not just you.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  3. #3
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    Ok, I just figured it would take up too much space on the message boards.

    The goal of this project is to test a sentence (inputted by the user) to be a palendrome or not. A palendrome is a sentence that says the same thing when it is reversed...kind of like radar is radar backwards.

    I am to use strings and pointers. I declare a string of 80 characters, and after that, I must use pointer notation...no string1[i]. So it would be (pointer1 + i)...i think.

    Any how...here is my code:

    my driver:

    #include <iostream.h>
    #include <stdio.h>
    #include <string.h>
    #include "palendrome.h"

    int palendrome(char *); // prototype

    void main(void) {
    char string1[80];
    char string2[80];
    char *pointer1, *pointer2;
    pointer1 = string1;
    pointer2 = string2;
    int i;
    int j;

    cout << "Input the sentence to be tested for a palendrome: " << endl;
    gets(pointer1);


    // the following for loop will load all char values from the inputted sentence into another string.

    for(i=0,j=0;i<80; i++ ) {
    if(char isalpha(pointer1 + i)) {
    (pointer2 + j) = (pointer1 + j);
    j++;
    }
    else
    j++;
    }

    if(palendrome(pointer2)){
    cout << "Yes, this is a palendrome " << endl;
    }
    else {
    cout << "No, this is not a palendrome " << endl;

    }
    }


    here is my header file: function palendrome:

    int palendrome(char *pointer2) {

    char *pointer1;
    char stringb[80];
    pointer1 = stringb;
    int s=0;
    int r=0;
    int p;


    // the following for loop will create another string with the same contents as the string inputted by the user

    for(p=0; p<80; p++) {
    (pointer1 + p) = (pointer2 + p);
    }

    while((pointer1 + s) != '/0') { // determining how many chars i have in the string
    s++;
    }
    s--;

    while((pointer2 + r) == (pointer1 + s)) {
    if(r != s) {
    r++;
    s--;
    }
    else {
    return 1;
    }
    }

    return 0;
    }

  4. #4
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    void main(void)
    WWWOOOHHHH void main alert.

    bgbfflochp it should be int main(void).

    an example of a palindrome function using pointers can be found Here
    Last edited by C_Coder; 03-17-2002 at 06:41 AM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  5. #5
    Registered User
    Join Date
    Mar 2002
    Posts
    8

    still having problems.

    I changed my header file to the code in the link to see if it would even work.

    Using my same driver as posted above...and the following new header file:

    int palindrome(char *text) {
    char *start = text;
    char *end = text +strlen(text) -1;

    while( end > start) {
    if( tolower(*start) != tolower(*end)) {
    return 0;
    }
    start++;
    end--;
    }
    return 1;
    }

    I'm getting the following errors using Visual C++ v6.0

    error 1 'tolower' undeclared identifier;
    error 2 'initializing': cannot convert from 'char *' to 'char'
    which is taking place in the driver posted previously in the thread on the 'isalpha' line..."if(char isalpha(pointer1 + i)) {"

    error 3 '=': left operand must be l-value
    which is taking place in the driver on the line under the isalpha line..."(pointer2 + j) = (pointer1 + j);"

    any ideas? i've got a headache.

    Much thanks in advance,
    Dave

  6. #6
    Registered User C_Coder's Avatar
    Join Date
    Oct 2001
    Posts
    523
    Code:
    #include <iostream.h> 
    #include <stdio.h> 
    #include <string.h> 
    #include <ctype.h>
    
    int palendrome(char *text); // prototype 
    
    int main(void) 
    { 
    	char string[80];   
    
    	cout << "Input the sentence to be tested for a palendrome: " << endl; 
    	fgets(string, sizeof(string), stdin);  
            string[ strlen(string)-1 ] = '\0';
    
    	if(palendrome(string)) 
    		cout << "Yes, this is a palendrome " << endl;
    	else 
    		cout << "No, this is not a palendrome " << endl;
    	
    	return 0; 
    } 
    
    int palendrome(char *text)  
    {
       char* start = text;
       char* end = text + strlen(text) - 1;
       while(end > start)
       {
           if(tolower(*start) != tolower(*end))
    	   {
               return(0);
    	   }
           end--;
           start++;
       }
       return(1);
    }
    I changed your code a bit.
    You don't need 2 arrays and you can use the array name as a pointer.
    fgets(string, sizeof(string), stdin);
    never use gets() as it allows the user enter more characters than can be stored in your array, fgets limits the maximum number of characters that can be read ie sizeof(string).
    string[ strlen(string)-1 ] = '\0';
    When using fgets you need to remember that the newline isnt replaced with a null, its added to the end of the string. This line overwrites the newline with a null. If you comment this line out you will see the program won't work
    Last edited by C_Coder; 03-17-2002 at 08:13 AM.
    All spelling mistakes, syntatical errors and stupid comments are intentional.

  7. #7
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    First off, I would like to thank you for all the help so far...you've definitely helped me figure out how some things work/don't work.
    I appreciate it.



    code:

    string[strlen(string)-1] = '\0';

    ----------------------------------------------

    say I declared:

    char string[80];
    char *pointer;
    pointer = string;

    instead of using the line above for the null character, how would I write it using a pointer?

  8. #8
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    *(pointer1 + strlen(string) -1) = '\0';

    I got that line of code to work.

    One more question though:

    code:

    fgets(string, sizeof(string), stdin);

    ----------------------------------------------------

    I'm not really sure what the stdin parameter is...can you please explain a little?

    Thanks in advance

  9. #9
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    one last problem (I hope)...

    I need to check the string for char values and put them into another string...that away I don't have any ints, doubles, etc...should the user type them in as part of their input.

    So,
    I tried the following code:

    for(i=0, j=0; i<80; i++ ) {
    if(char isalpha(*(pointer1 + i))) { // pointer1 being the inputted string from user.
    (pointer2 + j) = (pointer1 + j); // pointer2 being my 2nd string that I'm loading my char values into
    j++;
    }
    else
    j++;
    }

    ----------------end of code---------------------
    I'm getting an error on the '(pointer2 + j) = (pointer1 + j); ' line.

    It's saying '='; left operand must be an l-value.
    Any ideas?

  10. #10
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    stdin is short for standard input I believe. In its use here it means get the input from the keyboard. There is also stdout (standard output), stderr (standard error, i.e. non-redirectable output), and I think also stdprn (standard printer) and stdaux (standard auxillary?). These parameters are primarily used in conjunction with the fprintf, fscanf, and fgets type of functions.

    You don't really see these used much in C++ anymore, mostly just in C style code. With C++ streams, these concepts are somewhat mimiced/replaced by the use of cin, cout, and cerr.

    I could be wrong, but those are my thoughts on the matter.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  11. #11
    Registered User
    Join Date
    Mar 2002
    Posts
    8
    ok, so fgets() parameters are:

    fgets(string for the input, the limit for amount of input, input/output...etc...) ?

    I'm sorry I couldn't word that any better.


    PS, hk_mp5kpdw...your name have anything to do with airsoft?

    Dave

  12. #12
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    For functions that get data, like fgets and fscanf, from the user you will pretty much always be using either stdin or some filepointer. For functions that output data, like fprintf and fputs, you will be using filepointers or stdout, stderr and stdprn.

    The name has less to do with AirSoft and the replica BB guns they make and more to do with the real-life MP5K PDW SMG (sub-machine gun) made by the Heckler & Koch (HK) company. It is my dream gun. I would buy one if only they didn't cost $9000+ and if I didn't have to go through all of that damn paperwork with the BATF and wait 6 months for the background check just to get the stupid transfer tax stamp. If it wasn't for all that I'd snap one up in a jiffy.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with calloc and pointers to strings.
    By sweetarg in forum C Programming
    Replies: 1
    Last Post: 10-24-2005, 02:28 PM
  2. Arrays, pointers and strings
    By Apropos in forum C++ Programming
    Replies: 12
    Last Post: 03-21-2005, 10:25 PM
  3. Concatenating strings (dynamic array using pointers)
    By Tankndozer in forum C Programming
    Replies: 8
    Last Post: 07-01-2004, 07:27 AM
  4. hangman need help with strings and pointers
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 08:13 AM
  5. Pointers to pointers to strings
    By Natase in forum C Programming
    Replies: 2
    Last Post: 09-17-2001, 11:30 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21