讓我說我有字符串「我擁有許多寵物」。 如何在C語言中的「many」之前找到字符串的長度?我需要找到一個子串的長度beore一個字在C
回答
只要找到第一次出現的索引「多」
可以使用strstr()
功能做到這一點,只是找到strstr()
返回的指針和指向整個字符串之間的區別。
正如@ 4386427指出的那樣,如果我們談論單詞(單詞≠子串),此解決方案可能不起作用。
所以,如果你想用單詞做些什麼,你將不得不檢查你的子字符串前後是否有空格字符(並處理這個單詞在字符串開頭或結尾的情況搜索中的子字符串)。
我想用另一個字替換字符串中的一個字,但該字 不必是相同的長度。我可以做到這一點,如果他們是相同的長度,但我不知道如何做,如果他們是不同的長度。我 雖然只拼接在一起有字符串來做到這一點。但我是 不知道該怎麼做。
查找字符串中的每個單詞使用strstr(mystr, " ");
假設只有一個字與字之間的空間(只是爲了簡化分析),創建使用start
和end
while循環。每個單詞從start
開始,並且在結束指針中的單詞後面有空白。使用此循環設置指針陣列
/* This assumes there is only one space between words
It also assumes there are no leading or trailing blanks
The assumptions are just for ease of analysis.
*/
char str[] ="This is a simple string";
char *start = str;
char *end = NULL;
int count = 0;
char mystring[MAX_STRINGS][MAX_SIZE];
while(end = strstr(start," "))
{
len = end - start;
// copy the substring into the array using strncpy()
strncpy(mystring[count], start, len);
// add the final '\0' character to the string in the array
mystring[count++][len] = '\0';
start = end + 1;
}
// Copy the last substring int the array
// end is now NULL but the substring ends with '\0'
// Thus a plain strcpy() is all that is required.
strcpy(mystring[count], start);
您應該將單詞列表設置爲指針數組。我要把這作爲一組的步驟,而不是代碼,以便您可以構建代碼yoursel
創建數組的指針爲字符串。
char * mystrings [MAX_STRINGS];
將大的句子字符串拆分爲單個單詞並將每個單詞放入單獨的字符串中。您可以使用malloc來創建適當大小的緩衝區。
確定具有要替換的單詞的指針。
創建一個指向新詞,並把它放入數組
釋放舊指針。
使用
strcat()
將數組中的字符串連接成新字符串。免費的指針,並確保你不會溢出最後的緩衝,當你做了
strcat()
另一種方法是創建一個二維字符數組。
char mystrings[MAX_STRINGS][MAX_SIZE];
使用類似的過程,除了使用strcpy()
的話複製到緩衝區。舊字的替換也通過strcpy()
完成,您不必釋放舊指針。再次使用strcat()
將新句子作爲字符串。請注意,由於strcpy()
將最終的NUL字符置於原地,因此只要不超過緩衝區,這兩個字的大小就不一樣了。只要不超出緩衝區,Similarlt strcat()
會將最終的NUL放置到位以產生結果字符串。
一種方法是首先檢查是否找到sub string
,如果找到,則打印其位置-1
。
嘗試以下操作:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void checkString(char *string1, char *string2);
int main(void){
char test[256] = "I own many pets";
char string2[] = "many";
checkString(test, string2);
return 0;
}
void checkString(char *string1, char *string2){
char *s1, *s2, *s3;
size_t lenstring1 = strlen(string1);
size_t lenstring2 = strlen(string2);
if (lenstring2 < 1){
printf("There is no substring found");
exit(1);
}
size_t i=0,j=0;
int found=0;
s1 = string1;
s2 = string2;
for(i = 0; i < lenstring1; i++){
if(*s1 == *s2){
s3 = s1;
for(j = 0;j < lenstring2;j++){
if(*s3 == *s2){
s3++;s2++;
}else{
break;
}
}
s2 = string2;
if(j == strlen(string2)){
found = 1;
printf("The length is %zu\n", i);
}
}
s1++;
}
if(found == 0){
printf("No match Found");
}
}
輸出:
The length is 6
char *replace(char *str, char *old, char *new)
{
char *ret;
int lold, lnew;
char *pos = strstr(str, old);
if(pos == NULL)
return NULL;
lnew = strlen(new);
lold = strlen(old);
ret = malloc(1+strlen(str)+lnew-lold);
if(pos != str)
strncpy(ret, str, pos-str);
strcat(ret, new);
strcat(ret, pos+lold);
return ret;
}
- 1. 找到一個字符串的長度
- 2. 我需要一個特定長度的子序列,在Groovy
- 3. 如何「成長」一個字符串到想要的長度
- 4. unpack需要一個字符串參數長度爲24
- 5. 問題得到一個字符串的長度在C
- 6. 找到一個Lua號的字符串長度?
- 7. 我需要從一個字符串
- 8. 在R的長字符串中找到第一個匹配的子字符串
- 9. 在C中找到一個字符串
- 10. 一個字符串的字節長度
- 11. 找到N個唯一的字符最長子串
- 12. 找到一個子字符串中的
- 13. 需要在c中的字符串中的「字」後得到一個字符串
- 14. 我需要一個替代方法來串流字符串。 C++
- 15. 爲什麼需要在C中的字符串數組中聲明一個字符串的長度?
- 16. 在C中找到一個字符串中的子字符串C
- 17. 我需要從一個Java字符串中得到一個子串字符串Tokenizer
- 18. 需要找到一個特定的字符串模式php
- 19. 的indexOf()需要找到一個字符串
- 20. 我有一個數組,我需要得到的長度,它給我的麻煩
- 21. 找到一個子串和插入另一個字符串
- 22. C++需要比較一個字符串到200.000字
- 23. 返回一個字符串的長度
- 24. 在一個句子中找到一個字符串表達式
- 25. 如何找到一個LPCSTR的長度
- 26. 找到一個變量的長度
- 27. 找到一個JSON對象的長度
- 28. 找到一個SQL OleDbDataReader的長度
- 29. 找到一個子串
- 30. 需要一個方程來找到所需的速度
你看看[類似的問題(https://stackoverflow.com/search?q= [C] +找到+子)?答案應該至少能讓你獲得部分解決方案。 – 5gon12eder
可能重複的[如何從字符串查找子串?](http://stackoverflow.com/questions/13195353/how-to-find-substring-from-string) – user1803551