这段代码在turbo C中运行,但不在代码块中运行。如何在代码块上运行? [英] This code runs in turbo C but not in code block.how to make it run on codeblock?

查看:85
本文介绍了这段代码在turbo C中运行,但不在代码块中运行。如何在代码块上运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

//WAP to calculate length of string and to get reverse of the string without using strlen() and strrev() function in C.
#include<stdio.h>
#include<stdlib.h>


//Calculate length of the String.
int length(char *p)  /* *p ->When it is called so it will pass the string along with it just like strlen() we pass address.
                          Using p we will access the string.length function whose return type is int.
                            In parenthesis we pass string address and we make pointer(char*p).*/
{
    int i;
    for(i=0;*(p+i)!='\0';i++);  //We put; here as for body doesn't exist(It has Null Statement).
                               /*for loop working->i=0;*(p+i)means s[0]!='\0', so OK i will increment(i++),again condition
                                will check and again increment; this loop will work till ivalue=8 i.e\0.
                                  So,i=8;*(p+8)!='\0' become false and loop stops.*/
        return(i); //Means i value is 8 that we need to return as length of the string is 8.So,we write return(i);
}


//We make function that will reverse our string.
char* reverse(char *p) //char * means here we return address. p contain address of that string we need to reverse.
{
    int l,i;
    char ch;
    for(l=0;*(p+l)!='\0';l++); //l contains length.
      for(i=0;i<(l/2);i++)//We do swapping l/2 times.We start from i=0,thats why we are not writing i<=l/2.
      {
          ch=*(p+i); //*(p+i) is same as s[i].Initially, t=s[0].
	  *(p+i)=*(p+l-1-i);/*We do -i when i increase because of loop so p+l-1 value should decrease.
                              l->length,we written l-1,we get last block index. If length=8. eg. 8-1=7 index last character
                                 i.e;r. So, 0 index will swap with 7 index thats why we have written this*/
	  *(p+l-1-i)=ch;
      }//Reverse done.
   return(p);//We are returning address of that string that has been reversed.
}

//Now we make main()function to use both function- length and reverse.
int length(char *);
char* reverse(char *);
int main()
{
    printf("Length of the String = %d",length("Computer\0"));
    printf("Reverse of the String =%s",reverse("Computer\0"));
}





我的尝试:



我使用< cstring>

在main中使用return()

什么都不行



What I have tried:

I have use <cstring>
use return in main()
Nothing works

推荐答案

您正在将字符串文字(常量字符串Computer\ 0)传递给期望非const char * 参数的函数( length() reverse())。虽然这适用于 length(),但未修改字符串,但它可能不适用于 reverse()



你的代码类似于:

You are passing a string literal (the constant string "Computer\0") to functions that expect a non-const char* parameter (length() and reverse()). While this will work for length() where the string is not modified, it might not work with reverse().

You code is similar to:
int main()
{
    const char *str = "Computer\0";
    printf("Length of the String = %d",length(str);
    printf("Reverse of the String =%s",reverse(str);
}



解决方案是创建一个非常量字符串并传递:


The solution is to create a non-const string and pass that:

int main()
{
    char str[] = "Computer\0";
    printf("Length of the String = %d",length(str);
    printf("Reverse of the String =%s",reverse(str);
}


这篇关于这段代码在turbo C中运行,但不在代码块中运行。如何在代码块上运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