#ifndef _STRING_H_ #endif _STRING_H_ //#pragma once //此句与头两句效果相同 #include<iostream> using namespace std; #define MAX_STRING_SIZE 10 //合并后存放字符串的空间大小,可以比第一的字符串长度小 typedef char SString[MAX_STRING_SIZE+1]; #define MAX_STRING_SIZE1 6 //单个字符的长度 typedef char SString1[MAX_STRING_SIZE1+1]; int Length(SString s) { return strlen(s); } bool StringConcat(SString T, SString1 s1, SString1 s2)//字符串链接 { int t_length = MAX_STRING_SIZE; int s1_length = Length(s1); int s2_length = Length(s2); if(t_length >= s1_length+s2_length) { for(int i=0; i<s1_length; ++i) { T[i] = s1[i]; } for(int j=0; j<s2_length; ++j) { T[i+j] = s2[j]; } T[i+j] = '\0'; return true; } else if(t_length>s1_length && t_length<s1_length+s2_length) { for(int i=0; i<s1_length; ++i) { T[i] = s1[i]; } for(int j=0; j<t_length-s1_length; ++j) { T[i+j] = s2[j]; } T[i+j] = '\0'; return false; } else if(t_length < s1_length) ///// { for(int i=0; i<t_length; ++i) { T[i] = s1[i]; } T[i] = '\0'; return false; } return true; } void PrintString(char T[]) { cout<<T<<endl; }
#include"string.h" void main() { SString T; SString1 s1 = "hello "; SString1 s2 = "world!"; StringConcat(T,s1,s2); PrintString(T); }
时间: 2024-11-05 15:18:36