Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a string of variable length with character X in C

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" */
like image 564
recursion.ninja Avatar asked Sep 10 '12 13:09

recursion.ninja


People also ask

How do you create a string variable in C?

'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];

How do you write a variable length string?

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.

How do you declare a string in X?

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.

How do you get a length of a string C?

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.


2 Answers

You can try:

char *str = malloc(cLen + 1);
memset(str, 'a', cLen);
str[cLen] = 0;
like image 56
cnicutar Avatar answered Sep 23 '22 17:09

cnicutar


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.

like image 21
jpinto3912 Avatar answered Sep 22 '22 17:09

jpinto3912