虽然这段代码很可怕,而且我已经清楚地解决了这个问题,但代码可以正常工作(目前)。我想搞砸它,但我真的不知道我在这里做错了什么。目标是,不使用任何内置函数,取字符串“This is a test”并将“te”替换为“gho”以拼出“This is a ghost”。
int main(int argc, const char * argv[]) {
char *s = "This is a test";
char *newstring = malloc(strlen(s));
for (int i = 0 ; s[i] != '\0' ; i++){
if (s[i] == 't' && s[i+1] == 'e') {
newstring[i] = 'g';}
else if (s[i] == 'e' && s[i+1] == 's') {
newstring[i] = 'h';}
else if (s[i] == 's' && s[i+1] == 't') {
newstring[i] = 'o';
}
else if (s[i] == 't') {
newstring[i] = 's';
}
else {
newstring[i] = s[i];
}
}
printf("%st",newstring);
return 0;
}
我的问题是我不知道如何将字符附加到C中的新字符串,这样我就可以在替换单个字符时保持原始字符串的完整性。
答案 0 :(得分:2)
char *s = "This is a test";
int len = 0;
while(s[len++]);//strlen(s) + 1
for (int i = 0 ; s[i] != '\0' ; i++){
if (s[i] == 't' && s[i+1] == 'e') {//count "te"
++len;
++i;
}
}
char *newstring = malloc(len);
int j = 0;
for (int i = 0 ; s[i] != '\0' ; i++){
if (s[i] == 't' && s[i+1] == 'e') {
newstring[j++] = 'g';
newstring[j++] = 'h';
newstring[j++] = 'o';
++i;
} else {
newstring[j++] = s[i];
}
}
newstring[j] = '\0';
puts(newstring);
free(newstring);
答案 1 :(得分:1)
我们应该认识到你想要做的模式。首先,记住你可以像这样填写一个新数组是一个有用的技巧:
int num = 0;
int buf[256];
buf[num++] = 123;
buf[num++] = 456;
buf[num++] = 789;
这是一个简写。这一行:
buf[num++] = 123;
提供与以下内容相同的行为:
buf[num] = 123;
++num;
那么我们想在这做什么?我们想要创建一个与原始字符串相同的新字符串,只不过它取代了" te"与" gho"。那怎么样?
#include <stdio.h>
int main(int argc, const char * argv[])
{
const char *s = "This is a test";
int i = 0;
int new_len = 0;
// We don't know what the final size of the string will be, so
// let's make one that's generally large enough. Can do more
// elaborate things here if you want a really robust solution.
char new_string[256] = {0};
// For each character in the string:
for (i=0; s[i] != '\0'; ++i)
{
if (s[i] == 't' && s[i+1] == 'e')
{
// If we find "te", add "gho".
new_string[new_len++] = 'g';
new_string[new_len++] = 'h';
new_string[new_len++] = 'o';
// ... and skip one character to ignore both the 't' and the 'e'.
++i;
}
else
{
// Otherwise just add the same character.
new_string[new_len++] = s[i];
}
}
// Add the null terminator at the end of our new string.
new_string[new_len] = '\0';
// Output the new string.
printf("%s\n", new_string);
}
答案 2 :(得分:0)
int myStrLen( char * );
int myStrlen( char *testStr )
{
int rtnCount;
for( rtnCount = 0; testStr[rtnCount]; rtnCount++ ) {;}
return( rtnCount );
} // end function: myStrlen
其他字符串函数可以以类似的方式实现。
然后'本土'字符串函数可以使用类似于此:
// get length of original string
int oldStringLen = myStrLen( s );
// provide room on the stack for the new string
char newString[oldStringLen+2] = {'\0'};
// search for occurrence of string to be replaced
char * targetString = myStrStr( s, "test" );
if( NULL == targetString )
{ // then target string not found
// handle error??
return( -1 );
}
// implied else, target string found in 's'
// copy first part of original string
myStrNCpy( newString, s, (targetString - s) +1 );
// append the replacement string
myStrCat( newString, "ghost" );
// append remainder of original string
myStrCat( newString, &targetString[4] );
代码是否需要继续查看原始字符串 其他可能的替代品?
当然,如何向用户显示结果 可能是“有趣的”,因为C无法执行任何I / O. 不使用类似stdio.h库的东西