Khaled Mohammad
Khaled Mohammad

Reputation: 183

Array unwanted characters in C

Ok,I am beginner in C.I was thought that for a array to hold to characters in need to declare it as:

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:

#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 |/

What I expect: long f9

Ok so in total I have 4 questions: *How to fix the problem of unwanted characters and why is it giving unwated characters? *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? *How to combine 2 strings?

Upvotes: 0

Views: 401

Answers (2)

user2084865
user2084865

Reputation: 697

The first question is already answered by 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); 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?

    There is no way, instead you could copy the elements in a loop or using a function like memcpy, strcpy. 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.

  • 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 e.g. strcpy is much easier ;-)

    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". But the second calls seems to be interesting. There we copy "second" to the 7th position (start counting from 0, therefor 6). At this position was the \0-character after the first function call. 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. Quite simple, isn't it?

Upvotes: 1

Carl Norum
Carl Norum

Reputation: 224844

You need to NULL terminate your string. Extend the array by one and add 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. 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;
}

Upvotes: 5

Related Questions