我想弄清楚在两个字符之间获取文本需要什么。
程序将提示用户输入字符串,然后程序将提示用户输入字符。
执行此操作后,程序将使用字符作为限制打印文本。
例如:
请插入您的文字:asdhello wordasd
请插入你的角色:d
您想要的文字:hello wor
我正在考虑该怎么做,但我一无所知。
有什么建议吗?
答案 0 :(得分:2)
如果您在C. C存储string
中执行此操作character array
。您可以使用数组character
(0到字符串的长度)访问每个index
。只需浏览阵列并检查您是否找到了想要找到的角色。如果你第一次找到它,那么设置一个标志变量,如果你发现它是第二次,那么检查标志变量,无论是第二次还是第一次。复制/打印第一个和第二个找到的事件之间的所有字符。祝你好运。
答案 1 :(得分:-1)
以下是代码:
#include<stdio.h>
int main (void)
{
char text[100];
char desiredText[100];
char character;
int size, begin, end, i, j;
// to initialize as an empty string
text[0] = '\0';
desiredText[0] = '\0';
printf("Please insert your text: ");
scanf ("%[^\n]%*c", text);
// finding the size of the text input by user
size = 0;
while( text[size] != '\0' )
size++;
printf("Please insert your character: ");
scanf("%c", &character);
// finding the first occurrence of the character or, if not found stop at the end of the text
begin = 0;
while( text[begin] != character && text[begin] != '\0' )
begin++;
// checking to see if an empty string was given as text or the character was not found at all
if( begin >= 0 && begin < size )
{
// checking to see if the first occurrence was at the last character of the text
if( begin == size -1 )
{
printf("Only one occurence of %c as the last character, hence, nothing in between!\n", character);
}
else
{
// if all is good, finding the second occurrence of the character or, if not found, stop at the end of the text
end = begin + 1;
while( text[end] != character && text[end] != '\0' )
end++;
// checking if both occurrences are adjacent to each other
if( begin == end - 1 )
{
printf("Nothing in between!\n");
}
else
{
// manually copying the string and adding a terminating character \0 to denote the end
for(i = begin + 1, j = 0; i < end; i++, j++)
desiredText[j] = text[i];
desiredText[j] = '\0';
printf("Your desired text: %s\n", desiredText);
}
}
}
else
{
printf("'%c' not found in the text.\n", character);
}
return 0;
}
一些解释:
在这里,由于我们不能使用string.h
,我们将不得不进行所有字符串操作,例如查找字符串长度,将字符串复制到另一个字符串,在字符串中搜索字符以及甚至用\0
来终止字符串以表示其结束。所有这些都是在上面的代码中手动完成的。 (请参阅代码中的注释以了解。)
&&
循环中的while
,在两个条件之间,用于在两个条件的 发生时停止循环。
使用空格作为scanf ("%[^\n]%*c", text);
中用户输入的字符串的方式取自this上的答案。
答案 2 :(得分:-1)
以下是代码:
#include<stdio.h>
#include<conio.h>
int main()
{
char string[100]; //for storing character array
char ch; //for storing the character given by user
char temp='!'; //for checking each character with ch
int count=0; //counter for while loop
int flag=0; // becomes 1 after the first occurence of ch in string
// and increases after the subsequent occurences
clrscr(); // used to clear screen(optional)
printf("Enter a string\n");
scanf("%[^\n]",string); // read string from the user
printf("Enter the character: ");
scanf("%c",&ch); // read character from the user
while(temp!='\0') //temp initiated with '!' so a to enter the loop
{
temp=string[count];
if(temp==ch)
{
flag++;
}
else if(flag==1)
{
printf("%c",temp);
}
count++;
}
getch();
return 0;
}
说明:
首先,我存储了字符串和字符。
然后,一个临时字符变量temp
,用于在循环中逐个存储字符串的每个字符。
然后,temp
与ch
匹配,当temp
与ch
匹配时,flag
从0
增加到1
第一次,所有后续字符都打印在控制台上,直到第二次temp
匹配ch
,然后,flag
进一步增加,这将停止打印更多字符