Solved

string

Posted on 1999-01-08
12
206 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

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…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

770 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