How to get the last part of a string in C - c

I have a string in C that contains a file path like "home/usr/wow/muchprogram".
I was wondering in C how I can get the string after the last "/". So Then I could store it as a variable. That variable would equal "muchprogram" to be clear.
I am also wondering how I could get everything before that final "/" as well. Thanks in advance.

Start scanning the string from the end. Once you get a / stop. Note the index and copy from index+1 to last_index, to a new array.
You get everything before the final / as well. You have the index. Start copying from start_index to index-1, to a new array.

Someone else already suggested this, but they forgot to include the C. This assumes it is ok to mutate the source string. Tested with GCC 4.7.3.
#include <stdio.h>
#include <string.h>
int main() {
char* s = "home/usr/wow/muchprogram";
int n = strlen(s);
char* suffix = s + n;
printf("%s\n%s\n", s, suffix);
while (0 < n && s[--n] != '/');
if (s[n] == '/') {
suffix = s + n + 1;
s[n] = '\0';
}
printf("%s\n%s\n", s, suffix);
return 0;
}

Search backwards from the end of the string until you find a '/'. The pointer to the next index from there is the string of everything after that. If you copy everything up to, but not including, the '/' into another string (or replace the '/' with '\0'), you obtain the string of everything before the last '/'.

http://pubs.opengroup.org/onlinepubs/9699919799/functions/strrchr.html
strrchr(3) has been there since C89 for that purpose.
#include <stdio.h>
#include <string.h>
static void find_destructive(char *s) {
char *p_sl = strrchr(s, '/');
if (p_sl) {
*p_sl = '\0';
printf("[%s] [%s]\n", s, p_sl + 1);
} else {
printf("Cannot find any slashes.\n");
}
}
static void find_transparent(const char *s) {
const char *p_sl = strrchr(s, '/');
if (p_sl) {
char *first = (char *)malloc(p_sl - s + 1);
if ( ! first) {
perror("malloc for a temp buffer: ");
return;
}
memcpy(first, s, p_sl - s);
first[p_sl - s] = '\0';
printf("[%s] [%s]\n", first, p_sl + 1);
free(first);
} else {
printf("Cannot find any slashes.\n");
}
}
int main() {
char s[] = "home/usr/wow/muchprogram";
find_transparent(s);
find_destructive(s);
return 0;
}
http://ideone.com/vApvqp

You can solve this in c# as follows..
Var tokens = Str.Split('/');
Var lastItem = tokens[tokens.Length-1];
Var everythingBeforeLastItem = string.Empty;
Enumerate.Range(0,tokens.Length-3).ToList().
ForEach(i => everythingBeforeLastItem = everythingBeforeLastItem+tokens[i]+"\");
EverythingBeforeLastItem += tokens[tokens.Length-2];
You can use StringBuilder for efficiency if you expect a deeper path resulting in large number of tokens..

Related

reduce the size of a string

(disclaimer: this is not a complete exercise because I have to finish it, but error occurred in this part of code)
I did this exercise to practice memory allocation.
create a function that takes an url (a C string) and returns the name of the website (with "www." and with the extension).
for example, given wikipedia's link, "http://www.wikipedia.org/", it has to return only "www.wikipedia.org" in another string (dynamically allocated in the heap).
this is what I did so far:
do a for-loop, and when "i" is greater than 6, then start copying each character in another string until "/" is reached.
I need to allocate the other string, and then reallocate that.
here's my attempt so far:
char *read_website(const char *url) {
char *str = malloc(sizeof(char));
if (str == NULL) {
exit(1);
}
for (unsigned int i = 0; url[i] != "/" && i > 6; ++i) {
if (i <= 6) {
continue;
}
char* s = realloc(str, sizeof(char) + 1);
if (s == NULL) {
exit(1);
}
*str = *s;
}
return str;
}
int main(void) {
char s[] = "http://www.wikipedia.org/";
char *str = read_website(s);
return 0;
}
(1) by debugging line-by-line, I've noticed that the program ends once for-loop is reached.
(2) another thing: I've chosen to create another pointer when I've used realloc, because I have to check if there's memory leak. Is it a good practice? Or should I've done something else?
There are multiple problems in your code:
url[i] != "/" is incorrect, it is a type mismatch. You should compare the character url[i] with a character constant '/', not a string literal "/".
char *s = realloc(str, sizeof(char) + 1); reallocates only to size 2, not the current length plus 1.
you do not increase the pointers, neither do you use the index variable.
instead of using malloc and realloc, you should first compute the length of the server name and allocate the array with the correct size directly.
Here is a modified version:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *read_website(const char *url) {
// skip the protocol part
if (!strncmp(url, "http://", 7))
url += 7;
else if (!strncmp(url, "https://", 8))
url += 8;
// compute the length of the domain name, stop at ':' or '/'
size_t n = strcspn(url, "/:");
// return an allocated copy of the substring
return strndup(url, n);
}
int main(void) {
char s[] = "http://www.wikipedia.org/";
char *str = read_website(s);
printf("%s -> %s\n", s, str);
free(str);
return 0;
}
strndup() is a POSIX function available on many systems and that will be part of the next version of the C Standard. If it is not available on your target, here is a simple implementation:
char *strndup(const char *s, size_t n) {
char *p;
size_t i;
for (i = 0; i < n && s[i]; i++)
continue;
p = malloc(i + 1);
if (p) {
memcpy(p, s, i);
p[i] = '\0';
}
return p;
}
The assignment doesn't say the returned string must be of minimal size, and the amount of memory used for URLs is minimal.
Building on chqrlie's solution, I'd start by finding the beginning of the domain name (skipping the protocol portion), duplicate the rest of the string, and then truncate the result. Roughly:
char *prot[] = { "http://", "https://" };
for( int i=0; i < 2; i++ ) {
if( 0 == strncmp(url, http, strlen(prot)) )
s += strlen(prot);
break;
}
}
char *output = strdup(s);
if( output ) {
size_t n = strcspn(output, "/:");
output[n] = '\0';
}
return output;
The returned pointer can still be freed by the caller, so the total "wasted" space is limited to the trailing part of the truncated URL.

