如何从 C 中的字符串中保存最后一个字符?
How to save the last char from a string in C?
我正在尝试保存字符串中的最后一个字符?关于如何做到这一点的任何想法?
目标:我目前正在编写一个程序,其中一项任务是使用 Unix / POSIX 搜索文件扩展名。例如,如果用户输入:
./r_client localhost -name '*.c'
它应该 return 所有名称为 .c 文件扩展类型的文件。因此,我正在考虑将最后一个字符 strcat 抓取到以下代码
然后在我的目录中搜索如下:
下面是使用正则表达式的示例代码
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
int main(int argc, char *argv[]){
regex_t regex;
int reti;
char msgbuf[100];
char *name = "*.c";
// grab last char, strcat it to -> "[a-zA-Z0-9]\.c$"
// because it could be '.h', '.x', etc.
/* Compile regular expression */
reti = regcomp(®ex, "[a-zA-Z0-9]\.c$", 0);
if( reti ){ fprintf(stderr, "Could not compile regex\n"); exit(1); }
/* Execute regular expression */
reti = regexec(®ex, "e09s*fdf.ec", 0, NULL, 0);
if( !reti ){
puts("Match");
}
else if( reti == REG_NOMATCH ){
puts("No match");
}
else{
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(1);
}
/* Free compiled regular expression if you want to use the regex_t again */
regfree(®ex);
return 0;
}
// 然后调用("e09s*fdf.ec" - 这是一个测试) - 它应该如何失败 w/ .c
reti = regexec(®ex, "e09s*fdf.ec", 0, NULL, 0);
将替换为:dir_entry->d_name
除非有更好的方法吗?
提前感谢您的帮助
我想你想要这样的东西:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char dest[255];
char get_last_char(char* s)
{
return s[strlen(s) - 1];
}
int main(int argc, char *argv[])
{
char c = get_last_char(argv[1]);
char s[2];
s[0] = c;
s[1] = '[=10=]';
strcat(dest, "[a-zA-Z0-9]\.");
strcat(dest, s);
strcat(dest, "$");
printf("%s\n", dest);
exit(0);
}
示例:
$ ./concatenate1 "*.x"
[a-zA-Z0-9]\.x$
$ ./concatenate1 "*.c"
[a-zA-Z0-9]\.c$
$ ./concatenate1 "*.h"
[a-zA-Z0-9]\.h$
此外,如果您觉得长 运行.
更容易,请考虑使用 snprintf()
而不是多个 strcat()
编辑:
下面的代码应该可以完成这项工作。缺少一件事并标记为 TODO
- 验证。您永远无法确定用户会传递正确的数据。如果您的代码依赖于点后不超过 x
个字符这一事实,请检查 remaining_chars_count
并执行您认为好的操作。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *temp = strstr(argv[1], ".");
if (!temp)
{
printf("No dot found.\n");
exit(1);
}
unsigned int remaining_chars_count = strlen(argv[1]) - (temp - argv[1]) - 1;
/* TODO: add validation at this point! */
size_t output_string_length = remaining_chars_count;
char output_string[output_string_length];
unsigned output_string_count = remaining_chars_count;
size_t i = strlen(argv[1]);
for (i; i > strlen(argv[1]) - remaining_chars_count; i--)
{
output_string[output_string_count - 1] = argv[1][i - 1];
--output_string_count;
}
output_string[output_string_length] = '[=12=]';
printf("OUTPUT: %s\n", output_string);
exit(0);
}
示例:
$ ./dot1 "*1134..a"
OUTPUT: .a
$ ./dot1 abc.a
OUTPUT: a
$ ./dot1 "*a.hx"
OUTPUT: hx
我正在尝试保存字符串中的最后一个字符?关于如何做到这一点的任何想法?
目标:我目前正在编写一个程序,其中一项任务是使用 Unix / POSIX 搜索文件扩展名。例如,如果用户输入:
./r_client localhost -name '*.c'
它应该 return 所有名称为 .c 文件扩展类型的文件。因此,我正在考虑将最后一个字符 strcat 抓取到以下代码
然后在我的目录中搜索如下:
下面是使用正则表达式的示例代码
#include <sys/types.h>
#include <regex.h>
#include <stdio.h>
int main(int argc, char *argv[]){
regex_t regex;
int reti;
char msgbuf[100];
char *name = "*.c";
// grab last char, strcat it to -> "[a-zA-Z0-9]\.c$"
// because it could be '.h', '.x', etc.
/* Compile regular expression */
reti = regcomp(®ex, "[a-zA-Z0-9]\.c$", 0);
if( reti ){ fprintf(stderr, "Could not compile regex\n"); exit(1); }
/* Execute regular expression */
reti = regexec(®ex, "e09s*fdf.ec", 0, NULL, 0);
if( !reti ){
puts("Match");
}
else if( reti == REG_NOMATCH ){
puts("No match");
}
else{
regerror(reti, ®ex, msgbuf, sizeof(msgbuf));
fprintf(stderr, "Regex match failed: %s\n", msgbuf);
exit(1);
}
/* Free compiled regular expression if you want to use the regex_t again */
regfree(®ex);
return 0;
}
// 然后调用("e09s*fdf.ec" - 这是一个测试) - 它应该如何失败 w/ .c
reti = regexec(®ex, "e09s*fdf.ec", 0, NULL, 0);
将替换为:dir_entry->d_name
除非有更好的方法吗?
提前感谢您的帮助
我想你想要这样的东西:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char dest[255];
char get_last_char(char* s)
{
return s[strlen(s) - 1];
}
int main(int argc, char *argv[])
{
char c = get_last_char(argv[1]);
char s[2];
s[0] = c;
s[1] = '[=10=]';
strcat(dest, "[a-zA-Z0-9]\.");
strcat(dest, s);
strcat(dest, "$");
printf("%s\n", dest);
exit(0);
}
示例:
$ ./concatenate1 "*.x"
[a-zA-Z0-9]\.x$
$ ./concatenate1 "*.c"
[a-zA-Z0-9]\.c$
$ ./concatenate1 "*.h"
[a-zA-Z0-9]\.h$
此外,如果您觉得长 运行.
更容易,请考虑使用snprintf()
而不是多个 strcat()
编辑:
下面的代码应该可以完成这项工作。缺少一件事并标记为 TODO
- 验证。您永远无法确定用户会传递正确的数据。如果您的代码依赖于点后不超过 x
个字符这一事实,请检查 remaining_chars_count
并执行您认为好的操作。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
char *temp = strstr(argv[1], ".");
if (!temp)
{
printf("No dot found.\n");
exit(1);
}
unsigned int remaining_chars_count = strlen(argv[1]) - (temp - argv[1]) - 1;
/* TODO: add validation at this point! */
size_t output_string_length = remaining_chars_count;
char output_string[output_string_length];
unsigned output_string_count = remaining_chars_count;
size_t i = strlen(argv[1]);
for (i; i > strlen(argv[1]) - remaining_chars_count; i--)
{
output_string[output_string_count - 1] = argv[1][i - 1];
--output_string_count;
}
output_string[output_string_length] = '[=12=]';
printf("OUTPUT: %s\n", output_string);
exit(0);
}
示例:
$ ./dot1 "*1134..a"
OUTPUT: .a
$ ./dot1 abc.a
OUTPUT: a
$ ./dot1 "*a.hx"
OUTPUT: hx