c++ 字符串配对int型_配对情况:C ++程序在字符串的两个相同字符之间输入“ *”...

c++ 字符串配对int型

Given a string, compute recursively a new string where identical chars that are adjacent in the original string are separated from each other by a "*".

给定一个字符串,请递归计算一个新字符串,其中原始字符串中相邻的相同字符由“ *”分隔

Sample Input 1: "hello"

示例输入1: “ hello”

Sample Output 1: "hel*lo"

样本输出1: “ hel * lo”

Sample Input 2: "xxyy"

样本输入2: “ xxyy”

Sample Output 2: "x*xy*y"

样本输出2: “ x * xy * y”

Sample Input 3: "aaaa"

样本输入3: “ aaaa”

Sample Output 3: "a*a*a*a"

样本输出3: “ a * a * a * a”

Explanation:

说明:

In this question, we have to add a star between any pair of same letters. This could be easily achieved using recursion. When the start index is equal to (start + 1) index, we will shift all the letters from (start + 1) by 1 on the right side and on (start + 1), we will add a star.

在这个问题中,我们必须在任何一对相同的字母之间加一个星号 。 使用递归可以轻松实现。 当开始索引等于( 开始+ 1 )索引时,我们将在( 开始+ 1 )的右侧将所有字母从( 开始+ 1 )移位1,然后在( 开始+ 1 )上移位,我们将添加一个星号。

Algorithm:

算法:

  1. STEP 1: Declaring a recursive function pairStar with parameters (char arr[], int start)

    步骤1:使用参数(char arr [],int开始)声明一个递归函数pairStar

  2. STEP 2: Base Case: if(arr[start] == '\0') return;

    步骤2:基本情况: if(arr [start] =='\ 0')return;

  3. STEP 3: if(start == start +1)

    步骤3: if(start == start +1)

    Shift all the letters from

    将所有字母从

    start+1 to right side by 1.

    从1开始右移+1。

  4. STEP 4: Enter '*' at (start +1) in arr.

    步骤4:在arr中的(开始+1)输入'*'

Example:

例:

    Input = "aaa"
    First Traversal: "aa*a"
    Second Traversal: "a*a*a"


C++ program:

C ++程序:

#include <iostream>
using namespace std;

//Function To find length
int length(char arr[]){
	int len = 0;
	for(int i =0;arr[i]!='\0';i++){
		len++;
	}
	return len;
}

//Recursive Function
void pairStar(char arr[],int start){
	//Base Case: Reached End Of String
	if(arr[start]=='\0'){
		return;
	}   
	//Recursive Call 
	pairStar(arr,start+1);
	if(arr[start] == arr[start+1]){
		int l = length(arr);
		//Extending the string
		arr[l+1] = '\0';
		int i;
		//To shift the letters by 1
		for(i = l-1;i>=start +1;i--){
			arr[i+1] = arr [i];
		}
		//Entering * in between
		arr[start+1] = '*';
	}
}

//Main
int main(){
	char input[50];
	cout<<"Enter Input"<<endl;
	cin>> input;

	pairStar(input,0);  //Calling the function

	cout<<"Modified Output"<<endl;
	cout<<input<<endl;

	return 0;
}

Output

输出量

Enter Input
hello
Modified Output
hel*lo


翻译自: https://www.includehelp.com/cpp-programs/pair-case-program-to-enter-asterisk-between-two-identical-characters-in-a-string.aspx

c++ 字符串配对int型

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 之间加上一个空格。 回答:可以使用循环遍历字符串,每次取出两个字符并在它们之间加上一个空格,最后输出处理后的字符串即可。 示例代码: ```c #include <stdio.h> #include <string.h> int main() { char str[100]; printf("请输入一个字符串:"); scanf("%s", str); int len = strlen(str); for (int i = ; i < len; i += 2) { printf("%c%c ", str[i], str[i+1]); } printf("\n"); return ; } ``` 例如,输入字符串"hello world",输出结果为:"he ll o wo rl d"。 ### 回答2: 首先,我们可以使用Python编程来实现这个功能。以下是一个实例代码片段: ```python def output_pairs(str): pairs = [] for i in range(0, len(str)-1, 2): pair = str[i] + str[i+1] pairs.append(pair) return pairs input_str = input("请输入一个字符串:") result = output_pairs(input_str) print("这个字符串中每两个字符为:", result) ``` 在这个代码中,我们首先定义了一个名为`output_pairs()`的函数,它接受一个字符串作为输入参数。然后,我们使用一个`for`循环来迭代字符串中的每两个字符,并将它们存储在名为`pairs`的空列表中。最后,我们返回这个列表作为函数的输出。 接下来,我们使用`input()`函数来读取用户输入字符串,并将该字符串作为参数传递给`output_pairs()`函数。然后,我们打印出每两个字符的结果。 例如,如果用户输入字符串 `"abcdefg"`,则程序将输出 `["ab", "cd", "ef"]`,因为它将字符两两配对并存储在列表中。 这个程序实现了将字符串中的每两个字符提取出来并打印的功能。希望这能帮到你! ### 回答3: 可以使用循环遍历字符串的每个字符,然后依次输出相邻的两个字符。具体实现如下: ```python # 定义一个函数,参数为字符串s def output_pairs(s): # 遍历字符串的每个字符(除了最后一个字符) for i in range(len(s)-1): # 输出相邻的两个字符 print(s[i] + s[i+1]) # 输入字符串 input_str = input("请输入字符串:") # 调用函数输出相邻字符 output_pairs(input_str) ``` 这段代码首先定义了一个名为`output_pairs`的函数,用于输出字符串中的相邻字符。然后,通过`input`函数获取用户输入字符串,并将其赋值给变量`input_str`。最后,调用`output_pairs`函数,传入用户输入字符串作为参数,即可实现输出相邻字符的功能。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值