某些字符串小程序

某些字符串小程序

Intro: um,大概就是大一上学期C语言基本没学,只是到了考试前才一直在看别人写的程序…

反正就很混。然后大一下学期和编程相关的一点都没接触。大二上学期的关键课程–数据结构一点都没学。只是现在才开始醒悟,去花时间学习。大概思路还是先走了语法,然后要兼顾这学期开的面向对象和计算理论与算法分析的课程。

然后这两天就写了一些大一C语言课程里的程序,就当是复习(或者是重新学习)语法。

​ 然后直接上点程序吧。

First

// 字符串互相插入.cpp : 在一个字符串指定字符位置前插入另一个字符串
//Ste-Made

#define _CRT_SECURE_NO_WARNINGS
#define SIZE 200
#include <iostream>
#include <string.h>
using namespace std;

int main()
{
    char str1[SIZE], str2[SIZE];
    char fi_string[SIZE * 2];
    memset(fi_string, '0', sizeof(fi_string));
    char target;
    int leng, count = 0;
    int leng2 ;
    cin >> str1 >> str2;
    cin >> target;
    leng = strlen(str1);
    leng2 = strlen(str2);
    for (int i = 0; i < leng; i++)
    {
        if (str1[i] == target)
            break;
        else
            count++;
    }
    for (int i = 0; i < count; i++)
    {
        fi_string[i] = str1[i];
    }
    for (int i = 0; i < leng2; i++)
    {
        fi_string[i + count] = str2[i];
    }
    for (int i = 0; i < leng - count; i++)
    {
        fi_string[i + count + leng2] = str1[count + i];
    }
    for (int i = 0; i < leng+leng2; i++)
    {
        cout << fi_string[i];
    }
    cout << endl;
    return 0;

}

题意大概是输入两个字符串,然后再输入第一个字符串中的一个字符,在其第一次出现的位置之前插入第二个字符串。刚开始想的是用某些字符串整理函数,后来就一直“烫”,也就换了个建立一个全新的字符数组,然后将两个字符串分三个部分分别录入的思路。

Second

// 字符串连接且将空格提前.cpp :    将输入的所有字符串连接并将所有空格提到最前
//Ste-Made


#define _CRT_SECURE_NO_WARNINGS
#define SIZE 200
#include<stdio.h>
#include <string.h>

int main()
{
    char str[SIZE];
    char str1[SIZE];
    int len,lenno;
    int count = 0;
    scanf("%[^\n]", str);//定制自己的扫描集
    getchar();
    scanf("%[^\n]", str1);
   
    strcat(str, str1);
    lenno=len = strlen(str);
    for (int i = 0; i < len; i++)
    {
        if (str[i] == ' ')//类似链表中的删除操作
        {
            count++;//计数器,用来确定打印空格的数目
            if (i < len - 1)
            {
                for (int j = i; j < len; j++)
                {
                    str[j] = str[j + 1];
                  
                }
                lenno--;
            }
            else
                lenno--;
        }

    }
    for (int i = 0; i < count; i++)
    {
        putchar(' ');
    }
    for (int i = 0; i < lenno; i++)
    {
        putchar(str[i]);
    }
    putchar('\n');
    return 0;
}

题意是输入两个字符串,然后把他们合并,并把所有的空格放到字符串最前面。

学到了一种自定义扫描集的方法,在scanf里面,以“%[]”的形式,表示取反集,\n即表示读入换行之前的所有字符,感觉应该是变相实现了gets ?

然后getchar()的使用,就是取消换行符的影响,即清空键盘缓冲区。

将空格提前可以分为两个步骤:1.遇到空格时把空格之后的所有元素提前,将空格覆盖.2.每次操作时统计空格数目在最后打印出来。(感觉有点像链表的删除操作)

P.S.:复习了一下strcat()函数的用法。strcat(str1,str2),即为把str2接到str1后面——即str1变长了。

Third

// 统计字符串中某一字串出现的次数.cpp :     子串长度不加限制
//Ste-Made

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include<string.h>
#define SIZE 303

int main()
{
    char str[SIZE];
    int counter = 0;
    int count = 0;
    char son_sr[SIZE];
    int len, son_len;
    scanf("%[^\n]", str);
    getchar();
    scanf("%s", son_sr);
    len = strlen(str);
    son_len = strlen(son_sr);
    for (int i = 0; i < len-son_len+1; i++)
    {
        int k = i;
        for (int j = 0; j < son_len; j++)
        {
            if (str[k] == son_sr[j])
            {
                count++;
                k++;
            }
            else
                break;
        }
        if (count == son_len)
            counter++;
        count = 0;
    }
    if(counter>1)
         printf("There are %d '%s'.\n", counter,son_sr);
    else if(counter>0)
        printf("There is %d '%s's.\n", counter, son_sr);
    else
        printf("Sorry, no '%s' found.\n", son_sr);
    return 0;
}



