编制具有如下原型的函数findlast:\n char*findlast(char*sourcestr,char*substr);\nfindlast函数则要返回源串sourcestr
时间: 2023-04-30 16:06:36 浏览: 64
findlast函数是一个查找子串在源字符串中最后一次出现位置的函数,其定义为char* findlast(char* sourcestr, char* substr)。函数返回源字符串sourcestr中子串substr最后一次出现的位置,如果未找到则返回NULL。
相关问题
编制具有如下原型的函数findlast: char*findlast(char*sourcestr,char*substr); findlast函数则要返回源串sourcestr中最后一次出现substr子字符串的头字符位置。 而后编制主函数,输入两个字符串,将它们用作实参来调用这两个函数,如果返回null输出-1,否则输出子字符串出现时头字符在原字符串的下标,每个结果占一行。 要求实现程序中不可使用“string.h”头文件内有关寻找子串的标准库函数。
该题目要求实现一个函数findlast,在参数sourcestr中找到参数substr最后一次出现的位置,并返回该位置的头字符在sourcestr中的下标。如果找不到substr则返回-1。 该题目要求使用C语言实现,并且不允许使用字符串相关的标准库函数。
实现一个字符串加解密系统。 使用一个String对象passwd的字符序列作为密码,对另一个string对象sourceStr的字符序列进行加密,操作过程如下:利用toCharArray()将passwd的字符序列存入一个字符数组p[]中。假设该数组p的长度为n,那么就将待加密的sourceStr的字符序列按顺序以n个字符为一组(最后一组中的字符个数可以小于n),对每一组中的字符与数组p的对应字符做加法运算。 例如某一组中的n个字符是a0a1…an-1,那么按如下方式得到对该字符加密的结果: C0=(char)(a0+p[0]), C1=(char)(a1+p[1]), …, Cn-1=(char)(an-1+p[n-1]) 上述加密算法的解密算法是对密文做减法运算。
以下是一个简单的字符串加解密系统的实现:
```java
public class StringEncryptor {
public static String encrypt(String passwd, String sourceStr) {
char[] p = passwd.toCharArray();
char[] source = sourceStr.toCharArray();
int n = p.length;
StringBuilder encrypted = new StringBuilder();
for (int i = 0; i < source.length; i += n) {
for (int j = 0; j < n && i + j < source.length; j++) {
char c = (char) (source[i + j] + p[j]);
encrypted.append(c);
}
}
return encrypted.toString();
}
public static String decrypt(String passwd, String encryptedStr) {
char[] p = passwd.toCharArray();
char[] encrypted = encryptedStr.toCharArray();
int n = p.length;
StringBuilder decrypted = new StringBuilder();
for (int i = 0; i < encrypted.length; i += n) {
for (int j = 0; j < n && i + j < encrypted.length; j++) {
char c = (char) (encrypted[i + j] - p[j]);
decrypted.append(c);
}
}
return decrypted.toString();
}
}
```
使用例子:
```java
String passwd = "password";
String sourceStr = "hello world";
String encryptedStr = StringEncryptor.encrypt(passwd, sourceStr);
System.out.println("Encrypted: " + encryptedStr);
String decryptedStr = StringEncryptor.decrypt(passwd, encryptedStr);
System.out.println("Decrypted: " + decryptedStr);
```
输出:
```
Encrypted: ªØûÚÚÞäÕ
Decrypted: hello world
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)