我正在尝试做一些像打印字符串反向那样简单的事情。 示例:
Hello World! This is me
需要的O / P:
me is This World! Hello
我的代码是这样的:
#include<stdio.h>
#include<string.h>
int main(){
char *arr[20] ;
int i,j;
int size;
char *revarr[20];
printf(" enter the number of words\n");
scanf("%d",&size);
for(i=0;i<size;i++)
scanf("%s",&arr[i]);
for(i=0;i<size;i++)
{
printf("%s\n",&arr[size-1-i]); //overwritten words
revarr[i]=arr[size-1-i];
}
printf(" the reversed sentence is %s\n",(char *)revarr);
}
我除了arr [0],arr [1]等是单独的实体,但在打印和存储它们时,它们看起来像这样重叠: I / P:
Hello World
O / P:
World
HellWorld
the reversed sentence is WorlHell@#$
我似乎无法弄清楚出了什么问题! 提前谢谢!
编辑: 在打印时
printf(&arr[0]);
printf(&arr[1]);
我明白了:
HellWorld
World
我期望它打印的是
Hello
World
答案 0 :(得分:8)
您将arr
和revarr
声明为char
指针数组。您需要为其元素动态分配内存
另请注意,您在语句中不需要&
scanf("%s",&arr[i]);
和
printf("%s\n", &arr[size-1-i]);
// ^No need of &
以下是代码的修改版本。请注意,无需使用revarr
来反转字符串。
#include<stdio.h>
#include<stdlib.h>
int main(){
size_t i, size;
printf("Enter the number of words\n");
scanf("%d", &size);
char *arr[size] ; // Variable length array. Supported by C99 and latter
for(i = 0; i < size; i++)
{
arr[i] = malloc(20); // Assumimg words are no longer than 20 characters
scanf("%s", arr[i]);
}
printf("The reversed sentence is:\n");
for(i = size-1; i >= 0; i--) // Run loop in reverse order and print words
printf("%s ", arr[i]);
}
答案 1 :(得分:2)
在使用它们读取字符串之前,你还没有为arr[0], arr[1],
等分配内存
scanf("%s",&arr[i]);
这是未定义行为的原因。你需要这样的东西:
int main(){
char *arr[20] ;
int i,j;
int size;
char *revarr[20];
printf(" enter the number of words\n");
scanf("%d",&size);
for(i=0;i<size;i++)
{
// Allocate memory.
// make it large enough to hold the input
arr[i] = malloc(100);
scanf("%s", arr[i]);
}
for(i=0;i<size;i++)
{
revarr[i]=arr[size-1-i];
}
printf(" the reversed sentence is: ");
for(i=0;i<size;i++)
{
printf("%s ", revarr[i]);
}
printf("\n");
// Deallocate the memory.
for(i=0;i<size;i++)
{
free(arr[i]);
}
return 0;
}
答案 2 :(得分:2)
这是解决问题的好方法:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
/* the string line will contain the line of the input */
char line[100];
/* read the string with the function f gets */
fgets(line,100,stdin);
/* the tab will contain all the string of the variable line */
char *tab[20];
/* the variable p will point to each string of the line */
char *p=NULL;
/* we extract the strings of the line via the function strtok */
p=strtok(line," ");
int nb=-1;
while (p!=NULL)
{
nb++;
/* we allocate a space memory fo every str ing */
tab[nb]=malloc(sizeof(char)*100);
strcpy(tab[nb],p);
p=strtok(NULL," ");
}
/* there is an exception with the last string of the line we need to take care o f it */
tab[nb][strlen(tab[nb])-1]='\0';
int i;
/* print the strings in reverse or der */
for (i=nb;i>=0;i--)
{
printf("%s ",tab[i]);
/* dont forget to free the space memory at the end of the prog ram */
free(tab[i]);
}
printf("\n");
return 0;
}
答案 3 :(得分:1)
您需要以下
#include <stdio.h>
#include <string.h>
int main(void)
{
size_t size;
printf( "enter the number of words: " );
scanf( "%zu", &size );
char arr[size][20];
char revarr[size][20];
for ( size_t i = 0; i < size; i++ ) scanf( "%s", arr[i] );
printf( "\n" );
for ( size_t i = 0; i < size; i++ ) strcpy( revarr[i], arr[size-i-1] );
printf( "the reversed sentence is" );
for ( size_t i = 0; i < size; i++ ) printf( " %s", revarr[i] );
printf( "\n" );
return 0;
}
如果要输入
2
Hello World
然后输出
World Hello
考虑到只有在编译器支持C99时才会编译代码。否则,您必须为字符数组动态分配内存。
至于你的代码,它有未定义的行为,整体上是无效的。您没有为数组arr
和revarr
的每个元素分配内存。您不能将一个数组分配给另一个数组。相反,你必须使用标准函数strcpy
等等。