I have written this piece of code which removes spaces in string,its working perfectly, but the other thng that I need is to pass string to left trim function and remove spaces, the new string should returned to the main program, the new string should be passed to the right trim function and remove any
spaces at the end of the string. Return the trimmed string. Output the trimmed string. There should be some break point in string like "/" or "*" to divide left and rignt side of string. Could anyone help me with this???Please.Thanks a lot.

here's the code:

#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>

using namespace std;

int main(void)
{
 char s[50];
 cout << "\nEnter String to Remove Spaces from: " << endl;
 gets(s);
 int len = strlen(s),j=0;
 for(int i=0;i<len;i++)
 {
    if(isspace(s[i]))
   {
     j=i;
     for(;j<len-1;j++)
      s[j] = s[j+1];
      
     if(i!=j)
      {
       s[j] = '\0';
       len--;
      }
   }
 }

 cout << "\n\nNew Sting After Removing Spaces: " << s << endl;

 system("pause");
}

If I understand you correctly then you want to remove the spaces in a string in two passes. In one pass you remove the spaces from the left half of the string and then in the second pass you remove the spaces from the right side of the string. The easiest way to do this would be to use the length of the string as the limiting agent by finding the middle of the string. You already have the length of the string, variable len, so why not use a middle variable? int middle = len / 2; Then loop one side until the middle, then the next side from the middle to the end.

You mention something about a breaking point string, but I'm not too sure what you're getting at there. If you need to stop at a specific character then I suppose you can put that right into your loop.

yeah, thats exactly what i am trying to do, its sounds simple, but the problem is i don't really know how to implement it in the code itself. Could you provide me with some code, please. Thanks.

yeah, thats exactly what i am trying to do, its sounds simple, but the problem is i don't really know how to implement it in the code itself. Could you provide me with some code, please. Thanks.

Method 1 is to use a "middle" variable.

#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>

using namespace std;

int main(void)
{
 char s[50];
 cout << "\nEnter String to Remove Spaces from: " << endl;
 gets(s);
 int len = strlen(s),j=0;
 int middle = len / 2;

 //Clear left side only
 for(int i = 0 ; i < middle; i++)
 {
    if(isspace(s[i]))
   {
     j=i;
     for(;j<len-1;j++)
      s[j] = s[j+1];

     if(i!=j)
      {
       s[j] = '\0';
       len--;
      }
   }
 }

 //Clear right ride
 for(int i = middle ; i < len; i++)
 {
    if(isspace(s[i]))
   {
     j=i;
     for(;j<len-1;j++)
      s[j] = s[j+1];

     if(i!=j)
      {
       s[j] = '\0';
       len--;
      }
   }
 }

 cout << "\n\nNew Sting After Removing Spaces: " << s << endl;

 cout << "Program paused...";
 cin.get();

 return 0;
}

Note that I replaced system("pause"); with cin.get() because I'm using Unix and I wanted a platform independent way of pausing the program.

This one works, not super elegant, but it works. You'll have to eliminate your marker if you want.

#include <iostream>
#include <iomanip>
#include <string>
#include <stdio.h>

using namespace std;

int main(void)
{
 char s[50];
 cout << "\nEnter String to Remove Spaces from: " << endl;
 gets(s);
 int len = strlen(s),j=0;

//This will keep track of where we are
 int currentPosition = 0;

 //Clear left side only, stop at a '*' character
 for(int i = 0 ; i < len ; i++)
 {
     //Declared before the for loop so it will persist after loop ends
     currentPosition = i;
     
    if(isspace(s[i]))
   {
     j=i;
     for(;j<len-1;j++)
      s[j] = s[j+1];

     if(i!=j)
      {
       s[j] = '\0';
       len--;
      }

     if (s[i] == '*')
     {
         break;
     }
   }
 }

 //Clear right ride from the '*' character
 for(int i = currentPosition ; i < len; i++)
 {
    if(isspace(s[i]))
   {
     j=i;
     for(;j<len-1;j++)
      s[j] = s[j+1];

     if(i!=j)
      {
       s[j] = '\0';
       len--;
      }
   }
 }

 cout << "\n\nNew Sting After Removing Spaces: " << s << endl;

 cout << "Program paused...";
 cin.get();

 return 0;
}

thanks a lot everyone, it was really good help provided.cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.