1、实现 mystrcpy(), mystrcmp(), mystrcat(), mystrlen() ;
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
size_t mystrlen(char c[]) {
int i = 0;
while (c[i]) {
i++;
}
//i--;
return i;
}
int mystrcmp(const char* str1, const char* str2) {
int i = 0;
while (str1[i] && str2[i]) {
if (str1[i] == str2[i]) {
i++;
}
else if (str1[i] > str2[i]) {
return 1;
}
else {
return -1;
}
}
return 0;
}
char* mystrcpy(char* to, const char* from) {
int i = 0;
while (from[i]) {
to[i] = from[i];
i++;
}
to[i] = from[i];
return to;
}
char* mystrcat(char* str1, const char* str2) {
int i = 0;
int len = strlen(str1);
while (str2[i]) {
str1[len] = str2[i];
i++;
len++;
}
str1[len] = 0;
return str1;
}
int main() {
char c[50];
char a[50];
char* p,* sp;
size_t len,standard_len;
while (gets(c) != NULL) {
len = mystrlen(c);
standard_len = strlen(c);
printf("字符串str的长度为:%d\n", len);
printf("调用标准函数字符串str的长度为:%d\n", standard_len);
printf("请输入另一个比较的字符串:\n");
gets(a);
printf("调用mystrcmp函数的结果为:%d\n", mystrcmp(c, a));
printf("调用strcmp函数的结果为:%d\n", strcmp(c, a));
p = mystrcpy(a, c);
printf("调用mystrcpy函数的结果:");
puts(p);
sp = strcpy(a, c);
printf("调用strcpy函数的结果:");
puts(sp);
gets(a);
p = mystrcat(c, a);
printf("调用mystrcat函数的结果:");
puts(p);
}
return 0;
}
2、 输入一行字符串(单词和若干空格), 输出该行单词个数。
Input :hello _ world_ how___are___you___\n
Output: 5
3. 输入一行字符串(单词和若干空格),输出该行单词(每个单词一行)
Input:hello _ world_ how___are___you___\n
Output: hello
world
how
are
you
4、输入一行字符串,将字符串反转输出
//2、输入一行字符串,输出一行单词的个数
//3、输入一行字符串,输出一行单词
//4、输入一行字符串,将字符串反转
#include <stdio.h>
int count_number(char* str) {
int i = 0;
int cnt = 0;
int flag = 1;
while (str[i]) {
if (str[i] != ' ' && flag == 1) {
i++;
cnt++;
flag = 0;
}
if (str[i] != ' ' && flag == 0) {
i++;
}
if (str[i] == ' ') {
i++;
flag = 1;
}
}
return cnt;
}
void print_word(char* str) {
int i = 0;
int j = 0;
char word[50] = {0};
while (str[i]) {
if (str[i] == ' ') {
puts(word);
memset(word, 0, sizeof(word));
j = 0;
i++;
}
else {
word[j] = str[i];
i++;
j++;
}
}
puts(word);
}
char* reserve(char* str1, char *str2) {
int len = strlen(str1);
int i = 0;
len--;
for (len; len >= 0; len--) {
str2[i] = str1[len];
i++;
}
str2[i] = 0;
//puts(a);
return str2;
}
void reserve_character(char* str1, char* str2) {
char* p,* q;
p = reserve(str1, str2);
int i = 0;
int j = 0;
char word[50] = { 0 };
char r_word[50] = { 0 };
while (p[i]) {
if (p[i] == ' ') {
q = reserve(word, r_word);
printf("%s ", q);
memset(word, 0, sizeof(word));
j = 0;
i++;
}
else {
word[j] = p[i];
i++;
j++;
}
}
printf("%s ", reserve(word, r_word));
}
int main() {
char c[50];
char a[50] = {0};
int number;
while (gets(c) != NULL) {
/*number = count_number(c);
printf("该字符串中含有的单词数量为:%d\n", number);
print_word(c);*/
printf("反转后的字符串为:\n");
reserve_character(c, a);
printf("\n");
}
return 0;
}
输入一行字符串,将字符串反转的另一种代码格式
转载过来的代码,但一直放在笔记中已经不知道原作者哪位了,仅供参考
#include <stdio.h>
#include <string.h>
void strReverse(char str[]) { //将数组中的内容反转并输出
int slen = strlen(str);
for (int i = slen - 1; i >= 0; i--) {
putchar(str[i]);
}
putchar(' ');
}
int main() {
int i = 0, flag = 0, count = 0, word_len = 0; //flag为标志位,用来通过单词间的空格区分单词数量count
char str[50]; //原字符串
char word[50]; //现字符串
int slen = 0;
printf("请输入长度不超过50的字符串 !!\n");
while (gets(str) != NULL) {
slen = strlen(str);
//逆序拣选单词,然后再将单词逆序输出。
for (i = slen - 1, count = 0, flag = 0; i >= 0; i--) {
if (flag == 0 && str[i] != ' ') { //当前遇到新单词首部,flag标志为0
count++; //单词数量++
flag = 1; //标志位至1
word_len = 0;
word[word_len++] = str[i];
}
else if (flag == 1 && str[i] != ' ') { //把单词字符逆序加入到数组中
word[word_len++] = str[i];
word[word_len] = '\0'; //末尾加上空字符
flag = 1;
}
else if (flag == 1 && str[i] == ' ') { //当flag为1,并遇到第一个空格
strReverse(word); //将word数组中的单词反转并输出
word_len = 0;
flag = 0;
}
}
strReverse(word);
printf("\ncount is %d \n", count);
}