How to extract some formatted string from the buffer using scanf?

I need to extract both "rudolf" and "12" from that long string: "hello, i know that rudolph=12 but it so small..." using scanf, how can I do it?
This buffer can contains any formatted strings like ruby=45 or bomb=1, and I dont know it in advance.
I am trying something like that, but it was unsuccessful
#include <stdio.h>
int main()
{
char sentence[] = "hello, i know that rudolph=12 but it so small...";
char name[32];
int value;
sscanf(sentence, "%[a-z]=%d", name, &value);
printf("%s -> %d\n", name, value);
getchar();
return 0;
}
Iterate through the sentence using a temporary pointer and %n to extract each sub-string.
%n will give the number of characters processed by the scan to that point. Add that to the temporary pointer to advance through the sentence.
Try to parse from each sub-string the name and value. The scanset %31[^=] will scan a maximum of 31 characters, leaving room in name for a terminating zero. It will scan all characters that are not an =. Then the format string will scan the = and try to scan an integer.
#include <stdio.h>
#include <stdlib.h>
int main (void) {
char sentence[] = "hello, i know that rudolph=12 but it so small...";
char string[sizeof sentence] = "";
char name[32] = "";
char *temp = sentence;
int value = 0;
int count = 0;
int parsed = 0;
while (1 == sscanf(temp, "%s%n", string, &count)) {
temp += count;
if (2 == sscanf(string, "%31[^=]=%d", name, &value)) {
parsed = 1;
break;
}
}
if (parsed) {
printf("%s %d\n", name, value);
}
return 0;
}
You can write your own string serach engine. Which could be quite simple, let's for example:
advance until you find one of [a-z]
remember position
advance until the end of [a-z]
check if it's = now
if it is, there was our variable name
advance until end of value
return it
if there's no =, omit everything that is not a [a-z] ie. can't be variable name
A sample program:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
struct find_assignment_s {
const char *line;
const char *var;
size_t varlen;
const char *val;
size_t vallen;
};
struct find_assignment_s find_assignment_init(const char *line) {
return (struct find_assignment_s){.line = line};
}
int find_assignment(struct find_assignment_s *t) {
while (*t->line) {
const char *p = t->line;
while (*p && isalpha((unsigned char)*p)) p++;
// There is at least one alphabetic character and there is a space right after.
if (t->line != p && *p == '=') {
// Found a "variable="!
t->var = t->line;
t->varlen = p - t->line;
// value is up until a space is found
t->val = p + 1;
while (*p && !isspace((unsigned char)*p)) p++;
t->vallen = p - t->val;
// Advance the pointer behind value.
t->line = *p ? p + 1 : p;
return 1;
}
// Ignore spaces
while (*p && !isalpha((unsigned char)*p)) p++;
// Advance over whole word.
t->line = p;
}
return 0;
}
int main() {
const char line[] = "hello, i know that rudolph=12 but it so small... a=b c=d fdnajn=123";
for (struct find_assignment_s fs = find_assignment_init(line);
find_assignment(&fs) == 1; ) {
printf("%.*s = %.*s\n", (int)fs.varlen, fs.var, (int)fs.vallen, fs.val);
}
}
outputs:
rudolph = 12
a = b
c = d
fdnajn = 123

