I am new to C and am having some troubles with strings. How do I create a string of variable length containing a specified character in C? This is what I have tried but I get a compiler error:
int cLen = 8 /* Specified Length */
char chr = 'a'; /* Specified Character */
char outStr[cLen];
int tmp = 0;
while (tmp < cLen-1)
outStr[tmp++] = chr;
outStr[cLen-1] = '\0';
/* outStr = "aaaaaaaa" */
'C' language does not directly support string as a data type. Hence, to display a String in C, you need to make use of a character array. The general syntax for declaring a variable as a String in C is as follows, char string_variable_name [array_size];
To create a string of variable length:Use the Array() constructor to create an array of empty elements with length N + 1. Call the join() method on the array to join the elements with the string you want to repeat. The join method returns a new string, where the array elements are separated by the specified string.
Declaring a string is as simple as declaring a one-dimensional array. Below is the basic syntax for declaring a string. char str_name[size]; In the above syntax str_name is any name given to the string variable and size is used to define the length of the string, i.e the number of characters strings will store.
String Length in C Using Built-In Library Function The length of a string can be found using the built-in library function strlen() in string. h header file. It returns the length of the string passed to it (ignoring the null character). The string is passed to the strlen() function, which returns its length.
You can try:
char *str = malloc(cLen + 1);
memset(str, 'a', cLen);
str[cLen] = 0;
Strings in C might not be as flexible as you want, on the first look.
What you did with "char outStr[]" was to indicate you'd like a pointer to char, that can be iterated with array syntax... it creates no actual storage for the characters, because you never mentioned how many you would like to store.
In C you can have the storage decoupled from these special variables, called pointers. The example of wanting a variable length string is actually a good example of why would you want that: I want an entity that holds knowledge of where the storage is at; I want methods to allow me to change the storage size.
So, you prepare yourself to deal with dynamic memory allocation by including
#include <stdlib.h>
declare a pointer to chars by
char *cpString;
you ask for an allocation of "n" chars with
cpString=malloc(n*sizeof(char));
Now you can strcat, printf, whatever you want to do with a string that has n-1 charaters (because it must be null terminated). Specifically, you can now initialize your string with
memset(cpString,X,n-1);
cpString[n]=0;
which creates a XXXX...XXX\0 string, of n-1 characters.
When you want to change cpString storage size, here's the tricky part, you need to free the allocated memory before you request for a new storage allocation
if (cpString !=0)
{
free(cpString);
cpString=0;
}
cpString=malloc(n*sizeof(char));
otherwise the dynamic memory storage area (called a "heap") is left with an un-reclaimable piece of the old n size.
There are better allocators, that don't need free(), but I better leave you studying and practicing with malloc() free() usage.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With