给定字符串(ASCII码0-255)数组,请在不开辟额外空间的情况下删除开始和结尾处的空格,并将中间的多个连续的空格合并成一个。例如:"
i am a little boy. ",变成"i am a little
boy",语言不限,但不要用伪代码作答,函数输入输出请参考如下的函数原型:
C++函数原型: void FormatString(char str[],int len){ }
public class ClearSpace { public static void main(String[] args) { String st = " i am a little boy. "; char[] str = st.toCharArray(); int i=0,j=0; while(str[i]==' '){ i++; } while(i<=str.length-1){ if(str[i]==' '&&(i==str.length-1||str[i+1]==' ')){ //如果中间空格的之后还存在空格的话,或者是结尾的话。 i++; continue; } str[j++]=str[i++]; } for(int o=0;o<j;o++){ System.out.print(str[o]); } } }
public class FormatString { public void FormatStringX(char str[],int len){ int index = 0; int currentIndex = 0; // at the beginning index = findPosition(str,0); // begin at the first non-space character while(index <= len - 1){ // copy if (str[index] != ' ') { str[currentIndex++] = str[index++]; } else { index = findPosition(str, index); str[currentIndex++] = ' '; } } str[currentIndex] = '\0'; } public int findPosition(char str[],int index){ while(index < str.length && str[index] == ' '){ index++; } return index; }
#include <iostream> using namespace std; void formatString(char str[],int length) { bool isLastNotSpace=false; int newStrIndex=0; int i=0; while(str[i]!='\0') { if(str[i]!=' ') { str[newStrIndex++]=str[i++]; isLastNotSpace=true; } //如果遍历得到的此字符是空格 else { //前一个遍历得到不是空格,则这个字符也需要保留 if(isLastNotSpace) { str[newStrIndex++]=str[i++]; isLastNotSpace=false; } else { i++; } } } //通过上面的处理,最后一个字符仍然有可能是空格 if(newStrIndex>0 && str[newStrIndex-1]==' ') { str[newStrIndex-1]='\0'; } else { str[newStrIndex]='\0'; } } int main() { char a[]=" i am a little boy. "; cout<<"原字符串:"<<a<<endl; formatString(a,sizeof(a)/sizeof(a[0])); cout<<"新字符串:"<<a<<endl; return 0; }
//#define xFormatString //给定字符串(ASCII码0-255)数组, 优化 #ifdef xFormatString //no other space; void FormatString(char str[],int len) { assert(str !=NULL || len <0 || str[len-1]!='\0'); int i=0 , j=0, k =0; while( str[i] == ' ')i++; //find i; while( str[i] !='\0')//不要随意的使用while(str[i++]) { if (str[i] == ' ' && str[i+1] == ' ' || str[i+1] == '\0')//完善不容易 { i++; continue; } str[j++] = str[i++]; } str[j]='\0'; } int main(){ char a[]=" i am a little boy. "; //char a[]=" begin"; //char a[]="end "; //char *pa=a; unsigned int len=strlen(a); FormatString(a,len); printf("%s\n",a); system("pause"); return 0; } #endif
#include <iostream> #include <stdlib.h> #include <string> #include <cstringt.h> //#include <stdio.h> using namespace std; void FormatString(char str[], int len) { for (int i = 0; i < len-1;i++) { if (str[i]==' '&&str[i+1]==' ') { for (int j = i ; j < len-1;j++) { str[j] = str[j + 1]; } len--; i--; } } if (str[len-1]==' ') { str[len - 1] = '\0'; } if (str[0]==' ') { for (int i = 0; i < len - 1; i++) { str[i] = str[i + 1]; } len--; } } int main() { //char *str=new char[1000]; char str[1000]; //cin >> str; strcpy_s(str, " I am a little boy . "); int len = strlen(str); FormatString(str, len); for (int i = 0; str[i] != '\0';i++) { cout << str[i] ; } //cout << endl; //delete[]str; //str = NULL; system("pause"); return 0; }
public static void FormatString(char[] str,int len){ for(int i=-1,j=0,k=0;i<len;i=j){ for(j=i+1;j<len;j++) if(str[j]==' ') break; for(k=j+1;k<len;k++) if(str[k]!=' ') break; while (j<len&&k<len&&str[k]!=' ') { str[j++] = str[k]; str[k] = ' '; k++; } } for(int i=0;i<str.length;i++){ if(str[i]=='.') break; System.out.print(str[i]); } }
//没有测试用例,也不知道对不对。。。
/**
* Created by huali on 2018/5/26.
*/
public class Main{
// 给定字符串(ASCII码0-255)数组,
// 请在不开辟额外空间的情况下删除开始和结尾处的空格,
// 并将中间的多个连续的空格合并成一个。例如:" i am a little boy. ",
// 变成"i am a little boy",语言不限,但不要用伪代码作答。
public static void main(String[]args)
{
//String str = " i am a little boy. ";
String str = "i am a boy. ";
char [] ch = str.toCharArray();
formatString(ch, ch.length);
//for(int i=0;i<ch.length;i++)
//{
// if(ch[i]==ch[i+1]&&ch[i]==' ')
// break;
// System.out.print(ch[i]);
//}
System.out.println();
}
private static void formatString(char[] ch, int length) {
int count = 0;
char be = ' ';
for(int i=0;i<ch.length;i++)
{
if(ch[i]==' ')
{
be = ' ';
continue;
}
else
{
if(count!=0&&be ==' ')
System.out.print(' ');
char tmp = ch[i];
ch[i] = ch[count];
ch[count] = tmp;
System.out.print(ch[count]);
be = tmp;
count++;
}
}
}
}
import re s=" i am a little boy. " s=s.strip() s=re.sub(' +'," ",s) print s
#include <iostream> #include <cstring> using namespace std; void FormatString(char str[],int len) { if(str==nullptr||len<=0) return; char *p1=str,*p2=str; bool is=false; while(*p1!='\0') { if(*p1!=' ') { if(is) *p2++=' '; *p2++=*p1; } else { if(p2!=str) is=true; } ++p1; } *p2='\0'; } int main() { char str[]=" i am a little boy. "; FormatString(str,strlen(str)); cout<<str<<endl; return 0; }
void FormatString(char str[], int len){ int spaceIndex = 0; int newLen = 0; bool isPevSpace = false; bool isFirstSpace = true; for (int i = 0; i < len; i++) { if (str[i] == ' ') { if (isPevSpace && isFirstSpace) { spaceIndex = i; } else if (isFirstSpace) { continue; } else if (!isPevSpace) { str[spaceIndex++] = ' '; } isPevSpace = true; } else { isFirstSpace = false; str[spaceIndex++] = str[i]; newLen++; isPevSpace = false; } } str[spaceIndex] = '\0'; }