简体   繁体   English

在C中排列不需要的字符

[英]Array unwanted characters in C

Ok,I am beginner in CI was thought that for a array to hold to characters in need to declare it as: 好的,我是CI的初学者,为了让需要保留字符的数组声明为:

char a[10];

So I will have 10 elements from (0 to 9) but it is not working.It is giving me unwanted characters.Can you tell me the problem is.My code: 所以我将从(0到9)有10个元素,但它不起作用,给了我不需要的字符,你能告诉我问题是我的代码:

#include<stdio.h>
#include<stdlib.h>
int main(void)
{
    printf("%s",rand_string());
}
int rand_string(void)
{
    srand(time(NULL)); 
    char a[7];
    int e;
    int d;
    a[0]='l';
    a[1]='o';
    a[2]='n';
    a[3]='g';
    a[4]=' ';
    d=(rand()%6) + 97;
    a[5]=d;
    e=(rand()%10) + 48;
    a[6]=e;
    printf("\n%s\n",a);
    return a;
}

I get results like: long f99 |/ 我得到的结果是:long f99 | /

What I expect: long f9 我的期望:长f9

Ok so in total I have 4 questions: *How to fix the problem of unwanted characters and why is it giving unwated characters? 好吧,总的来说,我有4个问题:*如何解决不需要的字符的问题,以及为什么会产生多余的字符? *Is my way of generating random numbers with limit ok? *我的方法是生成带有限制的随机数吗? *how to write the first 4 letters "long" in one line rather that for each line in an array? *如何在一行中而不是在数组的每一行中写前4个字母“ long”? *How to combine 2 strings? *如何组合2个字符串?

You need to NULL terminate your string. 您需要NULL终止您的字符串。 Extend the array by one and add a[7] = 0; 将数组扩展一并添加a[7] = 0; in there and you'll be set. 在那里,您将被安置。

Editorial note: Your program has another big problem in that you are returning a pointer to a local variable. 编者注:您的程序还有另一个问题,因为您正在返回指向局部变量的指针。 You may want to change rand_string to fill in a buffer provided by main instead. 您可能需要更改rand_string来代替main提供的缓冲区。 Here's a quick example with both of these modifications: 这是同时进行这两个修改的简单示例:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>

void rand_string(char a[8])
{
    srand(time(NULL)); 
    int e;
    int d;
    a[0]='l';
    a[1]='o';
    a[2]='n';
    a[3]='g';
    a[4]=' ';
    d=(rand()%6) + 97;
    a[5]=d;
    e=(rand()%10) + 48;
    a[6]=e;
    a[7]=0;
    printf("\n%s\n",a);
}

int main(void)
{
    char buffer[8];
    rand_string(buffer);
    printf("%s", buffer);
    return 0;
}

The first question is already answered by Carl Norum. Carl Norum已经回答了第一个问题。

  • Is my way of generating random numbers with limit ok? 我生成带有限制的随机数的方法可以吗?

    Yes, but defining a function would be nice, wouldn't it? 是的,但是定义一个函数会很好,不是吗? Calling like a[0] = randomBetween(97, 102); a[0] = randomBetween(97, 102);这样调用a[0] = randomBetween(97, 102); is much more readable though. 虽然更具可读性。

    EDIT: As in a comment above stated: you even could write 编辑:如上述评论中所述:您甚至可以编写

    a[0] = randomBetween('a', 'f'); Just a little bit more readable ;-) 更具可读性;-)

  • how to write the first 4 letters "long" in one line rather that for each line in an array? 如何在一行中而不是在数组的每一行中写前4个字母“ long”?

    There is no way, instead you could copy the elements in a loop or using a function like memcpy , strcpy . 没有办法,相反,您可以循环或使用memcpystrcpy之类的函数复制元素。 Taking your question wordly: 认真地回答您的问题:

     a[0] = 'l'; a[1] = 'o'; a[2] = 'n'; a[3] = 'g'; 

    But this is not what you want, I guess :-) See also the strcpy-example below. 但这不是您想要的,我猜:-)另请参见下面的strcpy-example。

  • How to combine 2 strings? 如何合并两个字符串?

    Again, either using a loop or the functions mentioned above: 同样,使用循环或上述功能:

     char *first = "Hello "; char *second = "World"; char combined[12]; int currentIndex = 0, i = 0; // copy characters from "first" as long we did not find a \\0 while(first[i] != 0) combined[currentIndex++] = first[i++]; i = 0; // copy characters from "second" as long we did not find a \\0 while(second[i] != 0) combined[currentIndex++] = second[i++]; // finally don't forget to null-terminate! combined[currentIndex] = 0; 

    Using eg strcpy is much easier ;-) 使用例如strcpy要容易得多;-)

     char *first = "Hello "; char *second = "World"; char combined[12]; strcpy(combined, first); strcpy(&combined[6], second); 

    What are we doing here? 我们在这里做什么? The first strcpy-call copies simply "first" to "combined". 第一个strcpy调用仅将“第一个”复制到“组合”。 But the second calls seems to be interesting. 但是第二个电话似乎很有趣。 There we copy "second" to the 7th position (start counting from 0, therefor 6). 在那里,我们将“第二个”复制到第七个位置(从0开始计数,因此为6)。 At this position was the \\0 -character after the first function call. 在此位置是第一个函数调用之后的\\0字符。 But we don't want the string to end here, so we override it with the first character of the second string. 但是我们不希望字符串在此处结束,因此我们用第二个字符串的第一个字符覆盖它。 One nice thing is that strcpy automatically copies the terminating \\0 at the end. 一件好事是strcpy会自动复制结尾的\\ 0结尾。 Quite simple, isn't it? 很简单,不是吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM