字符串
来源:互联网 发布:anitama知乎 编辑:程序博客网 时间:2024/06/18 12:06
//============================================================================
// Name : C++Study.cpp
// Author : pan
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <assert.h>
#include <string.h>
using namespace std;
struct String {
char* s = NULL;
int length = 0;
};
void assignString(String* dst, const char* t) {
assert(t!=NULL);
if (dst->s) {
delete dst->s;
}
int len = strlen(t);
dst->s = new char[len + 1];
dst->s[len] = '\0'; ///////这里好好理解下
for (int i = 0; i < len; i++) {
dst->s[i] = t[i];
dst->length++;
}
}
String* concatString(String* s1, String* s2) {
String* s = new String;
s->s = new char[s1->length + s2->length + 1];
s->s[s1->length + s2->length] = '\0';
int i = 0;
while (s1->s[i] != '\0') {
s->s[i] = s1->s[i];
i++;
}
while (s2->s[i] != '\0') {
s->s[i] = s2->s[i];
i++;
}
s1->s = new char[s1->length + s2->length + 1];
i = 0;
while (s->s[i] != '\0') {
s1->s[i] = s->s[i];
cout << s->s[i];
i++;
}
s1->s[i] = '\0';
delete s->s;
delete s;
return s1;
}
void showString(String* s) {
int i = 0;
while (s->s[i] != '\0') {
cout << s->s[i];
i++;
}
}
int indexSubString(String* src, String* sub) { //求子串的位置
int i = 0, j = 0;
while (i < src->length && j < sub->length) {
if (src->s[i] == sub->s[j]) {
i++;
j++;
} else {
i = i - j + 1;
j = 0;
}
}
if (j == sub->length)
return i - j + 1;
return 0;
}
int main() {
/* if('\0'==NULL)
{
cout<<"yes";
}*/
String s1, s2;
char* c = "abcdefg";
assignString(&s1, c);
assignString(&s2, c);
String* s = concatString(&s1, &s2);
showString(s);
delete s1.s;
delete s2.s;
delete s->s;
return 0;
}
// Name : C++Study.cpp
// Author : pan
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include <stdio.h>
#include <cstdlib>
#include <iostream>
#include <queue>
#include <stack>
#include <assert.h>
#include <string.h>
using namespace std;
struct String {
char* s = NULL;
int length = 0;
};
void assignString(String* dst, const char* t) {
assert(t!=NULL);
if (dst->s) {
delete dst->s;
}
int len = strlen(t);
dst->s = new char[len + 1];
dst->s[len] = '\0'; ///////这里好好理解下
for (int i = 0; i < len; i++) {
dst->s[i] = t[i];
dst->length++;
}
}
String* concatString(String* s1, String* s2) {
String* s = new String;
s->s = new char[s1->length + s2->length + 1];
s->s[s1->length + s2->length] = '\0';
int i = 0;
while (s1->s[i] != '\0') {
s->s[i] = s1->s[i];
i++;
}
while (s2->s[i] != '\0') {
s->s[i] = s2->s[i];
i++;
}
s1->s = new char[s1->length + s2->length + 1];
i = 0;
while (s->s[i] != '\0') {
s1->s[i] = s->s[i];
cout << s->s[i];
i++;
}
s1->s[i] = '\0';
delete s->s;
delete s;
return s1;
}
void showString(String* s) {
int i = 0;
while (s->s[i] != '\0') {
cout << s->s[i];
i++;
}
}
int indexSubString(String* src, String* sub) { //求子串的位置
int i = 0, j = 0;
while (i < src->length && j < sub->length) {
if (src->s[i] == sub->s[j]) {
i++;
j++;
} else {
i = i - j + 1;
j = 0;
}
}
if (j == sub->length)
return i - j + 1;
return 0;
}
int main() {
/* if('\0'==NULL)
{
cout<<"yes";
}*/
String s1, s2;
char* c = "abcdefg";
assignString(&s1, c);
assignString(&s2, c);
String* s = concatString(&s1, &s2);
showString(s);
delete s1.s;
delete s2.s;
delete s->s;
return 0;
}
0 0
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- 字符串
- boost 已解决
- 从Viola&Jones的人脸检测说起
- C#网络编程(同步传输字符串)
- 1112 KGold
- ant全自动更新部署web程序的详解
- 字符串
- 七大排序实测(计数、快速、归并、大堆、希尔、插入、选择)
- Linux(centos 6.5) 调用java脚本以及定时执行的脚本实例及配置文件详解
- pandas+numpy ufunc重写question2计算位温
- 使用Spring容器
- YII1.0中验证码刷新不更新的问题的解决。
- C#中的委托和事件
- FPGA中对FIFO深度的计算
- js实现的省市联动