題目:給定乙個字串,其中包含 '-',乙個 '-' 代表需要擴充字串,
例如 a-d ====> abcd
0-9 ====> 0123456789
a-f ====> abcdef
以下幾種情況不需要擴充:
a-2 , a-d, 6-3, f-a,
前後型別不匹配不擴充
型別相同,前者大於後者不匹配
思路:
1. 遍歷字串尋找 '-' 以便擴充字串
2. 判斷該處是否需要擴充套件
3. 擴充套件該字串
具體情況如下:
1. 如何擴充字串:
首先判斷給定字串是否為空,為空則返回
其次迴圈遍歷字串的每乙個字元尋找 '-'
判斷該'-' 出現的位置是否為字串開頭或結尾,若是則跳過本次迴圈,檢測下乙個字元或結束。
判斷該 '-'出現的位置是否符合字串擴充的標準,若是則進行擴充,否則遍歷下乙個字元。
2. 怎樣判斷是否符合擴充標準
//擴充字串有3中可能性
// 1. 數字 0-9 2.大寫字母a-z 3. 小寫字母
獲得 '-' 的前乙個字元str_left 和後乙個字元 str_right
判斷是否符合
0 <= str_left <= str_right <=9
'a' <= str_left <= str_right <='z'
'z' <= str_left <= str_right <='z'
三個標準中的乙個,若是則需要擴充,否則不擴充
3. 計算需要新增的字元數 add_count
計算字串需要增加的長度 add_length
判斷 str_left 和 str_right 是否一樣 若一樣則刪去 '-'
否則將字串從 str_right 處後移add_length 個長度
然後再補全需要新增的字元
**如下:
#include #include #include #include #include #define true (1)
#define false (0)
typedef unsigned char bool;
void extend_add(char *str)
*--str_right = '\0';
} else
// 補全需要新增的字元
str_end += add_length;
while ( str_end > str_left )
}}bool extend_able(char *str)
else if ( 'a' <= *str_left && *str_left <= *str_right && *str_right <= 'z' ) else if ( 'a' <= *str_left && *str_left <= *str_right && *str_right <= 'z' ) else
}char *my_strextend(const char *src)
while ( *str_current )
// 判斷是否符合擴充套件標準,若是則進行擴充套件
if ( extend_able(str_current) )
}str_current++;
}return str_start;
}int main(int argc, const char *argv)
js如何獲取乙個字串在另外乙個字串中的下標
有字串 a 34 b 123456789 要求定義乙個函式返回b字串中第乙個匹配a字串的下標,不得使用現有的方法 indexof 兩種解決辦法 substr 方法可在字串中抽取從 start 下標開始的指定數目的字元。const b,a return 1 let a 34 let b 1234567...
在乙個字串中尋找另外乙個字串
在乙個字串中尋找另外乙個字串 public class text foundit true break test system.out.println foundit?found it didn t find it 該段程式有點難以理解,主要就是if語句的理解,if searchme.charat ...
如何讓乙個字串反轉
如何讓乙個字串反轉?第一種實現 string str xie xie da jia 謝謝大家 system.out.print 逆轉後的字串是 for int i str.length 1 i 0 i system.out.print 第一種實現輸出結果是 逆轉後的字串是 家大謝謝 aij ad e...