C, C ++ return string judgment (string pointer usage), return pointer

Source: Internet
Author: User

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.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.