Having char double[] = "1.2345678"; , I have to trim all digits in 4nd place and above after the . in this char * i.e make it to "1.234" .
Find the '.', advance four steps, and put the string terminator there.
Watch out so you don't pass the string terminator yourself, if the number contains less than three digits after the dot.
To help you find the dot, look up the strchr function.
One obvious solution is to replace a character by NUL, like so:
char *foo = strdup("1.2345678"); // FIXME: check return value
foo[5] = '\0';
Note that the exact position might differ, depening on how many digits appear before the '.' character.
Iterate over the string foo, change the state in passed_dot if you come across '.', and insert a NUL after 4 more characters:
char *p = foo;
int i = 0;
int passed_dot = 0;
while (p && *p) {
if (*p == '.') passed_dot = 1;
if (passed_dot) i++;
if (i == 4) {
*p = '\0';
break;
}
p++;
}
If you can't afford to buy more RAM, you may strdup() the resulting string and free() the old one in order to save memory:
new_str = strdup(foo); // and don't forget to check for NULL
free(foo);
Note that
char * double= "1.2345678";
declares a string literal. This is const so can't be directly modified. To get a modifiable string, you could declare it as
char double[] = "1.2345678";
or
char* double = strdup("1.2345678");
then insert a nul character as suggested in other answers.
Related
I have two possible strings that would be pointed to by a char *:
char *s = "this is a string";
char *s = "this is a string [this is more string]";
I want to be able to remove the brackets and their contents from the char if they exist. What is the best way to do that in C?
Thanks!
First, you must have access to memory you are permitted to modify. For hysterical raisins, C still allows you to declare pointers to constant character arrays as non-const... but you still can’t modify them.
Instead, declare yourself an array which is initialized by the constant data.
char s[] = "hello world";
That gives you a mutable array of 11+1 characters (a copy of the immutable source string).
Often, however, we want to be able to work on our strings, so we need to make sure there is enough room to play with them.
char s[100] = "hello world"; // string capacity == 99+1. In use == 11+1.
s[6] = '\0'; // now string contains "hello ". In use == 6+1.
strcat( s, "everyone" ); // now string contains "hello everyone". In use == 14+1.
To remove content from a string, you must first identify where the start and stop is:
char s[] = "this is a string [this is more string] original string";
char * first = strchr( s, '[' ); // find the '['
char * last = strchr( s, ']' ); // find the ']'
if (first and last) // if both were found...
// ...copy the end of the string over the part to be replaced
memmove( first, last+1, strlen(last+1)+1 );
// s now contains "this is a string original string"
Notice all those +1s in there? Be mindful:
of where you are indexing the string (you want to copy from after the ']' to the end of the string)
that strings must end with a nul-terminator — a zero-valued character (which we should also copy, so we added one to the strlen).
Remember, a string is just an array of characters, where the last used character is immediately followed by a zero-value character (the nul-terminator). All the methods you use to handle arrays of any other kind apply to handling strings (AKA arrays of characters).
This is one of those "fun" problems that attracts more than its share of answers.
Full credit to other answers for noting that attempts to modify a "string literal" triggers UB. (String literals are often stored in an immutable "read only" region of memory.) (Thanks to #chux-reinstatemonica for clarification.)
Taking this to the next level, below is a bit of code that handles both multiple regions bounded by a pair of delimiters ('[' & ']'), and handles nested instances, too. The code is simple enough, using cut as a counter that ensures nesting is accounted for. NB: It is presumed that the source string is "well formed".
#include <stdio.h>
int main( void ) {
char *wb =
"Once [upon] a [time] there [lived a] princess. "
"She [ was [a hottie]]. The end.";
char wo[100]; // big enough
int cut = 0;
size_t s = 0, d = 0;
for( ; wb[s]; s++ ) {
cut += ( wb[s] == '[' ); // entering region to cut?
if( !cut ) wo[ d++ ] = wb[ s ]; // not cutting, so copy
cut -= ( wb[s] == ']' ); // exiting region that was cut?
if( cut < 0 ) cut = 0; // in case of spurious ']'
}
wo[ d ] = '\0'; // terminate shortened string
puts( wb );
puts( wo );
return 0;
}
Once [upon] a [time] there [lived a] princess. She [ was [a hottie]]. The end.
Once a there princess. She . The end.
It now becomes a small challenge to, perhaps, remove multiple consecutive SPs in the output array. This could quite easily be done on the fly, and is left as an exercise.
One begins to see how something like this could be extended to make an "infix calculator program". Always something new!
Use the strchnul function defined in string.h
*(strchrnul(s, '[')) = '\0';
You may need to define the _GNU_SOURCE macro.
It won't work on string literals.
To remove the bracketed fragment(s) from the string, you cannot modify the string literal in place. You should either use an automatic array or an allocated block and construct the modified string there.
Here is an example:
#include <stdio.h>
#include <stdlib.h>
char *strip_brackets(const char *s) {
size_t i, j, len;
char *p;
// compute the modified string length
for (i = len = 0; s[i];) {
if (s[i] == '[') {
for (j = i + 1; s[j] && s[j] != ']'; j++)
continue;
if (s[j] == ']') {
i = j + 1;
continue;
}
}
len++;
i++;
}
p = malloc(len + 1);
if (p != NULL) {
// construct the modified string
for (i = len = 0; s[i];) {
if (s[i] == '[') {
for (j = i + 1; s[j] && s[j] != ']'; j++)
continue;
if (s[j] == ']') {
i = j + 1;
continue;
}
}
p[len++] = s[i++];
}
p[len] = '\0';
}
return p;
}
int main() {
const char *s = "this is a string [this is more string]";
char *s1 = strip_brackets(s);
printf("before: %s\n", s);
if (s1) {
printf("after : %s\n", s1);
free(s1);
}
return 0;
}
How should I return an empty string from a function? I tried using lcp[i] = ' ' but it creates an error. Then I used lcp[i] = 0 and it returned an empty string. However, I do not know if it's right.
Also, is it necessary to use free(lcp) in the caller function? Since I could not free and return at the same time.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_LEN 50
char *find_LCP(char str1[], char str2[]);
char *find_LCP(char str1[], char str2[]){
char * lcp = malloc(MAX_LEN * sizeof(char));
int a = strlen(str1);
int b = strlen(str2);
int min = a < b ? a : b;
for(int i = 0; i < min; i++){
if(str1[i] == str2[i])
lcp[i] = str1[i];
else
lcp[i] = 0;
}
return lcp;
}
int main()
{
char str1[MAX_LEN], str2[MAX_LEN];
char * lcp;
printf("Enter first word > ");
scanf("%s", str1);
printf("Enter second word > ");
scanf("%s", str2);
lcp = find_LCP(str1, str2);
printf("\nLongest common prefix: '%s'\n", lcp);
free(lcp);
return 0;
}
An "empty" string is just a string with the first byte zero, so you can write:
s[0] = 0;
However, it is not clear what you are trying to do. The LCP of "foo" and "fob" is "fo", not the empty string.
You can also return as soon as you find the first non-matching character, no need to go until the end.
Further, you can simply pass the output string as a parameter and have lcp be an array. That way you avoid both malloc and free:
char lcp[MAX_LEN];
...
find_LCP(lcp, str1, str2);
If you want to empty a string without using a for loop then you can do
lcp[0] = 0
but for emptying a string it was right the way you did using a for loop.
There are plenty other ways of emptying the string word by word using for loop:
lcp[i] = '\0';
and it's the right way to make string empty as letter by letter you trying to do using for loop
But if you are not using some loops and simply empty a string then you can do this.
memset(buffer,0,strlen(buffer));
but this will only work for zeroing up to the first NULL character.
If the string is a static array, you can use:
memset(buffer,0,sizeof(buffer));
Your program has a bug: If you supply two identical strings, lcp[i] = 0; never executes which means that your function will return a string which is not NUL-terminated. This will cause undefined behvaior when you use that string in your printf in main.
The fix for this is easy, NUL-terminate the string after the loop:
int i;
for (i = 0; i < min; i++){
if(str1[i] == str2[i])
lcp[i] = str1[i];
else
break;
}
lcp[i] = 0;
As for the answer to the question, an empty string is one which has the NUL-terminator right at the start. We've already handled that as we've NUL-terminated the string outside the loop.
Also, is it necessary to use free(lcp) in the caller function?
In this case, it is not required as the allocated memory will get freed when the program exits, but I'd recommend keeping it because it is good practice.
As the comments say, you can use calloc instead of malloc which fills the allocated memory with zeros so you don't have to worry about NUL-terminating.
In the spirit of code golf. No need to calculate string lengths. Pick any string and iterate through it until the current character either null or differs from the corresponding character in the other string. Store the index, then copy appropriate number of bytes.
char *getlcp(const char *s1, const char *s2) {
int i = 0;
while (s1[i] == s2[i] && s1[i] != '\0') ++i;
char *lcp = calloc((i + 1), sizeof(*lcp));
memcpy(lcp, s1, i);
return lcp;
}
P.S. If you don't care about preserving one of input strings then you can simplify the code even further and just return the index (the position of the last character of the common prefix) from the function, then put '\0' at that index into one of the strings.
I am writing my own trim() in C. There is a structure which contains all string values, the structure is getting populated from the data coming from a file which contains spaces before and after the beginning of a word.
char *trim(char *string)
{
int stPos,endPos;
int len=strlen(string);
for(stPos=0;stPos<len && string[stPos]==' ';++stPos);
for(endPos=len-1;endPos>=0 && string[endPos]==' ';--endPos);
char *trimmedStr = (char*)malloc(len*sizeof(char));
strncpy(trimmedStr,string+stPos,endPos+1);
return trimmedStr;
}
int main()
{
char string1[]=" a sdf ie ";
char *string =trim(string1);
printf("%s",string);
return 0;
}
Above code is working fine, but i don't want to declare new variable that stores the trimmed word. As the structure contains around 100 variables.
Is there any way to do somthing like below where I dont need any second variable to print the trimmed string.
printf("%s",trim(string1));
I believe above print can create dangling pointer situation.
Also, is there any way where I don't have to charge original string as well, like if I print trim(string) it will print trimmed string and when i print only string, it will print original string
elcuco was faster. but it's done so here we go:
char *trim(char *string)
{
char *ptr = NULL;
while (*string == ' ') string++; // chomp away space at the start
ptr = string + strlen(string) - 1; // jump to the last char (-1 because '\0')
while (*ptr == ' '){ *ptr = '\0' ; ptr--; } ; // overwrite with end of string
return string; // return pointer to the modified start
}
If you don't want to alter the original string I'd write a special print instead:
void trim_print(char *string)
{
char *ptr = NULL;
while (*string == ' ') string++; // chomp away space at the start
ptr = string + strlen(string) - 1; // jump to the last char (-1 because '\0')
while (*ptr == ' '){ ptr--; } ; // find end of string
while (string <= ptr) { putchar(*string++); } // you get the picture
}
something like that.
You could the original string in order to do this. For trimming the prefix I just advance the pointer, and for the suffix, I actually add \0. If you want to keep the original starting as is, you will have to move memory (which makes this an O(n^2) time complexity solution, from an O(n) I provided).
#include <stdio.h>
char *trim(char *string)
{
// trim prefix
while ((*string) == ' ' ) {
string ++;
}
// find end of original string
char *c = string;
while (*c) {
c ++;
}
c--;
// trim suffix
while ((*c) == ' ' ) {
*c = '\0';
c--;
}
return string;
}
int main()
{
char string1[] = " abcdefg abcdf ";
char *string = trim(string1);
printf("String is [%s]\n",string);
return 0;
}
(re-thinking... is it really O(n^2)? Or is it O(2n) which is a higher O(n)...? I guess depending on implementation)
You can modify the function by giving the output in the same input string
void trim(char *string)
{
int i;
int stPos,endPos;
int len=strlen(string);
for(stPos=0;stPos<len && string[stPos]==' ';++stPos);
for(endPos=len-1;endPos>=0 && string[endPos]==' ';--endPos);
for (i=0; i<=(endPos-stPos); i++)
{
string[i] = string[i+stPos];
}
string[i] = '\0'; // terminate the string and discard the remaining spaces.
}
...is there any way where i don't have to charge original string as well, like if i do trim(string) it will print trimmed string and when i print only string, it will print original string – avinashse 8 mins ago
Yes, though it gets silly.
You could modify the original string.
trim(string);
printf("trimmed: %s\n", string);
The advantage is you have the option of duplicating the string if you want to retain the original.
char *original = strdup(string);
trim(string);
printf("trimmed: %s\n", string);
If you don't want to modify the original string, that means you need to allocate memory for the modified string. That memory then must be freed. That means a new variable to hold the pointer so you can free it.
char *trimmed = trim(original);
printf("trimmed: %s\n", trimmed);
free(trimmed);
You can get around this by passing a function pointer into trim and having trim manage all the memory for you.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void trim(char *string, void(*func)(char *) )
{
// Advance the pointer to the first non-space char
while( *string == ' ' ) {
string++;
}
// Shrink the length to the last non-space char.
size_t len = strlen(string);
while(string[len-1]==' ') {
len--;
}
// Copy the string to stack memory
char trimmedStr[len + 1];
strncpy(trimmedStr,string, len);
// strncpy does not add a null byte, add it ourselves.
trimmedStr[len] = '\0';
// pass the trimmed string into the user function.
func(trimmedStr);
}
void print_string(char *str) {
printf("'%s'\n", str);
}
int main()
{
char string[]=" a sdf ie ";
trim(string, print_string);
printf("original: '%s'\n", string);
return 0;
}
Ta da! One variable, the original is left unmodified, no memory leaks.
While function pointers have their uses, this is a bit silly.
It's C. Get used to managing memory. ¯\_(ツ)_/¯
Also, is there any way where I don't have to charge original string as
well, like if I print trim(string) it will print trimmed string and
when i print only string, it will print original string
Yes you can, but you cannot allocate new memory in the trim function as you will not be holding the return memory.
You can have a static char buffer in the trim function and operate on it.
Updated version of #elcuco answer.
#include <stdio.h>
char *trim(char *string)
{
static char buff[some max length];
// trim prefix
while ((*string) == ' ' ) {
string++;
}
// find end of original string
int i = 0;
while (*string) {
buff[i++] = *string;
string++;
}
// trim suffix
while ((buff[i]) == ' ' ) {
buff[i] = '\0';
i--;
}
return buff;
}
int main()
{
char string1[] = " abcdefg abcdf ";
char *string = trim(string1);
printf("String is [%s]\n",string);
return 0;
}
With this you don't need to worry about holding reference to trim function return.
Note: Previous values of buff will be overwritten with new call to trim function.
If you don't want to change the original, then you will need to make a copy, or pass a second array of sufficient size as a parameter to your function for filling. Otherwise a simple in-place trmming is fine -- so long as the original string is mutable.
An easy way to approach trimming on leading and trailing whitespace is to determine the number of leading whitespace characters to remove. Then simply use memmove to move from the first non-whitespace character back to the beginning of the string (don't forget to move the nul-character with the right portion of the string).
That leaves only removing trailing whitespace. An easy approach there is to loop from the end of the string toward the beginning, overwriting each character of trailing whitespace with a nul-character until your first non-whitespace character denoting the new end of string is found.
A simple implementation for that could be:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#define DELIM " \t\n" /* whitespace constant delimiters for strspn */
/** trim leading and trailing whitespace from s, (s must be mutable) */
char *trim (char *s)
{
size_t beg = strspn (s, DELIM), /* no of chars of leading whitespace */
len = strlen (s); /* length of s */
if (beg == len) { /* string is all whitespace */
*s = 0; /* make s the empty-string */
return s;
}
memmove (s, s + beg, len - beg + 1); /* shift string to beginning */
for (int i = (int)(len - beg - 1); i >= 0; i--) { /* loop from end */
if (isspace(s[i])) /* checking if char is whitespace */
s[i] = 0; /* overwrite with nul-character */
else
break; /* otherwise - done */
}
return s; /* Return s */
}
int main (void) {
char string1[] = " a sdf ie ";
printf ("original: '%s'\n", string1);
printf ("trimmed : '%s'\n", trim(string1));
}
(note: additional intervening whitespace was added to your initial string to show that multiple intervening whitespace is left unchanged, the output is single-quoted to show the remaining text boundaries)
Example Use/Output
$ ./bin/strtrim
original: ' a sdf ie '
trimmed : 'a sdf ie'
Look things over and let me know if you have further questions.
I need to replace " (ASCII value 34) char by empty character "".
In output, instead of quote i get an "?" question mark character.
I tried to use things like:
mystring[itit] = "";
mystring[itit] = '';
mystring[itit] = "\O";
My code:
strcpy( mystring ,op->data.value.str );
for(itit=0;itit<10;itit++)
{
if(mystring[itit] == 34)
{
mystring[itit] = NULL;
}
}
printf( "%s\n",mystring);
Any ideas how to fix that?
For clarification: the strings in mystring are like:
"hello"
"place "
"school"
all with the quotation marks - I Actually need to remove them and get:
hello
place
school
int removeChar(char *str, char c) {
int i, j;
for(i = 0, j = 0 ; str[i] ; i++){
if( str[i] == c) continue; // skip c do not copy it
str[j] = str[i]; // shift characters left
j++;
}
str[j]=0; // terminate the string
return j; // return the actual size
}
What you need to do is remove the character, not replace it, since you're not replacing it with anything. To do this, when you find the character is question, you need to move the remaining characters down.
int i,j;
strcpy(mystring, "aa\"bb\"cc");
for(i=0,j=0;i<10;i++)
{
if(mystring[i] != '"')
{
mystring[j] = mystring[i];
j++;
}
}
mystring[j] = '\0';
printf("mystring=%s\n",mystring);
Result:
mystring=aabbcc
To remove a character from a string, you can do this:
void remove(char* str, char rm)
{
char *src, *dst;
for (src = dst = str; *src != '\0'; ++src) {
*dst = *src;
if (*dst != rm) ++dst;
}
*dst = '\0'; /*insert terminator at the new place*/
}
and call with rm equal to 34.
This algorithm is well-known; I've adopted it from Kernighan & Ritchie. Do study it carefully with your debugger.
In C, strings are simply arrays of characters with a NUL (0) at the end. (They cannot contain NULs.) As with any array, you can't simply "remove" an element. You need to shift all the following elements one position, with the result that there will be an unneeded element at the end. With strings this extra element isn't a huge problem becauyse the NUL still identifies where the string ends.
In this case, you are copying the string first, so you might as well copy it without the characters you want to delete. Unless you know how many such characters there are, you will need to have allocated enough space in the new string for the entire string you want to copy:
/* Before this, you must ensure that mystring has enough space */
{
char* out = mystring;
const char* in = op->data.value.str;
do {
if (*in != '"') *out++ = *in;
} while (*in++);
}
Note: I use the fact that strings are NUL-terminated to terminate the loop, which saves me from having to know in advance how long op->data.value.str is. For this reason, I use character pointers rather than indexes.
There is no "empty character". A string can be empty by having no characters, but a character is an atomic element and can't be empty, like a box of apples can be empty, but one can't have an "empty apple".
Instead, you need to remove the quotes and close the space they took up. Better yet, if you do the copying yourself, just don't copy them:
char *psrc = op->data.value.str;
char *pdest = mystring;
while (*psrc != '\0')
{
if (*psrc != '\"')
{
*pdest = *psrc;
++pdest;
}
++psrc;
}
*pdest = '\0';
You can use this to strip all '\"'-characters:
void stripquotes(char *ptr) {
char *ptr2 = ptr;
do {
*ptr2 = *ptr++;
if (*ptr2 != '\"')
ptr2++;
} while (*ptr);
}
In C, how do I extract the first n characters from a string until I find a space in the string? Essentially, which C function will find the position of the next space for me and which C function will give me a substring? I am thinking in terms of C++. such as:
string str = "Help me Please!";
int blankPos = str.find(' ');
str.substr(0, blankPos);
Thanks,
Use strchr to find the space.
Allocate a new char buffer to hold the substring.
Copy the substring into the buffer with memcpy.
hint: strchr()
I need to type some more characters.
char str[] = "Help me Please"; // Source string
char newstr[80]; // Result string
// Copy substring characters until you reach ' ' (i.e. "Help")
for (i=0; str[i] != ' '; i++) {
newstr[i] = str[i];
}
newstr[i] = 0; // Add string terminator at the end of substring
If you just want to get the first part of the string, use strchr() as everyone has suggested. If you're looking to break a string into substrings delimited by spaces, then look into strtok().
So you want something like:
#include <string.h>
const char *str = "Help me Please";
//find space charachter or end of string if no space found
char *substr, *space = strchr(str, ' ');
int len = space ? (space-str) : strlen(str);
//create new string and copy data
substr = malloc(len+1);
memcpy(substr, str, len);
substr[len] = 0;
char* str = "Help me Please";
int i =0;
//Find first space
while(str[i] != ' '){
i++;
}
char* newstr;
newstr = strndup(str+0,i);
I guess you could also use strchr() to get the first space in the string.
Another variant allowing to use more than one character as delimitter.
char str[] = "Help me Please";
char newstr[80];
char *p = strpbrk(str, " \t\xA0"); /* space, tab or non-breaking space (assuming western encoding, that part would need adaptation to be trule portable) */
if(p)
strlcpy(newstr, str, p - str + 1);
else
newstr[0] = 0;
strlcpy is not standard but widespread enough to be used. If it is not available on the platform, it's easy to implement. Note that strlcpy always puts a 0 at the last position copied, therfore the +1 in the length expression.