Using a replace function in C programming [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
How do I write a replace function which takes a pointer to a string as a parameter and replaces all spaces in that string with minus signs?
example :
input = "I love pies", output = "I-love-pies"

This is what you need:
#include <stdio.h>
int change_string(char *input) {
for (char *p = input; *p; p++) {
if (*p == ' ') *p = '-';
}
return 1;
}
int main() {
char input[] = "I love pies";
printf("%s\n", input);
change_string(input);
printf("%s\n", input);
return 0;
}

Related

string splitting logic in C programing [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 9 months ago.
Improve this question
Could you please provide me some code or solution how to split the below string in C programming
Sample string :
SMABCDEFGHIJK,887276617459,5,552612260849779,552612260840646,552612260843632,552612260843525,552612260846817
Output needed :
552612260849779,552612260840646,552612260843632,552612260843525,552612260846817
Basically for the input string we would need to Ignore first 3 positions and want rest of the string in different variable.
The positions to ignore and delimiter values will get the from the database table label.
So, If someone help me to give the logic that would be very helpful
In your case, you do not need to split the string. Simply ignore everything before the n-th occurrence of the delimiter.
char *ignoreFisrstN(const char *str, int delim, size_t ignoreCount)
{
char *result = NULL;
while(ignoreCount--)
{
if((str = strchr(str, delim))) str++;
else break;
}
if(str)
{
result = malloc(strlen(str) + 1);
if(result) strcpy(result, str);
}
return result;
}
int main(void)
{
char *s = "SMABCDEFGHIJK,887276617459,5,552612260849779,552612260840646,552612260843632,552612260843525,552612260846817";
for(size_t i = 0; i < 10; i++)
{
char *r = ignoreFisrstN(s, ',', i);
printf("%zu: `%s`\n", i, r ? r : "NULL");
free(r);
}
}
https://godbolt.org/z/xWe74sY46

Split .txt file into words and store each word in array [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
Let's say my file has about 1000 words with commas, dots and semicolons.
I have to split the text into words (probably using "strtok", but I don't know how to do this correctly) and then write these words into array. How to do such a thing? Can somebody write a piece of working code and explain how it works?
I hope this program can help you. It might not be perfect but it's close to what you ask about.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main()
{
char str[5000];
char *ptr;
char *words[5000];
FILE * fp = fopen("hi.txt", "r");
fgets(str, 49, fp); // read 49 characters
ptr = strtok(str, ",.; "); // split our findings around the " "
int i = 0;
while(ptr != NULL) // while there's more to the string
{
words[i]= ptr;
i++;
ptr = strtok(NULL, ",.; "); // and keep splitting
}
fclose(fp);
for(int j=0;j<i;j++) {
printf("%s\n", words[j]);
}
}
file hi.txt
foo, bar. baz; bletch.
Test
./a.out
foo
bar
baz
bletch

Segmentation fault on fgets [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
When I run the following code, it prints '>', waits for the input, and then segfaults. Does anybody know why?
int main(int argc, char **argv){
char input[MAX_INPUT_LINE];
while(1==1){
if (isatty(0)){
printf(">");
}
fgets(input, MAX_INPUT_LINE, stdin);
int len1=sizeof(input);
for (int i=0; i<len1; i++){
printf("%s", input[i]);
}
}
int len1=sizeof(input);
for (int i=0; i<len1; i++){
printf("%s", input[i]); // <-- %s would expect char * not char
}
Use %c to print a character not %s. %s would expect a nul terminated char * and you pass char, this causes undefined behaviour.
sizeof would return size of the array not length of string . So use strlen to get length of string.

null compare in C language [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
i already have this code in C language, is a string of 15 data delimited by commas the code split the string by comma and store every data in a single variable called array[], the issue is that i have the last data comma if after the last comma there are no data then the variable x15 = 0, but if there are a value after the last , then convert that value to a int. i print the value of the array[15] to verify and this is null, so i out a condition for that but do not work, the program just break after compiling.
char buf[] ="¶bL0 L3,01,+08590323,-079343001,010215,00000000000000,-tN,000,012689997,001219456,000,7FF2,C07F,0,4,";
printf("\n \n string=[%s]\n\n", buf);
int i = 0;
int u;
char *p = strtok (buf, ",");
char *array[16];
char *y15;
while (p != NULL)
{
array[i++] = p;
p = strtok (NULL, ",");
}
for (i = 0; i <16; ++i){
if(array[15] == NULL){
wbt.x15=0;
}else{
wbt.x15=atoi(array[15]);
}
//printf("data: [%s]\n", array[i]);
}
You are using an uninitialized array element, which is cause for undefined behavior.
You have:
char *array[16];
The elements of the array are uninitialized. And then, you proceed to use:
if(array[15] == NULL){
wbt.x15=0;
}else{
wbt.x15=atoi(array[15]);
}
It's not clear why you have that check for every iteration of the loop but that's another problem. The problem with the posted code is that array[15] is not initialized. Using that value is a problem.
Make sure you initialize array properly. Use:
char *array[16] = {0};
Also, I think your for loop needs to be something like:
for (i = 0; i <16; ++i)
{
int x = 0;
if(array[i] != NULL)
{
x = atoi(array[i]);
}
// Now use x.
}

What is the name of the buffer that can be overflowed buf, msg, len or out [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I am new to this and I can't seem to understand can you please help me identify the buffer that can be overflowed and why?
#include <stdio.h>
#include <string.h>
#define S 100
#define N 1000
int main(int argc, char *argv[]) {
char out[S];
char buf[N];
char msg[] = "Welcome to the argument echoing program\n";
int len = 0;
buf[0] = '\0';
printf(msg);
while (argc) {
sprintf(out, "argument %d is %s\n", argc-1, argv[argc-1]);
argc--;``
strncat(buf,out,sizeof(buf)-len-1);
len = strlen(buf);
}
printf("%s",buf);
return 0;
}
The problem is here:
sprintf(out, "argument %d is %s\n", argc - 1, argv[argc - 1]);
When you face a problem like this one, it's always a good idea to go through your code commenting lines until the program stops crashing. Then you will know where the bug is :D

Resources