[英]reversing the string without any predefined function in c
i am using below code, i don't knw what i doing wrong.我正在使用下面的代码,我不知道我做错了什么。
#include <stdio.h>
void main()
{
char a[5];
char ptr[5] = "hello";
int i,j,k=0;
for(i=0;ptr[i] !='\0'&&ptr[i]!=' ';i++){}
k=i;
for(j=0;j<=i;j++)
{
a[j] = ptr[k];
printf("%c %c %d %d %d\n",a[j],ptr[k],i,j,k);
k--;
}
printf("%s %s",a,ptr);
}
Output: Output:
5 0 5
o o 5 1 4
l l 5 2 3
l l 5 3 2
e e 5 4 1
h h 5 5 0
hello
I suppose this is what you're looking for.我想这就是你要找的。
#include <stdio.h>
int main()
{
char a[6];
char ptr[6] = "hello";
int i,j,k=0;
for(i=0;ptr[i] !='\0'&&ptr[i]!=' ';i++){}
k= i - 1;
for(j=0;j<i;j++)
{
a[j] = ptr[k];
printf("%c %c %d %d %d\n",a[j],ptr[k],i,j,k);
k--;
}
printf("%s %s",a,ptr);
}
I have edited my answer for undefined length of input string.我已经针对输入字符串的未定义长度编辑了我的答案。 Have a look:
看一看:
#include <stdio.h>
#include<string.h>
void main(){
char a[10];
char ptr[10];
scanf("%s", ptr);
for(int i = 0, j = strlen(ptr)-1; j != -1; i++, j--){
a[i] = ptr[j];
}
printf("%s", a);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.