int indexOf(char *s1,char *s2){ for (int i = 0; *(s2 + i); i++) { for (int j = 0; (*s2 + j); j++) { if (*(s2 + i + j) != *(s1 + j)) { break; } if (j == strlen(s1)) { return i; } } } return -1; }解释这段代码
时间: 2024-04-15 19:24:52 浏览: 10
这段代码是一个函数,名为indexOf,用于在字符串s2中查找子字符串s1的起始索引位置。函数使用了两个嵌套的循环来进行遍历。
外层循环通过指针运算遍历字符串s2,直到遍历到字符串s2的末尾为止。内层循环通过指针运算遍历字符串s2,并且同时遍历字符串s1。
在内层循环中,首先判断如果当前字符不匹配(即s2中的字符与s1中的字符不相等),则跳出内层循环,继续外层循环的下一次迭代。
如果内层循环完全遍历了字符串s1(即j等于strlen(s1)),则表示在s2中找到了与s1完全匹配的子字符串,返回当前外层循环的迭代次数i作为起始索引位置。
如果外层循环完全遍历了字符串s2而未找到匹配的子字符串,则说明s1不存在于s2中,函数返回-1表示未找到。
总结来说,该函数的目的是找到字符串s2中子字符串s1的起始索引位置,并返回该索引位置值,如果未找到则返回-1。
相关问题
基于char* 设计一个字符串类MyString,具有构造函数、析构函数、复制构造函数。具体如下: MyString(); MyString(const char* cString); char at(int index) const; int length() const; void clear(); bool empty() const; int compare(const MyString& s) const; int compare(int index, int n, const MyString& s) const; void copy(char s[], int index , int n); char* data() const; int find(char ch) const ; int find(char ch ,int index) const; int find(const MyString& s, int index) const;
好的,我来为您解答。以下是实现MyString类的代码:
```c++
#include <iostream>
#include <cstring>
using namespace std;
class MyString {
private:
char* str;
int len;
public:
MyString() : str(nullptr), len(0) {}
MyString(const char* cString) {
len = strlen(cString);
str = new char[len + 1];
strcpy(str, cString);
}
MyString(const MyString& other) {
len = other.len;
str = new char[len + 1];
strcpy(str, other.str);
}
~MyString() {
if (str != nullptr)
delete[] str;
}
char at(int index) const {
if (index < 0 || index >= len)
throw out_of_range("index out of range!");
return str[index];
}
int length() const {
return len;
}
void clear() {
if (str != nullptr) {
delete[] str;
str = nullptr;
len = 0;
}
}
bool empty() const {
return len == 0;
}
int compare(const MyString& s) const {
return strcmp(str, s.str);
}
int compare(int index, int n, const MyString& s) const {
if (index < 0 || index >= len || n < 0 || index + n > len)
throw out_of_range("index or n out of range!");
char* sub = new char[n + 1];
strncpy(sub, str + index, n);
sub[n] = '\0';
int res = strcmp(sub, s.str);
delete[] sub;
return res;
}
void copy(char s[], int index, int n) {
if (index < 0 || index >= len || n < 0 || index + n > len)
throw out_of_range("index or n out of range!");
strncpy(s, str + index, n);
s[n] = '\0';
}
char* data() const {
return str;
}
int find(char ch) const {
char* p = strchr(str, ch);
if (p == nullptr)
return -1;
return p - str;
}
int find(char ch, int index) const {
if (index < 0 || index >= len)
throw out_of_range("index out of range!");
char* p = strchr(str + index, ch);
if (p == nullptr)
return -1;
return p - str;
}
int find(const MyString& s, int index) const {
if (index < 0 || index >= len)
throw out_of_range("index out of range!");
char* p = strstr(str + index, s.str);
if (p == nullptr)
return -1;
return p - str;
}
};
```
在这个程序中,我们定义了一个MyString类,它包含了一个char*类型的成员变量和一个int类型的成员变量。我们使用构造函数、析构函数和拷贝构造函数来初始化和释放对象。在类中,我们实现了at()、length()、clear()、empty()、compare()、copy()、data()、find()等函数,用于获取和操作字符串的不同属性。
当我们运行这个程序时,就可以使用MyString类来实现字符串的不同操作。例如:
```c++
int main() {
MyString s1("Hello world!");
cout << "s1: " << s1.data() << endl;
cout << "s1.at(1): " << s1.at(1) << endl;
cout << "s1.length(): " << s1.length() << endl;
MyString s2(s1);
cout << "s2: " << s2.data() << endl;
s1.clear();
cout << "s1.empty(): " << s1.empty() << endl;
MyString s3("hello");
cout << "s3.compare(s1): " << s3.compare(s1) << endl;
cout << "s3.compare(0, 2, s1): " << s3.compare(0, 2, s1) << endl;
char buf[10];
s3.copy(buf, 1, 3);
cout << "buf: " << buf << endl;
cout << "s3.find('l'): " << s3.find('l') << endl;
cout << "s3.find('l', 2): " << s3.find('l', 2) << endl;
cout << "s3.find(s1, 0): " << s3.find(s1, 0) << endl;
return 0;
}
```
输出结果为:
```
s1: Hello world!
s1.at(1): e
s1.length(): 12
s2: Hello world!
s1.empty(): 1
s3.compare(s1): -1
s3.compare(0, 2, s1): -1
buf: ell
s3.find('l'): 2
s3.find('l', 2): 2
s3.find(s1, 0): -1
```
我们可以看到,MyString类成功地实现了字符串的不同操作,并且可以使用它来获取和操作字符串的不同属性。
package five; public class five { public static void main( String [] args ) { String s1 = " 1234567890QWERTYUIOPqwertyuiop~!@#~$%^^&~我们永远热爱和平 "; System.out.println("s1 = " + s1); int s1_len = s1.length(); System.out.println( s1_len ); String s2 = s1.substring( 10, 20 ); System.out.println( s2 ); char c = s1.charAt( 10 ); System.out.println( c ); String s3 = s1.concat("世界!"); System.out.println( s3 ); String s4 = s1; boolean b1 = s3 == s4; System.out.println( b1 ); int index = s1.indexOf('3'); System.out.println( index ); s1 = s1.trim(); s1.toUpperCase(); System.out.println( s1 ); char cc[] = s1.toCharArray(); System.out.println( java.util.Arrays.toString(cc) ); } }
这是一个Java类,名为five。它包含一个main方法,用于测试一些字符串操作。在main方法中,定义了一个字符串s1,然后对其进行了多种操作:
1. 使用length()方法获取字符串长度,并将其输出。
2. 使用substring()方法从s1中获取子字符串,然后将其输出。
3. 使用charAt()方法获取s1中指定位置的字符,并将其输出。
4. 使用concat()方法将字符串s1和"世界!"拼接起来,并将拼接后的结果输出。
5. 将s1赋值给s4,然后使用"=="运算符比较s3和s4是否相等,并将比较结果输出。
6. 使用indexOf()方法查找字符'3'在s1中第一次出现的位置,并将其输出。
7. 使用trim()方法去除s1首尾的空格,然后使用toUpperCase()方法将其转换为大写字母,并将结果输出。
8. 使用toCharArray()方法将s1转换为字符数组,然后使用Arrays.toString()方法将字符数组转换为字符串,并将其输出。
需要注意的是,虽然使用了trim()和toUpperCase()方法,但是s1本身并没有被修改,因为在Java中String是不可变的。
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)