Remove substring between parentheses in a string

I need to remove each substring between parentheses. I have found some solutions but none is good. Here is an example:
My string is: text(lorem(ipsum)abcd)pieceoftext and the actual output: lorem(ipsum
However, the expected output: text(())pieceoftext or textpieceoftext
Here is the code. I've run out of ideas. I thought of using strtok() but I have two different delimiters.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main()
{
const char *s = "text(lorem(ipsum)abcd)pieceoftext";
const char *patternA = "(";
const char *patternB = ")";
char *target = NULL;
char *start, *end;
if (start = strstr( s, patternA ))
{
start += strlen( patternA);
if (end = strstr( start, patternB ) )
{
target = (char *)malloc(end - start + 1);
memcpy(target, start, end - start);
target[end - start] = '\0';
}
}
if (target)
printf("Answer: %s\n", target);
return 0;
}
Looking forward to hearing some of your ideas to solve this problem. Thank you
To begin with, just allocate enough memory to target as you need to hold the entire source string s, because you really have no idea how much space you will need. Remember to add one for the end-of-string character.
Then change patternA and patternB from char * to just char, so you can compare them against individual chars in s.
Then you need to loop through the source string, keeping track of whether you are inside parentheses or not. Since you need to support nested parentheses, I would use a counter of how deep inside the parentheses you are:
int main()
{
const char *s = "text(lorem(ipsum)abcd)pieceoftext";
const char patternA = '(';
const char patternB = ')';
char *target;
int targetIndex = 0;
int parenDepth = 0;
target = malloc(strlen(s) + 1);
// check for malloc() error
for (int sourceIndex = 0; sourceIndex < strlen(s); sourceIndex++) {
if (s[sourceIndex] == patternA) {
// we are going deeper into parens, add to level, then ignore the char
parenDepth++;
continue;
}
if (s[sourceIndex] == patternB) {
// we are coming out of the parens, lower our level, ignore the parens char
parenDepth--;
continue;
}
if (parenDepth == 0) {
// if depth is 0, then we are not inside parens, so copy the char to target
target[targetIndex++] = s[sourceIndex];
}
}
// add end-of-string
target[targetIndex] = '\0';
printf("Answer: %s\n", target);
return 0;
}
I don't understand why you don't use strtok(strtok_r) only. I think it is more functional for this purpose. Just play with it somewhat.
#include <stdio.h>
#include <string.h>
int main(void) {
char str[] = "text(lorem(ipsum)abcd)pieceoftext";
char const * delim = ")(";
char *token;
char *rest = str;
while ((token = strtok_r(rest, delim, &rest))) {
printf("token: %s\n", token);
printf("rest: %s\n", rest);
}
}
You should investigate basic parsing techniques and use those to build a small sized program that does what you want.
hello(world)world
A simple solution:
If lookahead is an opening paren, stop saving. Until there is a closing paren. When there might be imbricated parens you just maintain a global variable of how deep we are (increment when there is an opening paren and decrement when there is a closing paren). When this variable is zero you can save.
You can use the same pattern beforehand to check if there are enough closing parens.

how to remove extension from file name?

I want to throw the last three character from file name and get the rest?
I have this code:
char* remove(char* mystr) {
char tmp[] = {0};
unsigned int x;
for (x = 0; x < (strlen(mystr) - 3); x++)
tmp[x] = mystr[x];
return tmp;
}
Try:
char *remove(char* myStr) {
char *retStr;
char *lastExt;
if (myStr == NULL) return NULL;
if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL;
strcpy (retStr, myStr);
lastExt = strrchr (retStr, '.');
if (lastExt != NULL)
*lastExt = '\0';
return retStr;
}
You'll have to free the returned string yourself. It simply finds the last . in the string and replaces it with a null terminator character. It will handle errors (passing NULL or running out of memory) by returning NULL.
It won't work with things like /this.path/is_bad since it will find the . in the non-file portion but you could handle this by also doing a strrchr of /, or whatever your path separator is, and ensuring it's position is NULL or before the . position.
A more general purpose solution to this problem could be:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// remove_ext: removes the "extension" from a file spec.
// myStr is the string to process.
// extSep is the extension separator.
// pathSep is the path separator (0 means to ignore).
// Returns an allocated string identical to the original but
// with the extension removed. It must be freed when you're
// finished with it.
// If you pass in NULL or the new string can't be allocated,
// it returns NULL.
char *remove_ext (char* myStr, char extSep, char pathSep) {
char *retStr, *lastExt, *lastPath;
// Error checks and allocate string.
if (myStr == NULL) return NULL;
if ((retStr = malloc (strlen (myStr) + 1)) == NULL) return NULL;
// Make a copy and find the relevant characters.
strcpy (retStr, myStr);
lastExt = strrchr (retStr, extSep);
lastPath = (pathSep == 0) ? NULL : strrchr (retStr, pathSep);
// If it has an extension separator.
if (lastExt != NULL) {
// and it's to the right of the path separator.
if (lastPath != NULL) {
if (lastPath < lastExt) {
// then remove it.
*lastExt = '\0';
}
} else {
// Has extension separator with no path separator.
*lastExt = '\0';
}
}
// Return the modified string.
return retStr;
}
int main (int c, char *v[]) {
char *s;
printf ("[%s]\n", (s = remove_ext ("hello", '.', '/'))); free (s);
printf ("[%s]\n", (s = remove_ext ("hello.", '.', '/'))); free (s);
printf ("[%s]\n", (s = remove_ext ("hello.txt", '.', '/'))); free (s);
printf ("[%s]\n", (s = remove_ext ("hello.txt.txt", '.', '/'))); free (s);
printf ("[%s]\n", (s = remove_ext ("/no.dot/in_path", '.', '/'))); free (s);
printf ("[%s]\n", (s = remove_ext ("/has.dot/in.path", '.', '/'))); free (s);
printf ("[%s]\n", (s = remove_ext ("/no.dot/in_path", '.', 0))); free (s);
return 0;
}
and this produces:
[hello]
[hello]
[hello]
[hello.txt]
[/no.dot/in_path]
[/has.dot/in]
[/no]
Use rindex to locate the "." character. If the string is writable, you can replace it with the string terminator char ('\0') and you're done.
char * rindex(const char *s, int c);
DESCRIPTION
The rindex() function locates the last character matching c (converted to a char) in the null-terminated string s.
If you literally just want to remove the last three characters, because you somehow know that your filename has an extension exactly three chars long (and you want to keep the dot):
char *remove_three(const char *filename) {
size_t len = strlen(filename);
char *newfilename = malloc(len-2);
if (!newfilename) /* handle error */;
memcpy(newfilename, filename, len-3);
newfilename[len - 3] = 0;
return newfilename;
}
Or let the caller provide the destination buffer (which they must ensure is long enough):
char *remove_three(char *dst, const char *filename) {
size_t len = strlen(filename);
memcpy(dst, filename, len-3);
dst[len - 3] = 0;
return dst;
}
If you want to generically remove a file extension, that's harder, and should normally use whatever filename-handling routines your platform provides (basename on POSIX, _wsplitpath_s on Windows) if there's any chance that you're dealing with a path rather than just the final part of the filename:
/* warning: may modify filename. To avoid this, take a copy first
dst may need to be longer than filename, for example currently
"file.txt" -> "./file.txt". For this reason it would be safer to
pass in a length with dst, and/or allow dst to be NULL in which
case return the length required */
void remove_extn(char *dst, char *filename) {
strcpy(dst, dirname(filename));
size_t len = strlen(dst);
dst[len] = '/';
dst += len+1;
strcpy(dst, basename(filename));
char *dot = strrchr(dst, '.');
/* retain the '.' To remove it do dot[0] = 0 */
if (dot) dot[1] = 0;
}
Come to think of it, you might want to pass dst+1 rather than dst to strrchr, since a filename starting with a dot maybe shouldn't be truncated to just ".". Depends what it's for.
I would try the following algorithm:
last_dot = -1
for each char in str:
if char = '.':
last_dot = index(char)
if last_dot != -1:
str[last_dot] = '\0'
Just replace the dot with "0". If you know that your extension is always 3 characters long you can just do:
char file[] = "test.png";
file[strlen(file) - 4] = 0;
puts(file);
This will output "test". Also, you shouldn't return a pointer to a local variable. The compiler will also warn you about this.
To get paxdiablo's second more general purpose solution to work in a C++ compiler I changed this line:
if ((retstr = malloc (strlen (mystr) + 1)) == NULL)
to:
if ((retstr = static_cast<char*>(malloc (strlen (mystr) + 1))) == NULL)
Hope this helps someone.
This should do the job:
char* remove(char* oldstr) {
int oldlen = 0;
while(oldstr[oldlen] != NULL){
++oldlen;
}
int newlen = oldlen - 1;
while(newlen > 0 && mystr[newlen] != '.'){
--newlen;
}
if (newlen == 0) {
newlen = oldlen;
}
char* newstr = new char[newlen];
for (int i = 0; i < newlen; ++i){
newstr[i] = oldstr[i];
}
return newstr;
}
Get location and just copy up to that location into a new char *.
i = 0;
n = 0;
while(argv[1][i] != '\0') { // get length of filename
i++; }
for(ii = 0; i > -1; i--) { // look for extension working backwards
if(argv[1][i] == '.') {
n = i; // char # of exension
break; } }
memcpy(new_filename, argv[1], n);
This is simple way to change extension name.
....
char outputname[255]
sscanf(inputname,"%[^.]",outputname); // foo.bar => foo
sprintf(outputname,"%s.txt",outputname) // foo.txt <= foo
....
With configurable minimum file length and configurable maximum extension length. Returns index where extension was changed to null character, or -1 if no extension was found.
int32_t strip_extension(char *in_str)
{
static const uint8_t name_min_len = 1;
static const uint8_t max_ext_len = 4;
/* Check chars starting at end of string to find last '.' */
for (ssize_t i = sizeof(in_str); i > (name_min_len + max_ext_len); i--)
{
if (in_str[i] == '.')
{
in_str[i] = '\0';
return i;
}
}
return -1;
}
I use this code:
void remove_extension(char* s) {
char* dot = 0;
while (*s) {
if (*s == '.') dot = s; // last dot
else if (*s == '/' || *s == '\\') dot = 0; // ignore dots before path separators
s++;
}
if (dot) *dot = '\0';
}
It handles the Windows path convention correctly (both / and \ can be path separators).

string parsing occurrence in c

I have a string as const char *str = "Hello, this is an example of my string";
How could I get everything after the first comma. So for this instance: this is an example of my string
Thanks
You can do something similar to what you've posted:
char *a, *b;
int i = 0;
while (a[i] && a[i] != ',')
i++;
if (a[i] == ',') {
printf("%s", a + i + 1);
} else {
printf("Comma separator not found");
}
Alternatively, you can take a look at strtok and strstr.
With strstr you can do:
char *a = "hello, this is an example of my string";
char *b = ",";
char *c;
c = strstr(a, b);
if (c != NULL)
printf("%s", c + 1);
else
printf("Comma separator not found");
Since you want a tail of the original string, there's no need to copy or modify anything, so:
#include <string.h>
...
const char *result = strchr(str, ',');
if (result) {
printf("Found: %s\n", result+1);
} else {
printf("Not found\n");
}
If you want ideas how to do it yourself (useful if you later want to do something similar but not identical), take a look at an implementation of strchr.
const char *result;
for(result = str; *result; result++)
if(*result == ',')
{
result++;
break;
}
//result points to the first character after the comma
After this code, result points to the string starting right after the comma. Or to the final '\0' (empty string), if there is no comma in the string.
You have the right idea, the following programs is one way to do it:
#include <stdio.h>
#include <string.h>
static char *comma (char *s) {
char *cpos = strchr (s, ',');
if (cpos == NULL)
return s;
return cpos + 1;
}
int main (int c, char *v[]) {
int i;
if (c >1 )
for (i = 1; i < c; i++)
printf ("[%s] -> [%s]\n", v[i], comma (v[i]));
return 0;
}
It produced the following output:
$ commas hello,there goodbye two,commas,here
[hello,there] -> [there]
[goodbye] -> [goodbye]
[two,commas,here] -> [commas,here]

Resources