即输入两个字符串,统计第二个字符串作为子串在第一个字符串中出现的次数。

同样用了自定义扫描集的方法。

思路就是嵌套循环,遍历子串与源字符串中是否一一对应相等。用计数器的方法,如果计数器里层遍历一次过后,计数器达到了子串长度,即说明出现了一次。

ATTENTION:每次遍历过后记得计数器要置零

Fourth

// 删除字符串指定字符.cpp : 
//Ste-Made

#define _CRT_SECURE_NO_WARNINGS
#define SIZE 303
#include <stdio.h>
#include<string.h>

int main()
{
    char str[SIZE], str1[SIZE];
    int len, len1;
    int count;
    scanf("%s%s", str, str1);
    len = strlen(str);
    len1 = strlen(str1);
    for (int i = 0,k = 0 ; i < len; i++)
    {   
        k = i;
        for (int j = 0; j < len1; j++)
        {
            if (str[k] == str1[j])
            {
                for (count = k; count < len; count++)
                {
                    str[count] = str[count + 1];
                }
                len--;
                i--;//此处减一,跳出循环之后走外层循环i++继续回到当前位置以保证从str1[0]开始遍历 
                break;
            }
        }
        
    }
    for (int i = 0; i < len; i++)
        putchar(str[i]);
    putchar('\n');
    return 0;
}



这个程序是删除字符串中的指定字符而不是指定字符串。

思路还是嵌套循环吧。

对于里层循环,只要发现了源字符串中有出现在第二个字符串中的字符,即采用整体前移覆盖的方法实现删除。

需要注意的地方在于,每次删除之后要把当前位置减一并跳出当前循环。原因在于,每次完成删除后,当前位置就变成了后面的字符,而对于后面的一个字符的处理,并不一定是从第二个字符串的初始位置开始的。就会出现漏删的情况。

Fifth

// ASCII字符串拼接.cpp : 将字符串拼接并将结果字符串按照ascii升序排序
//Ste-Made

#define _CRT_SECURE_NO_WARNINGS
#define SIZE 300
#include <stdio.h>
#include <string.h>

int main()
{
    char str1[SIZE], str2[SIZE];
    char fi_string[SIZE * 2];
    int len;
    scanf("%s%s", str1, str2);
    strcpy(fi_string, str1);
    strcat(fi_string, str2);
    len = strlen(fi_string);
    for (int i = len-1; i; i--)
    {
        for (int j = 0; j < i; j++)
        {
            if (fi_string[j] < fi_string[j + 1])
                ;
            else
            {
                char temp;
                temp = fi_string[j];
                fi_string[j] = fi_string[j + 1];
                fi_string[j + 1] = temp;
            }
        }

    }
    for (int i = 0; i < len; i++)
        printf("%c", fi_string[i]);
    putchar('\n');
    return 0;
}

拼接字符串后再按照ASCII升序排序,核心思路就是对合并后的字符串走一个冒泡排序吧。

然后想说一下冒泡排序,每一趟都实现了什么。

从代码来看的话,bubble sort每次都比较相邻的两个字符,如果不符合条件即将二者交换。一直走到外层循环所控制的终点。而实现的结果就是,每次将外层循环所控制的终点之前的数据中的最大/最小值放到终点处。

kkkk流程图就先不放了(我不是很会放图片qaq)。

Sixth

//Ste-Made
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#define SIZE 300
char str[SIZE][SIZE];
void Input(void);


int main()
{

	int len[SIZE];
	int index=0;
	int i = 0;
	while (scanf("%s", str[i]) && strcmp(str[i], "stop")!= 0)
	{
		len[i] = strlen(str[i]);
		//printf("%d\n", len[i]);
		i++;
	
	}
	for (int j = 0; j < i; j++)
	{
		if (len[j] > index)
			index = len[j];
		else
			index = index;
	}
	//printf("%d\n", index);

	for (int j = 0; j < i; j++)
	{
		if (index == len[j])
		{
			puts(str[j]);
		}
		
	}
	
	return 0;


}

输入任意个字符串,stop终止输入。之后输出最长的那一个。

遇到的第一个问题就是当时忘记要怎么设置“stop”为终止条件了。以为用str[i]==“stop”就行。

简直愚蠢至极,后来才想起来strcmp()。

然后思路就是,用字符串数组来存放输入的每一个串,另建一个数组来存放对应字符串的长度,用一个index来找到长度数组中的最大值,并输出对应的str[i].

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值