Function to delete space and to add space again...

Discussion in 'C' started by back from retirement, Dec 3, 2008.

  1. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    I was trying to generate a function that will delete all the spaces from a given passage. I was successful....my program may look a bit clumsy....but it is working on pretty fine....

    Code:
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    #include<malloc.h>
    
    char *spacedel(char *a)
    {
    	char *b;
    	int i, j, c=0, l=strlen(a);
    	for(i=0;i<l;i++)
    	{
    		if(*(a+i)==' ')
    			c++;
    	}
    	b=(char *)malloc((l-c)*sizeof(char));
    	i=0;
    	j=0;
    	while(i<l && j<(l-c))
    	{
    		if(*(a+i)==' ')
    			i++;
    		else
    		{
    			*(b+j)=*(a+i);
    			i++;
    			j++;
    		}
    	}
    	return(b);
    }
    
    void main()
    {
    	char a[]="C is the philosophy of life.";
    	char *b;
    	b=spacedel(a);
    	printf("\n%s", b);
    	getch();
    }
    
    Immediately after this I was given an assignment to retreat the sentence, I mean say the sentence initially given was...

    Code:
    C is the philosophy of life.
    
    After running through my program it becomes...
    Code:
    Cisthephilosophyoflife.
    
    Now, I have to retain it to its original form.....with the use of another function....

    How on the earth am I gonna do that??????? :confused:
    Please help....I am getting confused even in tracing out my process....
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Not possible unless you have a dictionary. Everything is not irreversible.
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Will the call to the new function always be preceded by a call to the existing function?
    If so then you can reverse the process by storing a list of locations where you removed spaces from.

    Otherwise Shabbir's correct; if you're just writing a new program to get "C is the..." out of "Cisthephilosophyoflife." then you need to know somehow that C, is, the etc are words that would normally be followed by spaces, and this is further complicated by the fact that "I" is a word, so you could end up well confused if you start decoding the string as "C I ". So you will need some way of determining how long a word is. Also you'd need to make sure that "life" isn't followed by a space.
     
  4. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    Yes....the new function call will be preceded by the spacedel one....

    Please tell me how to proceed then....
     
  5. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Store the position of the spaces in some array and introduce them back.

    The other way is store the initial one and then return when called by second function :lol:
     
  6. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    Excuse me.....how will I store spaces in an array????

    like this??

    Code:
    for(i=0;i<5;i++)
    a[i]=" ";
    
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    How will that help restore the original array?
    Can you think of something else you might store instead of the spaces themselves?
    Actually I already gave you one possibility in my previous post but you missed it.
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    No just the index.

    Say an Array of your string is

    arrStrSrc[] = "This is the Array"
    arrStrDest[] = "ThisistheArray"
    arrSpaceLoc[] = {4,7,11}

    Remember to be consistent in storing the location of Src or Destination.
     
  9. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    Yeah....and that can be done while I'm sliding the pointer along the given string at the first time...
    I can introduce a counter....
    Whenever the pointer experiences a space, the current value of the counter will be stored in another array....that will give the locations of the space....

    That fixes it much....I hope....

    One more query, what if I have to write a program which contains multiple spaces....and I have to reduce them in single space....

    e.g. Consider the following sentence...
    Code:
    C  is the    philosophy  of     life.
    
    And I have to write it as....
    Code:
    C is the philosophy of life.
    
    What program should I write?? Please give hint....

    Of course, I have to write a function....and pass that sentence in it to get that output....
     
    Last edited: Dec 5, 2008
  10. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Replace all double space into single space and loop till you can find double space at least once.
     
  11. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    But Shabbir....not only double spaces....there are triple or more as well....should I have to carry on looping???? It seems a bit difficult....but I will try then....
    Actually, I am just a beginner... :D
     
  12. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Looping would remove all the spaces. By looping I meant double loops which converts each double space into single one.

    Here is the test run

    Code:
    Do   you   know why   History repeats itself ???
    
    Do  you  know why  History repeats itself ???
    
    Do you know why History repeats itself ???
     
  13. back from retirement

    back from retirement New Member

    Joined:
    Nov 9, 2008
    Messages:
    72
    Likes Received:
    1
    Trophy Points:
    0
    Occupation:
    Student, UG 1st Yr., Jadavpur University
    Location:
    Uttarpara, West Bengal, India
    I guess....problem fully solved....thanks immensely....
     
  14. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    Add to the reputation of users whom you think helped you in solving it.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice