I'm getting this weird error when trying to extract a 2 or 3 digits string of numbers, from a filename in C, some times it executes right, and sometimes it does not, the input filename is "EC2_122016_1676_corte22_03012017084106.TXT", and I only want the part that says "22" out of "corte22".
The correct result in this case should be "Code: 022", but, sometimes it shows another value, like "Code: 223" or "Code: 224", I don't have much experience with C and so, I can't get around to why this is happening, any help?
Here's the code I'm using to separate the filename from the code:
int main(int argc, char *argv[]) {
char *sourceFileName = "EC2_122016_1676_corte22_03012017084106.TXT";
char fileCode[4] = "\0";
strcpy(fileCode, extractFileCode(sourceFileName));
printf("Code: %s\n", fileCode);
}
char* extractFileCode(const char sourceFileName[]) {
char *tmp = strdup(sourceFileName);
strlwr(tmp);
char *result = strstr(tmp, "corte");
result = strtok(result, "_");
result = extractNumbersFromString(result);
char *t;
// convert the number back to string, but add a leading zero
sprintf(result, "%.3d", strtol(result, &t, 10));
free(tmp);
return result;
}
char* extractNumbersFromString(const char *source) {
char *result = malloc(strlen(source) * sizeof(char));
int index = 0;
int i;
for(i = 0; i < strlen(source); i++) {
if(isdigit(source[i])) {
result[index++] = source[i];
}
}
return result;
}
in extractNumbersFromString you're not nul-terminating result (and the buffer is too short), so another digit can slip at the end of the buffer.
Fix:
char* extractNumbersFromString(const char *source) {
char *result = malloc(strlen(source) + 1);
int index = 0;
int i;
for(i = 0; i < strlen(source); i++) {
if(isdigit(source[i])) {
result[index++] = source[i];
}
}
result[index] = '\0';
return result;
}
besides * sizeof(char) is useless since always 1.
Related
I'm new to pointers and I can already see how confusing they can be.
I have tried to look this up in several threads and google but they don't quite return what I'm looking for maybe out of my inexperience.
I'm being passed an array of strings and I have to pass it again to another function however I'm extremely confused on how to do this and don't know what * or & to use or where.
My code:
#include <stdlib.h>
#include <stdio.h>
char *ft_strcat(char *dest, char *src)
{
unsigned int c;
unsigned int count;
count = 0;
while (dest[count] != 0)
{
count++;
}
c = 0;
while (src[c] != '\0')
{
dest[c + count] = src[c];
c++;
}
dest[c + count] = 0;
return (dest);
}
int size_str(char *str)
{
int c;
c = 0;
while (str[c] != '\0')
{
c++;
}
return (c - 1);
}
int size_all(int size, char *strs[], char *sep)
{
int i;
int counter;
i = 0;
counter = 0;
counter += size_str(sep) * (size - 1);
while (i < size)
{
counter += size_str(strs[i]);
i++;
}
return (counter);
}
char *ft_strjoin(int size, char **strs, char *sep)
{
int i;
char *str;
str = malloc(sizeof(char) * size_all(size, strs, sep));
str = strs[0];
i = 1;
while (i < size)
{
str = ft_strcat(str, strs[i]);
}
return (str);
}
int main(void)
{
char *sep = " ";
char a1[] = "Batata";
char a2[] = "frita";
char a3[] = "\'e";
char a4[] = "melhor";
char a5[] = "que";
char a6[] = "Banana";
char *strs[] = {a1, a2, a3, a4, a5, a6};
char *final = ft_strjoin(6, strs, sep);
printf("%s", final);
}
I thought that size all would have to have an extra dereference operator on the declaration of the function and an reference operator when I call it, but this works just fine. Am I doing something wrong or am I just misunderstanding how pointers work? Don't I have to add an extra * each time I pass it?
Finally why doesn't while (src[c] != '\0') work?
In size_str:
There's nothing wrong with while (src[c] != '\0'), but return (c - 1); is causing an off-by-one error with your string lengths. The NUL byte wasn't counted in the loop, there's no need to subtract 1.
In ft_strcat:
The first loop is repeating work that could be handled by a call to size_str.
In ft_strjoin:
str = malloc(sizeof(char) * sizeall(size, strs, sep)));
sizeof (char) is uneccessary, as it is always 1. You need an additional 1 byte added to the length passed to malloc to make room for the NUL byte in your final string.
Remember that pointers are values too. str = strs[0]; assigns the pointer held in strs[0] to the the variable str. It does not copy the contents of strs[0]. You are overwriting the value returned by malloc with a pointer to a different piece of memory.
Instead, given this set of functions, initialize the memory returned by malloc to be the empty string, by setting the first byte to NUL, and use ft_strcat to concatenate the first string.
There's no need to continually reassign the result of ft_strcat, as you are already altering str, and the return value will never change.
A complete example. One must not forget to free the resulting string when it is no longer needed.
#include <stdlib.h>
#include <stdio.h>
int size_str(char *str)
{
int i = 0;
while (str[i])
i++;
return i;
}
char *ft_strcat(char *dest, char *src)
{
int i = 0,
length = size_str(dest);
do
dest[length++] = src[i];
while (src[i++]);
return dest;
}
int size_all(int size, char **strs, char *sep)
{
int total_length = size_str(sep) * (size - 1);
for (int i = 0; i < size; i++)
total_length += size_str(strs[i]);
return total_length;
}
char *ft_strjoin(int size, char **strs, char *sep)
{
char *result = malloc(1 + size_all(size, strs, sep));
result[0] = '\0';
ft_strcat(result, strs[0]);
for (int i = 1; i < size; i++) {
ft_strcat(result, sep);
ft_strcat(result, strs[i]);
}
return result;
}
int main(void)
{
char *sep = " ";
char a1[] = "Batata";
char a2[] = "frita";
char a3[] = "\'e";
char a4[] = "melhor";
char a5[] = "que";
char a6[] = "Banana";
char *strs[] = {a1, a2, a3, a4, a5, a6};
char *final = ft_strjoin(6, strs, sep);
printf("%s\n", final);
free(final);
}
Output:
Batata frita 'e melhor que Banana
I have worked lately about this problematic, string joint. I noticed that you forgot to add an if condition where the size would be 0. Moreover, the while loop need an iteration, which means that it will give you an infinite loop.
You can find as follows some adjustment to your code:
int i;
char *str;
int j;
int k;
i = 0;
k = 0;
str = (char *)malloc(sizeof(char) * sizeall(size, strs, sep) + 1));
if (size == 0)
return (0);
while (i < size)
{
j = 0;
while (strs[i][j])
str[k++] = strs[i][j++];
j = 0;
if (i < size - 1)
while (sep[j])
str[k++] = sep[j++];
i++;
}
str[k] = '\0';
return (str);
Feel free to ask me if there is something you did not understand, and good luck.
I want to chain 2 strings in C. The function I use called concat()
First I defined something like this and it worked
char* concat(const char *s1, const char *s2)
{
char* result = malloc (15);
int lengh1 = simple_strlen (s1);
int lengh2 = simple_strlen (s2);
int i=0,j;
for ( i = 0 ;i < lengh1;i++){
if (i!=lengh1-1)
result[i]=s1[i];
else{
result[i]=s1[i];
for ( j=i+1 ; j< lengh1+lengh2;j++){
result[j] = s2[j-i-1];
}
}
}
return result;
}
But then I was asked to do it without malloc() so I defined something like this:
char* concat( char *result, const char *s2)
{
int lengh1 = simple_strlen (result);
int lengh2 = simple_strlen (s2);
int i=0;
for ( i = 0 ;i < lengh2;i++){
result[i+lengh1]=s2[i];
}
return result;
}
but it has segmentation fault
example:
int main(int argc , char* argv[], char* envp[])
{
printf(concat( "hello", "world"));/*output expected "helloworld"*/
return 0;
}
There are multiple problems in your code:
in the malloc version, the space allocated for the destination string is hard coded at 15 instead of computing it as lengh1 + lengh2 + 1, enough space for both strings and the trailing null byte.
you do not set the null terminator at the end of the destination string in both versions.
in your version without malloc, you must provide a large enough array as the destination for concat(). A string constant cannot be modified. A simple solution is to pass the destination buffer and the source strings as separate arguments.
Here are modified versions:
char *concat(const char *s1, const char *s2) {
int length1 = simple_strlen(s1);
int length2 = simple_strlen(s2);
char *result = malloc(length1 + length2 + 1);
int i, j;
for (i = 0; i < length1; i++) {
result[i] = s1[i];
}
for (i = 0; i < length2; i++) {
result[length1 + i] = s2[1];
}
result[length1 + length2] = '\0';
return result;
}
Without malloc():
#include <string.h>
char *concat(char *dest, const char *s1, const char *s2) {
char *p = dest;
while (*s1)
*p++ = *s1++;
while (*s2)
*p++ = *s2++;
*p = '\0';
return dest;
}
int main() {
char buf[100];
/* output expected "helloworld" */
printf("%s\n", concat(buf, "hello", "world"));
return 0;
}
As mentioned by the prior commenters, you need to allocate memory somewhere to store the concatenated string. If you are not allowed to allocate on the heap via malloc, then you can do something like this:
#include "stdafx.h"
# include <string.h>
char* concat( char * result , const char *s1, const char *s2)
{
int lengh1 = strlen(s1);
int lengh2 = strlen(s2);
int i = 0;
for (i = 0; i < lengh1; i++) {
result[i] = s1[i];
}
for (i = 0; i < lengh2; i++) {
result[i+ lengh1] = s2[i];
}
return result;
}
int main()
{
char mybuffer[100];
memset(mybuffer, 0, sizeof(mybuffer));
printf(concat( mybuffer,"hello", "world"));/*output expected "helloworld"*/
return 0;
}
I have an array of strings and am trying to reverse each string in the array to see if that string is a palindrome. I am using a for loop to increment an int i (the index). However after the I call the reverse function, the value of i becomes some really large number and I cant figure out why this is happening.
#include <stdio.h>
#include <string.h>
void revString(char *dest, const char *source);
int main() {
const char *strs[] = {
"racecar",
"radar",
"hello",
"world"
};
int i;
char res[] = "";
for (i = 0; i < strlen(*strs); i++) {
printf("i is %d\n", i);
revString(&res[0], strs[i]); //reversing string
printf("i is now %d\n", i);
//comparing string and reversed string
if (strcmp(res, strs[i]) == 0) {
printf("Is a palindrome");
} else {
printf("Not a palindrome");
}
}
return 0;
}
void revString(char *dest, const char *source) {
printf("%s\n", source);
int len = strlen(source);
printf("%d\n", len);
const char *p;
char s;
for (p = (source + (len - 1)); p >= source; p--) {
s = *p;
*(dest) = s;
dest += 1;
}
*dest = '\0';
}
This is the output showing the value of i before and after the revString function is called.
i is 0
i is now 1667588961
Illegal instruction: 4
There are multiple problems in your code:
You pass a destination array char res[] = ""; that is much too small for the strings you want to reverse. It's size is 1. This causes buffer overflow, resulting in undefined behavior.
Use char res[20]; instead.
You enumerate the array of string with an incorrect upper bound. Use this instead:
for (i = 0; i < sizeof(strs) / sizeof(*strs); i++)
The termination test for the loop in revString() is incorrect too: decrementing p when is equal to source has undefined behavior, although it is unlikely to have an consequences. You can simplify this function this way:
void revString(char *dest, const char *source) {
size_t len = strlen(source);
for (size_t i = 0; i < len; i++) {
dest[i] = source[len - i - 1];
}
dest[len] = '\0';
}
Here is the resulting code:
#include <stdio.h>
#include <string.h>
void revString(char *dest, const char *source) {
size_t len = strlen(source);
for (size_t i = 0; i < len; i++) {
dest[i] = source[len - i - 1];
}
dest[len] = '\0';
}
int main(void) {
const char *strs[] = { "racecar", "radar", "hello", "world" };
char res[20];
for (size_t i = 0; i < sizeof(strs) / sizeof(*strs); i++) {
revString(res, strs[i]);
//comparing string and reversed string
if (strcmp(res, strs[i]) == 0) {
printf("Is a palindrome\n");
} else {
printf("Not a palindrome\n");
}
}
return 0;
}
Here is Final Code with some change
#include <stdio.h>
#include <string.h>
void revString(char* dest, const char* source);
int main(){
const char* strs[] = {
"racecar",
"radar",
"hello",
"world"
};
static int i;
char res[] = "";
int length = (int) sizeof(strs)/sizeof(char*);
for(i = 0; i < length; i++)
{
printf("i is %d\n", i);
revString(&res[0], strs[i]); //reversing string
printf("i is now %d\n", i);
//comparing string and reversed string
if(strcmp(res, strs[i]) == 0){
printf("Is a palindrome");
}else{
printf("Not a palindrome");
}
}
return 0;
}
void revString(char* dest, const char* source){
printf("%s\n", source);
int len = (int) strlen(source);
printf("%d\n", len);
const char* p;
char s;
for(p = (source + (len - 1)); p >= source; p--){
s = *p;
*(dest) = s;
dest += 1;
}
*dest = '\0';
}
Change 1 :-
int i; to static int i; (Reason:- i is local variable you are calling
function so when function call the value of i will remove and after
that it will assign garbage value.)
change 2 :-
strlen(*strs) to length of array (because strlen(*strs) will give the
length of first string)
My program takes in the Constitution, reverses the line order, and prints the Constitution in reverse order back in the command line. When I put the algorithm to reverse the lines in a separate function, lineReversal, the only thing that prints is ▒▒▒.
int main(){
char * buffer;
char * strings[1000];
int arrayCount =0;
int size = 10;
while(!feof(stdin))
{
buffer= (char*) malloc(size);
getAline(&buffer, &size); //gets each line from the Constitution
strings[arrayCount++] = buffer;
}
lineReversal(&strings);
return 0;
}
char lineReversal(char ** stringPtr)
{
char * strings = *stringPtr;
for(int i = 873; i >=0 ; i--) {
if(strings[i] == '\0'){
break;
}
printf("%s", strings + i);
}
*stringPtr = strings;
return 0;
}
If I put that algorithm into main() and run my program, this is my output:
H▒▒▒▒▒▒▒▒▒▒▒▒Dhave intervened.Representatives, shall take effect, until an election of Representatives shall
The expected output is:
have intervened.
This is what my program looks like with the algorithm inside main.
int main(int argc, char ** argv){
char * buffer;
char * strings[1000];
int arrayCount =0;
int size = 10;
while(!feof(stdin))
{
buffer= (char*) malloc(size);
getAline(&buffer, &size);
strings[arrayCount++] = buffer;
}
for(int i = 873; i >=0 ; i--)
{
if(strings[i] == '\0'){
break;
}
printf("%s", strings[i]);
}
return 0;
}
I need to replace a strings in some text. I found this function here at stackoverflow:
char *replace(const char *s, const char *old, const char *new)
{
char *ret;
int i, count = 0;
size_t newlen = strlen(new);
size_t oldlen = strlen(old);
for (i = 0; s[i] != '\0'; i++) {
if (strstr(&s[i], old) == &s[i]) {
count++;
i += oldlen - 1;
}
}
ret = malloc(i + count * (newlen - oldlen));
if (ret == NULL)
exit(EXIT_FAILURE);
i = 0;
while (*s) {
if (strstr(s, old) == s) {
strcpy(&ret[i], new);
i += newlen;
s += oldlen;
} else
ret[i++] = *s++;
}
ret[i] = '\0';
return ret;
}
This function works for me fine for single replacement. But i need to replace a whole array "str2rep" to "replacement". So what i'm trying to do(im just a beginner)
****
#define MAXTEXT 39016
int l;
int j;
char *newsms = NULL;
char text[MAXTEXT];
char *str2rep[] = {":q:",":n:"};
char *replacement[] = {"?","\n"};
strcpy((char *)text,(char *)argv[5]);
l = sizeof(str2rep) / sizeof(*str2rep);
for(j = 0; j < l; j++)
{
newsms = replace(text,(char *)str2rep[j],(char *)replacement[j]);
strcpy(text,newsms);
free(newsms);
}
textlen = strlen(text);
This code even works locally, If I build it from single file... But this is asterisk module, so when this is being executed, asterisk stops with:
* glibc detected * /usr/sbin/asterisk: double free or corruption (!prev): 0x00007fa720006310 *
Issues:
ret = malloc(i + count * (newlen - oldlen)); is too small. Need + 1.
Consider what happens with replace("", "", ""). If your SO ref is this, it is wrong too.
Questionable results mixing signed/unsigned. count is signed. newlen, oldlen are unsigned.
I think the original code works OK, but I do not like using the wrap-around nature of unsigned math when it can be avoided which is what happens when newlen < oldlen.
// i + count * (newlen - oldlen)
size_t newsize = i + 1; // + 1 for above reason
if (newlen > oldlen) newsize += count * (newlen - oldlen);
if (newlen < oldlen) newsize -= count * (oldlen - newlen);
ret = malloc(newsize);
Insure enough space. #hyde Various approaches available here.
// strcpy(text, newsms);
if (strlen(newsms) >= sizeof text) Handle_Error();
strcpy(text, newsms);
Minor
No need for casts
// newsms = replace(text, (char *) str2rep[j], (char *) replacement[j]);
newsms = replace(text, str2rep[j], replacement[j]);
Better to use size_t for i. A pedantic solution would also use size_t count.
// int i;
size_t i;
I will suggest something that to me looks a bit more clear as an alternative, in place of a proper dynamic string implementation. Exception handling is left as an exercise for the reader to add. :)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *appendn(char *to, char *from, int length)
{
return strncat(realloc(to, strlen(to) + length + 1), from, length);
}
char *replace(char *string, char *find, char *sub)
{
char *result = calloc(1, 1);
while (1)
{
char *found = strstr(string, find);
if (!found)
break;
result = appendn(result, string, found - string);
result = appendn(result, sub, strlen(sub));
string = found + strlen(find);
}
return appendn(result, string, strlen(string));
}
int main()
{
const char text[] = "some [1] with [2] to [3] with other [2]";
char *find[] = {"[1]", "[2]", "[3]", NULL};
char *sub[] = {"text", "words", "replace"};
char *result, *s;
int i;
result = malloc(sizeof(text));
(void) strcpy(result, text);
for (i = 0; find[i]; i ++)
{
s = replace(result, find[i], sub[i]);
free(result);
result = s;
}
(void) printf("%s\n", result);
free(result);
}