当前位置: 动力学知识库 > 问答 > 编程问答 >

How to do String Reverse,count length in C++ or C?

问题描述:

How to do Reverse String & count length of string in C or C++ without using any library function??

网友答案:

I think this will help you..

char str[100], temp;
       int i, j = 0;

       printf("\nEnter the string :");
       gets(str);

       i = 0;
       j = strlen(str) - 1;

       while (i < j) {
          temp = str[i];
          str[i] = str[j];
          str[j] = temp;
          i++;
          j--;
       }

       printf("\nReverse string is :%s", str);
网友答案:
#include<iostream>
#include<stdio.h>
using namespace std;
int main()
{

 char a[20],a1[20];
 int i,j,count=0;
 cout<<"Enter any String:"<<"\n";
 gets(a);    cout<<"Reverse of the string is: ";

  for(i=1;a[i]!='\0';++i);
 for(j=i-1;j>=0;--j)
 {cout<<a[j];count++;}
 cout << "\nLength: " << count;return 0;
}
分享给朋友:
您可能感兴趣的文章:
随机阅读: