C, C ++ return string judgment (string pointer usage), return pointer
Function: enter a string to determine whether it is a return.
Mainly exercise pointer usage.
1. C Edition
#include<stdio.h>int main(){ char he[100]; char a; int i=0,flag=1; while((a=getchar())!='\n') { he[i]=a; i++; } int n=i; for(i=0;i<n/2;i++) { printf("%c\t%c\n",he[i],he[n-1-i]); if(he[i]!=he[n-1-i]) { printf("no");break; } } if(flag==1) { printf("yes"); } return 0;}
The getchar () function obtains an input in sequence, assigns it to the char variable a, and then assigns it to the char array he [] through a.
When "\ n" is input, that is, the Return key jumps out of the loop.
2. C ++
#include<iostream>#include<string.h>using namespace std;int main(void){ char *p="abcba"; int n=strlen(p); bool flag=1; int i; for(i=0;i<n/2;i++) { cout<<p[i]<<"\t"<<p[n-1-i]<<endl; if(p[i]!=p[n-1-i]) { flag=0; cout<<"no"<<endl;break; } } if(flag==1) cout<<"yes"<<endl; return 0;}
Strlen () Get the length
3. C function call Edition
#include<stdio.h>#include<string.h>int pp(char *p){ int n=strlen(p),i; //printf("%d",n); if(p==NULL)return -1; for(i=0;i<n/2;i++) { // printf("%c\t%c\n",p[i],p[n-1-i]); if(p[i]!=p[n-1-i]) { return 0; } } return 1;}int main(){ char *p="abcba"; int a=pp(p); printf("%d",a);}
Pp () is an int-type function, so an int-type value is returned. It is received by declaring an int a in the main function.