gpt4 book ai didi

c - 从最后一次出现的字符开始缩短 C 字符串?

转载 作者:行者123 更新时间:2023-12-02 21:47:35 25 4
gpt4 key购买 nike

所以我想做的是从最后一次出现的字符中剪切一个字符串。例如

input =  "Hellomrchicken"
input char = "c"
output = "cken"

问题是我无法让计数工作,因此我无法测试逻辑。我希望使用指针来执行此操作,理论上我会测试指针内的内容是否 == 为空值。我在这里使用了 while 循环。感谢任何帮助,谢谢!

#include <stdio.h>
#include <stdlib.h>

char *stringcutter(char *s, char ch);
int count( char *s);

void main(){
char input[100];
char c;
printf("Enter a string \n");
gets(input);
printf("Enter a char \n");
scanf("%c", &c);
stringcutter( *input , c );
getchar();
getchar();
getchar();
}


char *stringcutter(char *s, char ch){
int count = 0;
// Count the length of string

// Count the length of string
while ( check != '\0'){
count++;
s++;
printf("Processed");


printf("TRANSITION SUCCESSFUL /n");
printf( "Count = %d /n" , count);


// Count backwards then print string from last occurence

/* for (i=count ; i != 0 ; i--){
if (str[i] == ch)
*s = *str[i];
printf ( "Resultant string = %s", *s )
*/
return 0;
}

抱歉不知道为什么代码中途被切断

最佳答案

原始帖子并没有明确说明您是否想从头开始定义此函数,但它存在于 string.h 中,并且看起来像这样

#include <stdio.h>
#include <string.h>

int main ()
{
char input[] = "Hellomrchicken";
char c = 'c';
char *p;
p = strrchr(input, c);
printf("Last occurence of %c found at %d \n", c, p-input+1);
return 0;
}

关于c - 从最后一次出现的字符开始缩短 C 字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19264651/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com