因此,经过反复试验,我做到了:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*Prototype of a hashtag function*/
void hashtag(char *,char *);
int main()
{
/* Declaration of strings S and P*/
char S[100], P[100]="";
/*Getting the string S from a keyboard*/
printf("Unesi string S: ");
gets(S);
/*Calling "hashtag"*/
hashtag(S,P);
}
/*Hashtag function*/
void hashtag(char *S,char *P)
{
/*Finding the position of 2 #'s in string S*/
int i, t1, t2;
for(i=0;i<100;i++)
{
if(S[i]=='#')
{
t1=i;
break;
}
}
for(i=100;i>0;i--)
{
if(S[i]=='#')
{
t2=i;
break;
}
}
/*"Filling" the empty string P with the text in between #'s*/
int k;
for(i=t1+1,k=0;i<t2,k<(t2-t1-1);i++,k++)
{
P[k]=S[i];
}
puts(P);
}
为什么我感到这太过复杂了呢?我感觉没有必要找到确切的位置,而且可以做的比这简单得多。
void hashtag(char *S, char *P){
sscanf(S, "%*[^#]#%[^#]", P);
puts(P);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句