我正在学习C ++,但我不知道该怎么做。
在分配中,我们需要编写一个函数来检查字符串的前两个字符是否与后两个字符相同。
这些是局限性:
您不能使用
std::string
类或字符串函数,例如strlen
。您必须为此功能使用数组或指针。
我尝试过:
bool haveTheSameTwoChars(string str) {
char arr[] = str;
if (sizeof(arr) < 3) {
return false;
}
else if (arr[0] == arr[sizeof(arr) - 3] && arr[1] == arr[sizeof(arr) - 2]) {
return true;
}
else {
return false;
}
}
但是它不会接受str
到数组中。
但是,如果我要用引号代替str
,即使它们都是字符串,它也可以接受。
我在做什么错了?
答案 0 :(得分:3)
好吧,这是您的问题的细分:
您需要将输入作为数组或指针。例如:
bool isMatched( const char* str )
{
// ...
}
现在,您需要自己计算字符串的长度。修改循环并设计一些可以使您以空终止的字符串为长度的方法。 C字符串以null结尾,即'\0'
,因此当遇到null字符时,您可以结束循环。例如:
int len = 0;
while ( str[len] != '\0' ) len++;
那只是一个主意。做您自己的研究并正确计算字符串长度。
其余只是使用if
的前两个字符的比较。 :)
我确定您可以将所有内容放在一起,并稍微修改学习材料以解决此问题。
祝你好运!
编码愉快!
答案 1 :(得分:3)
如果不允许使用import os
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "DzenanElvir.settings.base")
import django
django.setup()
from base.models import ModelRazred
,则有力地暗示了可以解决问题而无需担心字符串的长度。
让我们这样做:
首先,您应该拥有原型(不允许strlen
,对吧?)
string
然后,验证字符串至少包含两个字符
bool haveTheSameTwoChars(const char* str)
然后您找到字符串的结尾:
if (!str || !str[0] || !str[1])
return false;
然后移回两个字符,因此const char* end = str;
while (*end)
end++;
指向最后两个字符中的第一个:
end
(这很安全,因为我们首先检查了至少两个字符。)
然后比较
end -= 2;
答案 2 :(得分:2)
您的教授希望您使用const char*
作为函数参数,即,将字符串建模为指向字符串中第一个字符的指针,其中字符串以0结尾。(我们称< em> NUL终止)。
所以你的功能是
bool haveTheSameTwoChars(const char* s)
然后,您需要滚动自己的strlen
1 版本,以计算字符串中的字符数,最多不包括NUL
:
#include <cstddef> // for std::size_t
std::size_t strlen(const char *s) {
const char *p = s;
while (*s) ++s;
return s - p;
}
在此之后,考虑到l
的长度,这是一个简单的情况,它处理几个边缘情况和一般情况:
l
为0或1,则返回false
。l
为2,则返回true
。l
大于2,则类似于问题中的内容就足够了,请记住我的l
比您的数字小一。请注意,C ++不支持可变长度数组,因此char arr[] = str;
不是有效的C ++,而sizeof
是 compile-time 运算符因此只会给您实际数组类型的大小,而不是衰减为指针类型的数组。
1 没有专业的程序员会梦想这样做。编译器可能会将strlen
优化为基于机器字的算法,即同时考虑多个字节。
答案 3 :(得分:0)
这是您需要的一些示例代码
bool haveTheSameTwoChars(const char* p) {
if(!p || !*p)
return false;
int len = -1;
while(p[++len]);
if(p[0] == p[len-2] &&
p[1] == p[len-1])
return true;
return false;
}
如果字符串为“ ABxxxAB”,则返回true;如果为“ ABxxxBA”,则返回false。休息吧,您可以根据自己的意愿进行调整。谢谢
答案 4 :(得分:0)
另一种方法是使用循环和单个指针遍历每行,保存第一个和第二个字符,以与字符串中的倒数第二个(倒数第二个)和最后一个(最后一个)字符进行比较。
通过对字符数组进行简单的迭代,只要它是 nul终止的,您就不必担心单独的循环来查找长度,只需抓取/保存前两个字符,然后迭代到字符串的末尾,同时保留上一个/最后一个字符。当您敲击字符串的末尾时,只需将第一个与最后一个进行比较,然后将第二个与倒数第二个进行比较,例如
/* function iterates pointer through chars in 'line' saving
* the 1st & 2nd chara in 'beg` and 'next' and the penultimate and
* ultimate characters in 'prev' & 'last' and compares. returns 1 if
* 1st matches ultimate AND 2nd matches penultimate, 0 otherwise.
*/
int begmatchend (const char *line)
{
const char *p = line; /* pointer to line */
char beg = *p++, /* save for 1st char */
next = *p++, /* save for 2nd char */
prev = *p++, /* save for next to last char */
last = *p++; /* save for last char */
while (*p) { /* iterate over line setting prev/last as you go */
prev = last;
last = *p++;
}
if (beg == last && next == prev) /* test whether equal */
return 1;
return 0;
}
然后,对于一个简单的测试,您可以只从每行调用begmatchend
的文件中馈送程序行,然后输出匹配的行的指示。一种简单的方法如下:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstddef>
#define MAXC 1024
/* function iterates pointer through chars in 'line' saving
* the 1st & 2nd chara in 'beg` and 'next' and the penultimate and
* ultimate characters in 'prev' & 'last' and compares. returns 1 if
* 1st matches ultimate AND 2nd matches penultimate, 0 otherwise.
*/
int begmatchend (const char *line)
{
const char *p = line; /* pointer to line */
char beg = *p++, /* save for 1st char */
next = *p++, /* save for 2nd char */
prev = *p++, /* save for next to last char */
last = *p++; /* save for last char */
while (*p) { /* iterate over line setting prev/last as you go */
prev = last;
last = *p++;
}
if (beg == last && next == prev) /* test whether equal */
return 1;
return 0;
}
int main (int argc, char **argv) {
char line[MAXC] = "";
size_t n = 0;
std::ifstream f (argc > 1 ? argv[1] : "/dev/stdin");
if (!f.is_open()) {
std::cerr << "error: file open failed.\n";
return 1;
}
while (f.getline (line, MAXC, '\n')) {
if (begmatchend ((const char *)line))
std::cout << "line[" << std::setw(3) << n <<
"] 1st/ultimate matched, 2nd/penultimate matched.\n";
n++;
}
return 0;
}
示例输入
$ cat dat/linesbegend.txt
all good vikings go to valhalla
be a tide that will flow and eb
a quick brown fox jumps over the lazy dog
we can find the begin and end - eew
使用/输出示例
$ ./bin/beg_end_match dat/linesbegend.txt
line[ 0] 1st/ultimate matched, 2nd/penultimate matched.
line[ 1] 1st/ultimate matched, 2nd/penultimate matched.
line[ 3] 1st/ultimate matched, 2nd/penultimate matched.
仔细研究一下,如果您有任何疑问,请告诉我。