如何做字符串反转,以C++或C计算长度

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

本文关键字:计算 C++ 字符串 何做      更新时间:2023-10-16

如何在不使用任何库函数的情况下在C或C++中执行反向字符串和计算字符串的长度?

我认为这会对你有所帮助。

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]!='';++i);
 for(j=i-1;j>=0;--j)
 {cout<<a[j];count++;}
 cout << "nLength: " << count;return 0;
}