我對 C 很陌生,我想制作一個簡單的程式,從值中獲取字串的索引,一旦有了索引,它就會從字串中洗掉它。它導致緩沖區溢位錯誤?很確定它來自在線搜索的strcat,但我不確定。任何幫助表示贊賞!另外請不要在答案中使用*,因為我不知道它們是如何作業的,我很快就會學會。(如果您的答案需要它,請說明您如何在代碼中使用它)
繼承人的代碼:
#include <string.h>
int findIndex(char string[], char substr) {
for (int index = 0; index < strlen(string); index ) {
if (string[index] == substr) {
return index;
}
}
return -1;
}
int main(void) {
char howAreYou[9] = "howAreYou";
char newString[9];
int index = findIndex(howAreYou, 'o');
for (int i = 0; i < 8; i ) {
if (i != index) {
strncat(newString, &howAreYou[i], 1);
}
}
printf("new string: %s", newString);
return 0;
}
uj5u.com熱心網友回復:
用于char howAreYou[] = "howAreYou";允許終止零。
用于char newString[sizeof howAreYou] = "";為陣列設定正確的大小并初始化陣列,使其在連接之前為空。
#include <stdio.h>
#include <string.h>
int findIndex(char string[], char substr) {
for (int index = 0; string[index]; index ) {
if (string[index] == substr) {
return index;
}
}
return -1;
}
int main(void) {
char howAreYou[] = "howAreYou";
char newString[sizeof howAreYou] = "";
int index = findIndex(howAreYou, 'o');
for (int i = 0; i < sizeof howAreYou; i ) {
if (i != index) {
strncat(newString, &howAreYou[i], 1);
}
}
printf("new string: %s\n", newString);
return 0;
}
uj5u.com熱心網友回復:
一個簡單的解決方案是創建一個從字串中洗掉所需字符的函式。 但請確保在字串的最后添加 '\0' (空字符)。
當您從字串中洗掉一個字符時,它的長度應該減一,因為該位置之后的所有字符都將移動到左一個位置,因此不需要最后一個字符
#include <string.h>
#include <stdio.h>
int findIndex(char string[], char substr)
{
for (int index = 0; index < strlen(string); index )
{
if (string[index] == substr)
{
return index;
}
}
return -1;
}
// This function removes the desired character from the char array
void removeChar(char string[], int index)
{
for (int i = index 1; i < strlen(string); i )
{
string[i - 1] = string[i];
}
// Making the last character '\0' (null character) so that string can end here
string[strlen(string) - 1] = '\0';
}
int main(void)
{
char howAreYou[9] = "howAreYou";
int index = findIndex(howAreYou, 'o');
removeChar(howAreYou, index);
printf("new string: %s", howAreYou);
return 0;
}
希望它有所幫助!:)
uj5u.com熱心網友回復:
對于初學者,您忘記包含標題<stdio.h>
howAreYou最好像這樣定義陣列
char howAreYou[] = "howAreYou";
代替
char howAreYou[9] = "howAreYou";
否則陣列不包含字串。
相應地,陣列newString應宣告為
char newString[sizeof( howAreYou )];
該陣列newString 不包含字串。所以函式的呼叫會strncat呼叫未定義的行為。
通常,您還需要檢查函式的回傳值findIndex。
由于您已經在使用標準 C 字串函式并且只想洗掉第一次出現的字母'o',因此只需使用標準 C 函式即可完成strchr。
例如
const char *p = strchr( howAreYou, 'o' );
if ( p == NULL )
{
strcpy( newString, howAreYou );
}
else
{
size_t n = p - howAreYou;
strncpy( newString, howAreYou, n );
strcpy( newString n, p 1 );
}
這是一個演示程式。
#include <stdio.h>
#include <string.h>
int main( void )
{
char howAreYou[] = "howAreYou";
char newString[sizeof( howAreYou )];
const char *p = strchr( howAreYou, 'o' );
if (p == NULL)
{
strcpy( newString, howAreYou );
}
else
{
size_t n = p - howAreYou;
strncpy( newString, howAreYou, n );
strcpy( newString n, p 1 );
}
puts( newString );
}
程式輸出為
hwAreYou
如果您想使用自己的功能findIndex,那么程式可以如下所示
#include <stdio.h>
#include <string.h>
size_t findIndex( const char string[], char c )
{
size_t i = 0;
while (string[i] != '\0' && string[i] != c) i;
return string[i] == '\0' ? -1 : i;
}
int main( void )
{
char howAreYou[] = "howAreYou";
char newString[sizeof( howAreYou )];
size_t n = findIndex( howAreYou, 'o' );
if ( n == ( size_t )-1 )
{
strcpy( newString, howAreYou );
}
else
{
strncpy( newString, howAreYou, n );
strcpy( newString n, howAreYou n 1 );
}
puts( newString );
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/522234.html
