在 C++ 中添加两个 C 字符串
adding two c-strings in c++
在我的代码中,我试图将两个字符串加在一起,但是由于某种原因,我似乎无法为我的stringAdd函数获得正确的返回类型。我希望能够返回一个 c 字符串。而且我的实现似乎也不起作用。有什么建议吗?
#include <iostream>
#include<cstring>
using namespace std;
int stringLength(char *); // Function prototype
char stringAdd(char *strPtr, char *strPtr2);//Function prototype
int main()
{
const int SIZE = 51; // Array size
char letter; // The character to count
char word1[SIZE] = "Happy ";
char word2[SIZE] = "Birthday";
cout <<"Your first c-string is: "<<word1<<"Your second c-string is: "<<word2<<"n";
cout << "The length of your first c-string is: ";
cout << stringLength(word1) << " chars long.n";
cout << "The length of your second c-string is: ";
cout << stringLength(word2) << " chars long.n";
if (SIZE >= (stringLength(word1) + stringLength(word2) + 1))
{
cout << "we are gunna add ur strings";
stringAdd(word1, word2);
}
else
{
cout << "String1 is not large enough for both strings.n";
}
return 0;
}
int stringLength(char *strPtr)
{
int times = 0; // Number of times a char appears in the string
// Step through the string each char.
while (*strPtr != ' ')
{
if (*strPtr != '0') // If the current character doesnt equals the null terminator...
times++; // Increments the counter
strPtr++; // Goes to the next char in the string.
}
return times;
}
到目前为止,我的代码工作正常,但是下面的函数似乎根本不起作用。我不确定如何使用引用添加两个 c 字符串
char stringAdd(char *strPtr, char *strPtr2)
{
int size1;
int size2;
size1= stringLength(strPtr);
int j=size1+1; // counter set to the num of chars in the first c-string
int i = 0; // counter for to add to the 2nd c-string
size2= stringLength(strPtr2);
size1=+size2;
char newWord[size1];
for(int i=0;i<size1;i++)
newWord[i] = *strPtr[i]
for(int j=0;j<size2;j++)
newWord[i]= *str
}
首先,使用 std::string
.
然后,使用 std::string
.
最后,如果你真的必须手动操作char
数组,那么至少使用 C 标准库函数,这样你就有希望得到正确的 null 终止。您要查找的函数是 std::strcat
,它连接两个字符串。
之后,使用 std::string
.
您在stringAdd
中有一个拼写错误,导致错误
size1=+size2;
这应该是
size1 += size2;
否则,您只是用 size2
的值覆盖size1
。话虽如此,在C++您也不允许这样做
char newWord[size1];
数组的大小必须在编译时而不是运行时知道。
函数
stringAdd
不正确,并且具有未定义的行为,因为它不返回任何内容。
还有这个 if 语句在函数stringLength
if (*strPtr != '0') // If the current character doesnt equals the null terminator...
times++; // Increments the counter
DOEA 没有多大意义,因为封闭 while 语句中的条件
while (*strPtr != ' ')
与 if 语句中的相同。
{
函数可以按以下方式编写
size_t stringLength( const char *strPtr )
{
size_t n = 0;
while ( strPtr[n] ) ++n;
return n;
}
char * stringAdd( char *strPtr, const char *strPtr2 )
{
char *p = strPtr + stringLength( strPtr );
while ( *p++ = *strPtr2++ );
return strPtr;
}
总的来说,你可以写
if (SIZE >= (stringLength(word1) + stringLength(word2) + 1)) {
cout << "we are gunna add ur strings" << endl;
cout << stringAdd(word1, word2) << endl;
}
//...
在这种情况下,word2 将附加到 word1。
你把它标记为"c ++"和"c-strings",这有点像要求一辆由脚力驱动的汽车。
您有 2 个选项:
- 使用C++字符串
- 使用 C 字符串
对于前者:
#include <iostream>
#include <string>
int main()
{
std::string word1 = "Happy";
std::string word2 = "Birthday";
// ... your other stuff
std::string result = word1 + " " + word2 + "!";
std::cout << "Result is " << result << std::endl;
return 0;
}
对于后者:
#include <iostream> // if you are stuck using c-strings, this is kind of odd
#include <cstring>
#include <memory>
int main()
{
const char* word1 = "Happy";
const char* word2 = "Birthday";
const unsigned int newWordSize = 20; // you only need 16 for this, so 20 is sufficient
char newWord[newWordSize];
std::memset(newWord, newWordSize, 0);
std::strcpy(newWord, word1);
std::strcat(newWord, " ");
std::strcat(newWord, word2);
std::strcat(newWord, "!");
std::cout << "New Word is " << newWord << std::endl;
return 0;
}
为什么你做的事情是错误的:
// NOTE: If your null-terminators are not set, this breaks as it is an infinite loop.
int stringLength(char *strPtr)
{
// NOTE: pointer arithmetic can speed up this function, and make this variable unnecessary
int times = 0; // Number of times a char appears in the string
// Step through the string each char.
while (*strPtr != ' ')
{
if (*strPtr != '0') // If the current character doesnt equals the null terminator...
times++; // Increments the counter
strPtr++; // Goes to the next char in the string.
}
return times;
}
char stringAdd(char *strPtr, char *strPtr2) // ERROR: your return value should probably be char*, and you will need to free that memory later
{
int size1;
int size2;
size1= stringLength(strPtr);
int j=size1+1; // counter set to the num of chars in the first c-string
int i = 0; // counter for to add to the 2nd c-string
size2= stringLength(strPtr2);
size1=+size2;
char newWord[size1]; // ERROR: you cannot allocate a dynamic array this way.
for(int i=0;i<size1;i++) // ERROR: you've set size1 = size1 + size2 + 1, and you attempt to access the first word with this new size. You will access memory outside the bounds of your array
newWord[i] = *strPtr[i]
for(int j=0;j<size2;j++)
newWord[i]= *str
// ERROR: You do not set the null-terminator for the new string
// ERROR: you do not return anything
}
相关文章:
- 两个字符串在 c++ 中不相等
- 在C++的两个字符串中连接以逗号分隔的唯一值
- 如何从文件中读取两个字符串和数字数组,并将它们存储在对象向量中
- 比较两个字符串后卡在无限循环中
- 我已经建立了递归关系,它找到了两个字符串之间最长的连续公共字符串,我怎么能跳过其中一个字符串中的一个字符
- 使用XOR查找O(n)-解决方案中的两个字符串是否为变位符
- 如何将两个字符串加在一起,就好像它们是变量一样?
- 我需要帮助在一个类中输入两个字符串并获取其输出
- 两个字符串之间的数学运算
- sscanf() 有两个字符串参数
- 我的字符串数组一次打印出前两个字符串
- 两个 c++ 字符串上的异或
- 函数返回两个字符串的第一个常用字符
- 目标是找到两个 c 字符串之间的公共前缀(必须使用特定的函数标头)
- 我需要在C++的两个字符串之间找到共同的前缀
- 我有一个返回字符串向量的函数.它需要两个字符串,并且返回一个字符串中缺少的字符串
- 给定两个字符串 S 和 T.确定与 T 相差最小的 S 子字符串?
- gtest:检查字符串是否等于两个字符串之一
- 交换两个字符串时访问正确的内存时,我遇到了分段错误
- 我可以在系统()中使用两个字符串吗?