Solved

string

Posted on 1999-01-08
12
207 Views
Last Modified: 2010-04-15
Write a program to remove all the embedded blanks (spaces) given a string
0
Comment
Question by:samuelccs
  • 4
  • 3
  • 3
  • +2
12 Comments
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255616
#include <stdio.h>
#include <string.h>

void remove_spaces(char *s) {
int j=0;
for(int i=0;s[i]!=0;i++)
  if(s[i]!=' ') s[j++]=s[i];
s[j]=0;
}

void main() {
char *s="The quick brown fox jumped over the lazy dogs";
remove_spaces(s);
printf(s);
}

0
 
LVL 14

Expert Comment

by:AlexVirochovsky
ID: 1255617
Seems as homework! Scrapdog, forbidden of roules!
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255618
This is a useful function, not necessarily homework.  But after looking at the nature of his other questions, it is obvious that it is homework.
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 11

Expert Comment

by:alexo
ID: 1255619
Scrappy, any one liner in the form of "write a program to..." is obviously homework.
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1255620
Want me to delete these questions anyone?
0
 

Accepted Solution

by:
bcn earned 20 total points
ID: 1255621
I think you want is:

string source:    "    The quick brown fox jumped over the lazy dogs             "

string dst: "The quick brown fox jumped over the lazy dogs"

The function is:

char *Trim(char *str)
{
int     r;
char    *s;

    for(r=strlen(str) - 1; (str[r] == ' ') && (r != 0); r--);

    str[r + 1] = '\0';
   
    s = strdup(str);

    while (*s == ' ')
        *s++;

    strcpy(str,s);

    return str;
}




0
 
LVL 11

Expert Comment

by:alexo
ID: 1255622
Clueless newbie.
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255623
All of us were newbies once...but some of us are clueless forever!
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1255624
Hehehe, I can name some clueless forever users... One _starts_ *with* "an" 'O,' !ends¡ "with" an _a_, and "has" some 't's' in "the" _middle_.  Who is it?
0
 
LVL 5

Expert Comment

by:scrapdog
ID: 1255625
more clues please
0
 
LVL 11

Expert Comment

by:alexo
ID: 1255626
>> more clues please
An orphan operating system that rhymes with "clue"
0
 
LVL 6

Expert Comment

by:thresher_shark
ID: 1255627
/Yeah/, ...and... "it"--_likes_ BETA 'video' *tapes*!
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.

790 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