反转输入的函数 string.Display 反转的字符串,但只有指针没有括号 [],没有库..函数将在内存中改变
Function to reverse the input string.Display the reversed string but just with pointer no brackets[],no libraries..function will change in memory
int *i;
ters_cevir(){
char *term=i;
char *som=i;
char som1;
while (*term != '[=11=]') { term++; }
while (*som != '[=11=]') {
som1=som*;
*term=som;
term--;
som++;
}
}
int main() {
char *isim=malloc(sizeof(char));
i=&isim;
printf("Reverse words=");
scanf("%s",isim);
printf("Kelimenizin tersi:\n ");
ters_cevir(); // When I call this, it must make the reverse one that make from memory
while (*isim != '[=11=]') {
printf("%c",*isim);
isim++;
sayac++;
}
return 0;
}
由于 som1=som*;
等语法错误,您的代码无法编译
您应该将字符串作为参数传递给 ters_cevir();
而不是类型不正确 int *
的全局变量 i
。
修复这些问题后,ters_cevir()
仍然不会达到预期的结果,因为它会用从头开始的字符覆盖从头开始的字符串,并有一个错误。
您可以通过在 *som
和 *term
处交换字符来更正此问题,但请注意在 som >= term
时停止,否则您将反转字符串两次。
此外,main
中的代码已完全损坏。
这是更正后的版本:
#include <stdio.h>
char *reverse(char *str) {
char *term = str;
char *som = str;
char c;
while (*term != '[=10=]') { term++; }
while (som < term) {
term--;
c = *som;
*som = *term;
*term = c;
som++;
}
return str;
}
int main() {
char buf[128];
printf("String to reverse: ");
if (scanf("%127[^\n]", buf) == 1) {
printf("Reversed string: %s\n", reverse(buf));
}
return 0;
}
您好,我已经修改了您的代码。请看下面也看我的评论:-
void ters_cevir(char *isim){
char *term=isim;
//char *som=isim;
//char som1;
while (*isim != '[=10=]') { isim++; }
while (*term != '[=10=]') {
//som1=som*;
*--isim=*term++//isim was pointing to the null character so we are pre decrement that pointer and post decrement term
//here we are coping the string in reverse order in isim
//term--;
//som++;
}
}
int main() {
char *isim=malloc(50);//you need enough space to store a string. you have just allocated only one byte which was not enough
//i=&isim;
printf("Reverse words=");
scanf("%s",isim);
printf("Kelimenizin tersi:\n ");
ters_cevir(isim); // now it will work fine. Here you are passing the address of isim
while (*isim != '[=10=]') {
printf("%c",*isim);
isim++;
sayac++;
}
return 0;
}
int *i;
ters_cevir(){
char *term=i;
char *som=i;
char som1;
while (*term != '[=11=]') { term++; }
while (*som != '[=11=]') {
som1=som*;
*term=som;
term--;
som++;
}
}
int main() {
char *isim=malloc(sizeof(char));
i=&isim;
printf("Reverse words=");
scanf("%s",isim);
printf("Kelimenizin tersi:\n ");
ters_cevir(); // When I call this, it must make the reverse one that make from memory
while (*isim != '[=11=]') {
printf("%c",*isim);
isim++;
sayac++;
}
return 0;
}
由于 som1=som*;
您应该将字符串作为参数传递给 ters_cevir();
而不是类型不正确 int *
的全局变量 i
。
修复这些问题后,ters_cevir()
仍然不会达到预期的结果,因为它会用从头开始的字符覆盖从头开始的字符串,并有一个错误。
您可以通过在 *som
和 *term
处交换字符来更正此问题,但请注意在 som >= term
时停止,否则您将反转字符串两次。
此外,main
中的代码已完全损坏。
这是更正后的版本:
#include <stdio.h>
char *reverse(char *str) {
char *term = str;
char *som = str;
char c;
while (*term != '[=10=]') { term++; }
while (som < term) {
term--;
c = *som;
*som = *term;
*term = c;
som++;
}
return str;
}
int main() {
char buf[128];
printf("String to reverse: ");
if (scanf("%127[^\n]", buf) == 1) {
printf("Reversed string: %s\n", reverse(buf));
}
return 0;
}
您好,我已经修改了您的代码。请看下面也看我的评论:-
void ters_cevir(char *isim){
char *term=isim;
//char *som=isim;
//char som1;
while (*isim != '[=10=]') { isim++; }
while (*term != '[=10=]') {
//som1=som*;
*--isim=*term++//isim was pointing to the null character so we are pre decrement that pointer and post decrement term
//here we are coping the string in reverse order in isim
//term--;
//som++;
}
}
int main() {
char *isim=malloc(50);//you need enough space to store a string. you have just allocated only one byte which was not enough
//i=&isim;
printf("Reverse words=");
scanf("%s",isim);
printf("Kelimenizin tersi:\n ");
ters_cevir(isim); // now it will work fine. Here you are passing the address of isim
while (*isim != '[=10=]') {
printf("%c",*isim);
isim++;
sayac++;
}
return 0;
}