可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I have this string
"go for goa"
and the output should be
"go for goa"
I want to remove the extra spaces. That means two or more consecutive spaces should be replaced with one space. I want to do it using an in place algorithm.
Below is the code I tried but it doesn't work:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Function to remove spaces in an string array */
char *removeSpaces(char *str) {
int ip_ind = 1;
/* In place removal of duplicate spaces*/
while(*(str + ip_ind)) {
if ((*(str + ip_ind) == *(str + ip_ind - 1)) && (*(str + ip_ind)==' ')) {
*(str_ip_ind-1)= *(str + ip_ind);
}
ip_ind++;
}
/* After above step add end of string*/
*(str + ip_ind) = '\0';
return str;
}
/* Driver program to test removeSpaces */
int main() {
char str[] = "go for go";
printf("%s", removeSpaces(str));
getchar();
return 0;
}
回答1:
Most solutions seem needlessly complicated:
#include <ctype.h>
#include <stdio.h>
void strip_extra_spaces(char* str) {
int i, x;
for(i=x=0; str[i]; ++i)
if(!isspace(str[i]) || (i > 0 && !isspace(str[i-1])))
str[x++] = str[i];
str[x] = '\0';
}
int main(int argc, char* argv[]) {
char str[] = " If you gaze into the abyss, the abyss gazes also into you. ";
strip_extra_spaces(str);
printf("%s\n",str);
return 0;
}
回答2:
I can't even tell what your function is trying to do. For one thing, the first time through, that -1 will access before the start of the string. Try something like:
char *removeSpaces(char *str) {
char *inp = str, *outp = str;
int prevSpace = 0;
while (*inp) {
if (isspace(*inp)) {
if (!prevSpace) {
*outp++ = ' ';
prevSpace = 1;
}
} else {
*outp++ = *inp;
prevSpace = 0;
}
++inp;
}
*outp = '\0';
return str;
}
回答3:
You aren''t checking for a space.. You are checking for a tab. Replace \t with (I mean space..)
回答4:
Your if
condition doesn't work. I will show you my code. It is similar to yours, by just using two pointers: back
and front
.
If front
is not a space, or front
is a space but back
is not a space, you need to copy front
to back+1
.
char *removeSpaces(char *str)
{
if (*str == '\0') return str;
char *back = str;
char *front = str + 1;
/* In place removal of duplicate spaces*/
while(*front != '\0')
{
if (*front != ' ' || *back != ' ') // highlight
*(++back) = *front;
front++;
}
/* After above step add end of string*/
*(back + 1) = '\0';
return str;
}
Hope this may help you.
回答5:
There you go:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* Function to remove spaces in an string array */
char *removeSpaces(char *str) {
int ip_ind = 0;
while (*(str+ip_ind) != 0) {
if (*(str + ip_ind++) == 32) {
if (*(str + ip_ind) == 32) {
int x=ip_ind;
while (*(str + x +1) != 0) {
*(str + x)= *(str + 1 + x++);
}
*(str + x)= 0;
--ip_ind;
}
}
}
return str;
}
/* Driver program to test removeSpaces */
int main() {
char str[] = "go for go";
printf("%s\n", str);
printf("%s\n", removeSpaces(str));
char str2[] = "go for go for go";
printf("%s\n", str2);
printf("%s\n", removeSpaces(str2));
return 0;
}
Output:
dda$ ./a.out
go for go
go for go
go for go for go
go for go for go
dda$
回答6:
The problem:
- You are tracking the '\t' tab, but you have to remove the
spaces.
- Also, you are tracking and finding the '\t', but you have no steps to removing it (I added one).
- Every-time you mustn't increment the
ip_ind
, you must inc, only when deletion is not done, because when deletion is done, that
scenario will be equal to incremented.
- Your scenario will fail for
""
, to avoid it, add a check parameter as below (Method 1) or else start from ip_ind
from 0 as in (Method 2). (Thanks to @Lee Daniel Crocker)
Solution:
You could try like this.,
Method 1 : ip_ind starts from one.
/* Function to remove spaces in an string array */
char *removeSpaces(char *str)
{
int ip_ind = 1;
char *ptr;
if(*str)
return str;
/* In place removal of duplicate spaces*/
while(*(str + ip_ind))
{
if ( (*(str + ip_ind) == *(str + ip_ind - 1)) && (*(str + ip_ind)==' ') )
{
ptr = str + ip_ind;
//Functionality for removal of spaces.
do{
*(ptr-1) = *ptr;
}while(*ptr++ != '\0');
}
else //Inc only if deletion is not done.
ip_ind++;
}
/* After above step add end of string*/
*(str + ip_ind) = '\0';
return str;
}
Method 2 : ip_ind starts from zero.
char *removeSpaces(char *str)
{
int ip_ind = 0;
char *ptr;
/* In place removal of duplicate spaces*/
while(*(str + ip_ind))
{
if ( (*(str + ip_ind) == *(str + ip_ind + 1)) && (*(str + ip_ind)==' ') )
{
ptr = str + ip_ind+1;
do{
*(ptr-1) = *ptr;
}while(*ptr++ != '\0');
}
else
ip_ind++;
}
/* After above step add end of string*/
*(str + ip_ind) = '\0';
return str;
}
回答7:
Simple one for You
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char a[100];
int i,t,k;
scanf("%[^\n]s",a);
t=strlen(a);
for(i=0;i<t;)
{
if((a[i]==' ')&&(a[i+1]==' '))
{
for(k=i+1;k<t;k++)
{
a[k]=a[k+1];
}
t=t-1;
}
else
i++;
}
printf("%s",a);
return 0;
}