1. 串的基本操作
#include <stdio.h>
#include <string.h>
#define MAIN_STR_LEN 100
#define SUB_STR_LEN 20
int MyStrCompare(char* Str1, char* Str2)
{
int i;
int result = 0;
for (i = 1; (i <= Str1[0]) && (i <= Str2[0]); i++)
{
if (Str1[i] > Str2[i])
{
result = 1;
break;
}
else if (Str1[i] < Str2[i])
{
result = -1;
break;
}
}
if (result == 0)
{
if (Str1[0] > Str2[0])
{
result = 1;
}
else if (Str1[0] < Str2[0])
{
result = -1;
}
}
return result;
}
int MySubStrIndex(char* MainStr , char* SubStr , int pos)
{
int i, j, result;
if ((pos < 0) || (pos > (MainStr [0] - SubStr[0] + 1)))
{
return 0;
}
i = pos;
j = 1;
while (i <= MainStr [0] && j <= SubStr [0])
{
if (MainStr[i] == SubStr[j])
{
i++;
j++;
}
else
{
i = i - j + 2;
j = 1;
}
}
if (j > SubStr [0])
{
result = i - SubStr [0];
}
else
{
result = 0;
}
return result;
}
char MyStrDelete(char* S, int pos , int len)
{
int i;
if (pos < 1 || pos > S[0] - len + 1 || len < 0)
return -1;
for (i = pos + len; i <= S[0]; i++)
S[i - len] = S[i];
S[0] -= len;
return 1;
}
char MyStrInsert(char* S, int pos , char* T)
{
int i;
if (pos < 1 || pos > S[0] + 1)
return -1;
if (S[0] + T[0] <= MAIN_STR_LEN)
{
for (i = S[0]; i >= pos; i--)
{
S[i + T[0]] = S[i];
}
for (i = pos; i < pos + T[0]; i++)
{
S[i] = T[i - pos + 1];
}
S[0] = S[0] + T[0];
return 1;
}
else
{
for (i = MAIN_STR_LEN; i <= pos; i--)
{
S[i] = S[i - T[0]];
}
for (i = pos; i < pos + T[0]; i++)
{
S[i] = T[i - pos + 1];
}
S[0] = MAIN_STR_LEN;
return 0;
}
}
int MyStrAssign(char * StrTobeAssigned , char * StrInput)
{
int len = strlen(StrInput);
if (len > MAIN_STR_LEN)
return -1;
StrTobeAssigned[0] = len;
strcpy(&StrTobeAssigned[1], StrInput);
return 1;
}
int MyStrReplace(char * MainStr , char * SubStr1 , char * SubStr2)
{
int i = 1, tot = 0;
do
{
i = MySubStrIndex(MainStr, SubStr1, i);
if (i)
{
MyStrDelete(MainStr, i, SubStr1[0]);
MyStrInsert(MainStr, i, SubStr2);
i += strlen(SubStr2);
tot++;
}
} while (i);
return tot;
}
int main()
{
char MainStr[MAIN_STR_LEN + 1];
char SubStr1[SUB_STR_LEN + 1];
char SubStr2[SUB_STR_LEN + 1];
int i;
int compareResult;
int pos;
int replaceCounter = 0;
char InputBuf[3][MAIN_STR_LEN + 1];
printf("please input 3 strings as MainStr , SubStr1 and SubStr2\n");
scanf("%s %s %s", InputBuf[0], InputBuf[1], InputBuf [2]);
if (MyStrAssign(MainStr, InputBuf[0]) == -1 || MyStrAssign(SubStr1, InputBuf[1]) == -1 || MyStrAssign(SubStr2, InputBuf[2]) == -1)
{
printf("Error: String length exceeds maximum length\n");
return -1;
}
printf("MainStr =:%d,", MainStr[0]);
for (i = 1; i <= MainStr[0]; i++)
{
printf("%c,", MainStr[i]);
}
printf("\n");
printf("SubStr1 =:%d,", SubStr1[0]);
for (i = 1; i <= SubStr1[0]; i++)
{
printf("%c,", SubStr1[i]);
}
printf("\n");
printf("SubStr2 =:%d,", SubStr2[0]);
for (i = 1; i <= SubStr2[0]; i++)
{
printf("%c,", SubStr2[i]);
}
printf("\n");
compareResult = MyStrCompare(SubStr1, SubStr2);
printf("SubStr1 compare with SubStr2 is %d\n", compareResult);
pos = MySubStrIndex(MainStr, SubStr1, 1);
printf("pos of SubStr1 in MainStr is %d\n", pos);
pos = MySubStrIndex(MainStr, SubStr2, 1);
printf("pos of SubStr2 in MainStr is %d\n", pos);
replaceCounter = MyStrReplace(MainStr, SubStr1, SubStr2);
printf("The content of MainStr after %d times of replacing SubStr1 with SubStr2 is :\n%d,", replaceCounter , MainStr[0]);
for (i = 1; i <= MainStr[0]; i++)
{
printf("%c,", MainStr[i]);
}
printf("\n");
return 0;
}
2. 统计字符数
#include <stdio.h>
#include <string.h>
#define MAXSTRLEN 100
#define TRUE 1
#define FALSE 0
#define ERROR -1
#define OK 1
typedef char SString[MAXSTRLEN + 1];
void SStringAssign(SString S,char*str)
{
int i = 1,len = 0;
for(;str[len] != '\0';len ++)
for(;str[i - 1] != '\0';i ++)
{
S[i] = str[i - 1];
}
S[0] = len;
S[len + 1] = '\0';
}
void CountStr(SString S,int *total)
{
int i = 1;
for(;i <= S[0];i ++)
{
total[S[i] - 'a'] ++;
}
}
void PrintMax(int *total)
{
int max = total[0],i = 0,max_n = 0;
for(;i < 26;i ++)
{
if(max < total[i])
{
max = total[i] ;
max_n = i;
}
}
printf("%c %d\n",max_n + 'a',max);
}
int main(int argc, char *argv[])
{
int n ;
scanf("%d",&n);
getchar();
while(n --)
{
SString S;
char str[MAXSTRLEN + 1000];
scanf("%s",str);
str[100] = '\0';
getchar();
SStringAssign(S,str);
int total[26] = {0};
CountStr(S,total);
PrintMax(total);
}
return 0;
}
3.字符串连接
#include <stdio.h>
#include <string.h>
#define MAXSTRLEN 100
#define TRUE 1
#define FALSE 0
#define ERROR -1
#define OK 1
typedef char SString[MAXSTRLEN + 1];
void StrAssign(SString S,char*str)
{
int i = 1;
for(;str[i - 1] != '\0';i ++)
{
if(str[i - 1] == '#') break;
S[i] = str[i - 1];
}
S[0] = i;
S[i + 1] = '\0';
}
int StrConcat(SString S, SString T) {
if (S[0] + T[0] > MAXSTRLEN) {
return ERROR;
}
int i = 1;
for(;S[i] != '\0';i++)
{
T[T[0] + i] = S[i];
}
T[0] += S[0];
return OK;
}
int main(int argc, char *argv[])
{
int n = 1000,i;
char str1[n],str2[n];
scanf("%[^#]#", str1);
getchar();
scanf("%[^#]#", str2);
SString S,T;
StrAssign(S,str1);
StrAssign(T,str2);
StrConcat(S,T);
for(i = 1;i <= T[0];i ++)
{
printf("%c",T[i]);
}
printf("\n");
return 0;
}
4. 词频统计
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#define MAX_WORD_LENGTH 50
#define HASH_SIZE 1000
typedef struct WordNode {
char word[MAX_WORD_LENGTH];
int count;
struct WordNode* next;
} WordNode;
typedef struct {
WordNode* buckets[HASH_SIZE];
} HashTable;
int hash(char *word) {
int hash = 0;
while (*word) {
hash = (hash * 31 + *word) % HASH_SIZE;
word++;
}
return hash;
}
WordNode* createWordNode(char *word) {
WordNode* newNode = (WordNode*)malloc(sizeof(WordNode));
if (newNode == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(1);
}
strcpy(newNode->word, word);
newNode->count = 1;
newNode->next = NULL;
return newNode;
}
WordNode* findWord(HashTable* hashTable, char *word) {
int index = hash(word);
WordNode* current = hashTable->buckets[index];
while (current != NULL) {
if (strcmp(current->word, word) == 0) {
return current;
}
current = current->next;
}
return NULL;
}
void insertWord(HashTable* hashTable, char *word) {
int index = hash(word);
WordNode* current = hashTable->buckets[index];
WordNode* previous = NULL;
while (current != NULL) {
if (strcmp(current->word, word) == 0) {
current->count++;
return;
}
previous = current;
current = current->next;
}
WordNode* newNode = createWordNode(word);
if (previous == NULL) {
hashTable->buckets[index] = newNode;
} else {
previous->next = newNode;
}
}
int compare(const void *a, const void *b) {
const WordNode *word1 = *(const WordNode **)a;
const WordNode *word2 = *(const WordNode **)b;
if (word1->count != word2->count) {
return word2->count - word1->count;
} else {
return strcmp(word1->word, word2->word);
}
}
void freeHashTable(HashTable* hashTable) {
for (int i = 0; i < HASH_SIZE; i++) {
WordNode* current = hashTable->buckets[i];
while (current != NULL) {
WordNode* temp = current;
current = current->next;
free(temp);
}
}
free(hashTable);
}
int main() {
FILE *file = fopen("article.txt", "r");
if (file == NULL) {
fprintf(stderr, "Failed to open file\n");
return 1;
}
HashTable* hashTable = (HashTable*)malloc(sizeof(HashTable));
if (hashTable == NULL) {
fprintf(stderr, "Memory allocation failed\n");
return 1;
}
memset(hashTable->buckets, 0, sizeof(hashTable->buckets));
char word[MAX_WORD_LENGTH];
while (fscanf(file, "%s", word) == 1) {
for (int i = 0; word[i]; i++) {
word[i] = tolower(word[i]);
}
int len = strlen(word);
while (len > 0 && ispunct(word[len - 1])) {
word[len - 1] = '\0';
len--;
}
insertWord(hashTable, word);
}
fclose(file);
WordNode* wordArray[HASH_SIZE];
int count = 0;
for (int i = 0; i < HASH_SIZE; i++) {
WordNode* current = hashTable->buckets[i];
while (current != NULL) {
wordArray[count++] = current;
current = current->next;
}
}
qsort(wordArray, count, sizeof(WordNode*), compare);
int numWords = count < 100 ? count : 100;
for (int i = 0; i < numWords; i++) {
printf("%s %d\n", wordArray[i]->word, wordArray[i]->count);
}
freeHashTable(hashTable);
return 0;
}
5. 字符串中的第一个唯一字符
#include <stdio.h>
#include <string.h>
#define MAXSTRLEN 300000
#define TRUE 1
#define FALSE 0
#define ERROR -1
#define OK 1
typedef char SString [MAXSTRLEN + 1];
void StrAssign(SString S,char*str)
{
int i = 1;
for(;str[i - 1] != '\0';i ++)
{
S[i] = str[i - 1];
}
S[0] = i - 1;
S[i] = '\0';
}
int Unique_Index(SString S)
{
if(S[0] == 0) return -1;
int i,j,tag;
for(i = 1;i <= S[0];i ++)
{
for(j = 1;j <= S[0];j ++)
{
tag = -1;
if(j == i) continue;
else if(S[i] == S[j]) break;
if(S[i] != S[j]) tag = i;
}
if(tag != -1) return i - 1;
}
return -1;
}
int main(int argc, char *argv[])
{
char str[MAXSTRLEN + 1];
scanf("%s",str);
SString S;
StrAssign(S,str);
int a = Unique_Index(S);
printf("%d",a);
return 0;
}
6. 求一个串中出现的第一个最长重复子串
#include <stdio.h>
#include <string.h>
#include<malloc.h>
#define MAXSTRLEN 255
#define TRUE 1
#define FALSE 0
#define ERROR -1
#define OK 1
int check(char *s, int x, int n,int *pos)
{
int j = 0,i,k;
for(;j < n - x + 1;j ++)
{
char substr[x + 1];
for(i = j,k=0;i < x + j; i ++,k++)
{
substr[k] = s[i];
}
substr[x] = '\0';
int count = 0;
for (i = 0; i <= n - x; i++)
{
if (strncmp(s + i, substr, x) == 0)
{
count++;
}
if(count == 2)
{
*pos = j;
return 1;
}
}
}
return 0;
}
char *search_longest_repeated_substring(char *s, int n) {
int l = 0, r = n,pos = 0;
while (l < r) {
int mid = l + (r - l + 1) / 2;
if (check(s, mid, n, &pos)) l = mid;
else r = mid - 1;
}
if (l == 0) {
return NULL;
}
char *result;
result = (char*)malloc((l + 1) * sizeof(char));
if (result == NULL) {
printf("内存分配失败\n");
return NULL;
}
strncpy(result, s + pos, l);
result[l] = '\0';
return result;
}
int main() {
char *s = malloc(MAXSTRLEN * sizeof(char));
if (s == NULL)
{
printf("内存分配失败\n");
return 1;
}
scanf("%s", s);
char *result = search_longest_repeated_substring(s, strlen(s));
if (result == NULL)
{
printf("没有最长重复子串\n");
}
else
{
printf("%s\n", result);
free(result);
}
free(s);
return 0;
}
7. Caesar密码
#include <stdio.h>
#include <string.h>
void decrypt_caesar(char *cipher_text) {
char mapping[26] = {
'V', 'W', 'X', 'Y', 'Z', 'A', 'B', 'C', 'D', 'E',
'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
'P', 'Q', 'R', 'S', 'T', 'U'
};
for (int i = 0; cipher_text[i] != '\0'; i++) {
if (cipher_text[i] >= 'A' && cipher_text[i] <= 'Z') {
cipher_text[i] = mapping[cipher_text[i] - 'A'];
}
}
}
int main() {
char line[200];
while (1) {
scanf("%s", line);
if (strcmp(line, "ENDOFINPUT") == 0) {
break;
}
if (strcmp(line, "START") == 0) {
getchar();
fgets(line, 200, stdin);
decrypt_caesar(line);
printf("%s", line);
}
}
return 0;
}
8.判断等值子串
#include <stdio.h>
#include <string.h>
void longest_equal_substring(char *s) {
int max_length = 0;
char *max_substring = NULL;
int current_length = 1,i;
char *current_start = s;
for ( i = 1; s[i] != '!'; i++) {
if (s[i] == s[i - 1]) {
current_length++;
} else {
if (current_length > max_length) {
max_length = current_length;
max_substring = current_start;
}
current_length = 1;
current_start = s + i;
}
}
if (current_length > max_length) {
max_length = current_length;
max_substring = current_start;
}
if (max_length > 1) {
for (i = 0; i < max_length; i++) {
printf("%c", max_substring[i]);
}
} else {
printf("no");
}
}
int main() {
char input_str[1000];
scanf("%s", input_str);
longest_equal_substring(input_str);
return 0;
}
9.判断两个字符串是否匹配
#include <stdio.h>
#include <stdbool.h>
#define TRUE 1
#define FALSE 0
bool isMatch(char *str1, char *str2) {
if (*str1 == '\0' && *str2 == '\0')
return TRUE;
if (*str1 == '\0' || *str2 == '\0')
return FALSE;
if (*str1 == *str2 || *str2 == '?')
return isMatch(str1 + 1, str2 + 1);
if (*str2 == '*') {
char *pattern = str2 + 1;
while(*pattern == '*')
{
pattern ++;
str1++;
}
if(*pattern == '\0') return TRUE;
while(*pattern == '?')
{
pattern++;
str1++;
if (*str1 == '\0' && *pattern == '\0')
return TRUE;
if (*str1 == '\0' || *str2 == '\0')
return FALSE;
}
while (*str1 != '\0') {
if (isMatch(str1, pattern))
return TRUE;
str1++;
}
return FALSE;
}
return FALSE;
}
int main() {
char str1[1000], str2[1000];
scanf("%[^#]#", str1);
getchar();
scanf("%[^#]#", str2);
if (isMatch(str2, str1)) {
printf("yes\n");
} else {
printf("no\n");
}
return 0;
}