1. 程式人生 > >C++去除字串中多餘的空格

C++去除字串中多餘的空格

今天寫了個小程式設計,去除字串中多餘的空格,例如"I___am_____a______student."(下劃線表示空格哈,打多個空格顯示的還是一個),最後輸出”I am a student.”

現在把自己的思路貼上,歡迎大家留言指正。

思路就是定義兩個指標next和tail,一個在前面尋找非空格的字元,另外一個在後面一步一步移動,把後面的字元全部轉移到前面來;然後為了去除多餘的空格,也就是有多個或者一個空格的地方要留一個空格。

#include <iostream>
using namespace std;

char * deleteSpace(char * str){
    char
* tail = str; char * next = str; while(*next){ // 兩個if可以合併到一塊兒,這樣寫只是為了看著方便 if(*next != ' '){ // 查詢不是空格的字元 *tail = *next; tail++; } if(*next == ' ' && *(next-1) != ' '){ // 只留一個空格的判斷條件;當前字元為空格且前一個不為空格。 *tail = *next; tail++; } next++; } *tail='\0'
; // 字串結束 return str; } int main(){ char str[] = "i am a student."; char *res = deleteSpace(str); cout << res << endl; return 0; }

相關推薦

C++去除字串多餘空格

今天寫了個小程式設計,去除字串中多餘的空格,例如"I___am_____a______student."(下劃線表示空格哈,打多個空格顯示的還是一個),最後輸出”I am a student.” 現在把自己的思路貼上,歡迎大家留言指正。 思路就是定義兩個指標

java去除字串多餘空格,只留一個

正則表示式: string.replaceAll(" {2,}", " ") ;自己寫方法: StringBuffer sb=new StringBuffer();//用其他方法實現 int flag; for(int i=0;i&l

c++ 去除字串空格和標點符號 (remove_if 函式的用法)

C++中提供了自動刪除空格和標點符號的函式,使用如下: #include <ctype.h> #include <algorithm> str_testing.erase(

centos 7 使用sed命令去除字串空格(可用於去除檔名的空格

1.刪除字串行首空格(刪除檔名首部空格)      sed 's/^[ \t]*//g' 2. 刪除字串行尾空格(刪除檔名尾部空格)     sed 's/[ \t]*$//g' 3.去除字串中所有空格(去除字串中所有空格)

preg_replace函式去除字串空格,逗號(,)等

$num=“1,2,3,4,5,6,7,8,86,9”; 1,如果格式是這樣子就用,PHP的preg_replace ,採用正則運算,去掉所有重複的","。 preg_replace(’#,{2,}#’,’,’,$num); $num=",1,23,4,5,6,7,8"; 2

Java去除字串所有空格的幾種方法

JAVA中去掉空格 1. String.trim() trim()是去掉首尾空格 2.str.replace(" ", ""); 去掉所有空格,包括首尾、中間 複製程式碼 程式碼如下:String str = " hell o "; String str2 = str.replaceAll(" ",

利用正則表示式去除字串空格

\s* 表示若干個空格(可以是0個)。 \s+ 表示一個或多個空格 public class Test { public static void main(String[] args) {

C#去除字串的中文、字母、數字

C#去除字串中的中文 使用正則表示式 1. /// /// 去除字串中的中文 /// /// /// public static string Delete

js去除字串所有空格正則表示式

//定義一個字串var str = " he  llo ";//剔除所有空格var conStr = str .replace(/\s+/g,"");//列印資訊到瀏覽器控制檯console.info(conStr );控制檯輸出資訊:hello

去除字串空格,並將字串翻轉

前幾天參加校招,遇到一個問題,現做分享。 題意大致如下: 將String world = “hello world”;去除所有的空格,並將字元翻轉輸出 答: S

java去除字串空格\t、回車\n、換行符\r、製表符\t

public class StringUtils { //第一種方法 public static String replaceBlank(String str) { String dest = ""; if (str!=nu

Java 去除字串空格、回車、換行符、製表符

public class StringUtils {     public static String replaceBlank(String str) {         String dest = "";         if(str!=null) {        

替換 html換行符、空格去除字串空格\\s*,回車\n,換行符\r,製表符\t

1、替換 html中換行符、空格 /** 替換 html中換行符、空格 */ public static String repText(String str) throws Exception {

C#去除字串的不可見內容

  字串中如果有   you make me smile ᶤ ᶫᵒᵛᵉᵧₒᵤ♥/"    這類不可見的字元 如何處理? 一句正則搞定 string s=you make me smile ᶤ ᶫᵒᵛᵉᵧₒᵤ♥/"; s = Regex.Replace(s, @"[^/x21-

java 去除字串空格,兩種方法

/** * 去除字串中的所有空格 * @author Administrator * */ public class StringTo { public static void main(String[] args) { /** * 第一種方法 Stri

java 技巧 去除字串空格、回車、換行符、製表符(正則表示式)

import java.util.regex.Matcher; import java.util.regex.Pattern; public class StringUtils { public static String replaceBlank(String

正則匹配:去除字串空格,獲取select下拉選項的option的文字內容

<pre name="code" class="javascript">//去除空格 String.prototype.Trim = function() { return t

C++去掉字串重複空格

#include<iostream> #include<string.h> using namespace std; void delSpace(char* buf) {//int len = strlen(buf);//int i,j;//if(i

C# 去除字串的非法字元

/// <summary> /// 檢查是否含有非法字元 /// </summary> /// <param name="str">要檢查的字串</param> /// <returns></returns&

Java去除字串空格、回車、換行符、製表符等特殊字元

在日常開發中,空格、回車、換行符等一些特殊字元經常會困擾著我們對於資料的比較,在存取的時候去除掉這些沒有意義的特殊字元就變得非常必要了。對於Java中的String.trim(),它只能去除掉字串前面