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.
Related
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.
Is there any efficient (- in terms of performance) way for printing some arbitrary string, but only until the first new line character in it (excluding the new line character) ?
Example:
char *string = "Hello\nWorld\n";
printf(foo(string + 6));
Output:
World
If you are concerned about performance this might help (untested code):
void MyPrint(const char *str)
{
int len = strlen(str) + 1;
char *temp = alloca(len);
int i;
for (i = 0; i < len; i++)
{
char ch = str[i];
if (ch == '\n')
break;
temp[i] = ch;
}
temp[i] = 0;
puts(temp);
}
strlen is fast, alloca is fast, copying the string up to the first \n is fast, puts is faster than printf but is is most likely far slower than all three operations mentioned before together.
size_t writetodelim(char const *in, int delim)
{
char *end = strchr(in, delim);
if (!end)
return 0;
return fwrite(in, 1, end - in, stdout);
}
This can be generalized somewhat (pass the FILE* to the function), but it's already flexible enough to terminate the output on any chosen delimiter, including '\n'.
Warning: Do not use printf without format specifier to print a variable string (or from a variable pointer). Use puts instead or "%s", string.
C strings are terminated by '\0' (NUL), not by newline. So, the functions print until the NUL terminator.
You can, however, use your own loop with putchar. If that is any performance penalty is to be tested. Normally printf does much the same in the library and might be even slower, as it has to care for more additional constraints, so your own loop might very well be even faster.
for ( char *sp = string + 6 ; *sp != '\0'; sp++ ) {
if ( *sp == '\n' ) break; // newline will not be printed
putchar(*sp);
}
(Move the if-line to the end of the loop if you want newline to be printed.)
An alternative would be to limit the length of the string to print, but that would require finding the next newline before calling printf.
I don't know if it is fast enough, but there is a way to build a string containing the source string up to a new line character only involving one standard function.
char *string = "Hello\nWorld\nI love C"; // Example of your string
static char newstr [256]; // String large enough to contain the result string, fulled with \0s or NULL-terimated
sscanf(string + 6, "%s", newstr); // sscanf will ignore whitespaces
sprintf(newstr); // printing the string
I guess there is no more efficient way than simply looping over your string until you find the first \n in it. As Olaf mentioned it, a string in C ends with a terminating \0 so if you want to use printf to print the string you need to make sure it contains the terminating \0 or yu could use putchar to print the string character by character.
If you want to provide a function creating a string up to the first found new line you could do something like that:
#include <stdio.h>
#include <string.h>
#define MAX 256
void foo(const char* string, char *ret)
{
int len = (strlen(string) < MAX) ? (int) strlen(string) : MAX;
int i = 0;
for (i = 0; i < len - 1; i++)
{
if (string[i] == '\n') break;
ret[i] = string[i];
}
ret[i + 1] = '\0';
}
int main()
{
const char* string = "Hello\nWorld\n";
char ret[MAX];
foo(string, ret);
printf("%s\n", ret);
foo(string+6, ret);
printf("%s\n", ret);
}
This will print
Hello
World
Another fast way (if the new line character is truly unwanted)
Simply:
*strchr(string, '\n') = '\0';
I am doing a program in C, and currently I'm having problems. I don't know how to remove the last part of the string. For example:
char str[100] = "one => two";
I want to remove => two. Can you help me? Thanks in advance.
If you want to remove the part after a particular char token then use:
char str[100] = "one => two";
char *temp;
temp = strchr(str,'='); //Get the pointer to char token
*temp = '\0'; //Replace token with null char
In C, the string's end is marked with a zero character. Thus, you can remove the end of the string by writing a zero in the correct position:
str[3] = 0;
find the place of the blank right after "one" and replace it with a '\0'
If the first part of your string always ends before the "=" and assuming it will always have the "=", you could do this:
int i = 0;
char newstr [100];
while (str[i] != '='){
i++;
}
strncpy (newstr, str, i); //copy the i first characters from a char [] to a new char []
newstr [i] = 0;
Remember to include string.h to use strncpy
The program I am writing needs to remove an ampersand character if it is the last character of a string. For instance, if char* str contains "firefox&", then I need to remove the ampersand so that str contains "firefox". Does anyone know how to do this?
Just set the last char to be '\0':
str[strlen(str)-1] = '\0';
In C, \0 indicates a string ending.
Every string in C ends with '\0'. So you need do this:
int size = strlen(my_str); //Total size of string
my_str[size-1] = '\0';
This way, you remove the last char.
To be on the safe side:
if (str != NULL)
{
const unsigned int length = strlen(str);
if ((length > 0) && (str[length-1] == '&')) str[length-1] = '\0';
}
Just for reference, the standard function strchr does just that. It effectively splits the string on a given set of characters. It works by substituting a character with 0x00
Example shamelessly stolen from: cplusplus.com
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] = "This is a firefox& string";
char * pch;
printf ("Looking for the '&' character in \"%s\"...\n",str);
pch=strchr(str,'&');
while (pch!=NULL)
{
printf ("found at %d\n",pch-str+1);
pch=strchr(pch+1,'&');
}
return 0;
}
Check the last character and if it is '&', replace it with '\0' (null terminator):
int size = strlen(my_str);
if (str[size - 1] == '&')
str[size - 1] = '\0';
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.