changing from array to pointer format?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mmaria21
    New Member
    • Feb 2010
    • 5

    changing from array to pointer format?

    Ive tried changing it to pointer format but when i do, the new string without spaces doesnt appear. i appreciate the help!


    Code:
    #include <iostream>
    int stripspaces(char str[]);
    using namespace std;
    [LEFT][CENTER][RIGHT][CODE]
    [/RIGHT][/CENTER][/LEFT]

    int main()
    {
    char ar[50];
    cout << "enter a sentence: " <<endl;
    cin.getline(ar, 49);
    cout<<" your sentence has " <<stripspaces(a r)<<" spaces "<<endl;

    return 0;
    }

    int stripspaces (char str[])
    {
    int spacecounter = 0;
    char * p =str;

    while (*p)
    {
    if (*p == ' ')
    spacecounter++;
    p++;
    }
    int i =0;
    char temp[50];
    int count= 0;

    while (str[i])
    {
    if(str[i]!= ' ')
    {
    temp[count] = str[i];
    count++;
    }
    i++;
    }
    temp[count] = '\0';
    cout <<" The new string without spaces is:" <<temp<<endl;
    return spacecounter;
    }[/CODE]
  • newb16
    Contributor
    • Jul 2008
    • 687

    #2
    What doesn't work in the program above and what is the expected result?

    Comment

    • mmaria21
      New Member
      • Feb 2010
      • 5

      #3
      the program above works but its written in array notation but i have to change it to pointer notation and so i tried writing something like char * t= temp; after line 27 so that later i could refer to the values in temp as *(t +i) and such so that i could change to pointer notation. but when i did that the program doesnt output the new string without the spaces(which is what is supposed to output). it does output the second part correctly, which is the part that tells you how many spaces are in the entered string.

      Comment

      • whodgson
        Contributor
        • Jan 2007
        • 542

        #4
        Apart from not understanding how your code keeps the screen open I would have thought that line 7 should be char* ar[50]; etc

        Comment

        • newb16
          Contributor
          • Jul 2008
          • 687

          #5
          I tried writing char *t=temp and replacing temp[count] with *(temp+count) after line 27 and it worked for me - it counts spaces and outputs trimmed string.

          Comment

          • mmaria21
            New Member
            • Feb 2010
            • 5

            #6
            hm.. I guess ill try again then! Thank you! would that be sufficient for the program to be considered in pointer notation?

            Comment

            • whodgson
              Contributor
              • Jan 2007
              • 542

              #7
              Yes.....because thats what it would be using.

              Comment

              Working...