Replace words in sentence - c

I am trying to make a function in C where the function has to replace given word with another given word in sentence. This is my code but it does not work:
#include <stdio.h>
#include <string.h>
void repl(char* sentence, char* thiss, char* change_to_this) {
int i = 0;
int j = 0;
int length_thiss = strlen(thiss);
int length_change_to_this = strlen(change_to_this);
while (sentence[i] != 0) {
if (sentence[i] == this[j]) {
int init = i;
while (sentence[init] == thiss[j] && thiss[j] != 0)
{
j++;
init++;
}
if (thiss[j] == 0) {
j -= length_thiss;
for (int m = 0; m < length_thiss; m++) {
sentence[i] = change_to_this[m];
}
j = 0;
}
i++;
}
}
}
This should be result:
char sent[] = "My name is John and I am thirty years old.";
char name1[] = "John";
char name2[] = "Rick";
char age1[] = "thirty";
char age2[] = "fifty";
replace(sent, name1, name2);
replace(sent, age1, age2);
printf("result: %s\n", a); // My name is Rick and I am fifty years old.
Length of thiss is at least the same as the length of change_to_this. I cannot include any other libraries except those 2 I already included. Any ideas how to correct the code, please?

if you use string functions it can be archived much easier
char *repl(char *haystack, const char *needle, const char *repl)
{
size_t hLen = strlen(haystack), nLen = strlen(needle), rLen = strlen(repl);
char *pos, *nhs = haystack;
while((pos = strstr(nhs, needle)))
{
memmove(pos + rLen, pos + nLen, hLen - (pos - haystack) - nLen + 1);
memcpy(pos, repl, rLen);
nhs = pos + rLen;
}
return haystack;
}
int main()
{
char sent[] = "My name is Johnatan and I am thirty years old.";
char name1[] = "Johnatan";
char name2[] = "Rich";
char age1[] = "thirty";
char age2[] = "fifty five";
repl(sent, name1, name2);
printf("result: %s\n", sent); // My name is Rick and I am fifty years old.
repl(sent, age1, age2);
printf("result: %s\n", sent); // My name is Rick and I am fifty years old.
}
https://godbolt.org/z/2fK3XY
In my another answers you will find all the functions used written by hand
EDIT
As in the deleted comment - amended to replace all occurrences.
the caller is responsible for the size of the haystack.

Related

How to replace a character in a string with another string in C

Let's suppose i have this phrase:
Hello $, Welcome!
I have to replace the '$' with a name, the result should be:
Hello Name, Welcome!
For now i did this, but it copies only the name and the first part of the phrase:
char * InsertName(char * string, char * name)
{
char temp;
for(int i = 0; i < strlen(string); i++)
{
if(string[i] == '$')
{
for(int k = i, j = 0; j < strlen(name); j++, k++)
{
temp = string[k+2];
string[k] = name[j];
string[k+1] = temp;
}
return string;
}
}
return "";
}
How can i shift all the elements after the name, so i can have the full string to be returned?
You can use sprintf() to print the output on a C-string, emulating the work done by printf():
Edit: You will have to include these two headers for this function to work:
#include <stdlib.h>
#include <memory.h>
An implementation of what you are trying to implement:
char* InsertAt(unsigned start, const char* source, const char* target, const char* with,
unsigned * position_ret)
{
const char * pointer = strstr(source, target);
if (pointer == NULL)
{
if (position_ret != NULL)
*position_ret = UINT_MAX;
return _strdup(source);
}
if (position_ret != NULL)
*position_ret = (unsigned)(pointer - source);
char* result = calloc(strlen(source) + strlen(with) + strlen(pointer), sizeof(char));
sprintf_s(result, strlen(source) + strlen(with) + strlen(pointer), "%.*s%.*s%.*s",
(signed)(pointer - source), _strdup(source),
(signed)strlen(with) + 1, _strdup(with),
(signed)(strlen(pointer) - strlen(target)), _strdup(pointer + strlen(target)));
return result;
}
Example:
#define InsertAtCharacter(src, ch, with) InsertAt(0u, (src), \
(char[]){ (char)(ch), '\0' }, (with), NULL)
int main(void)
{
printf("%s", InsertAtCharacter("Hello $, Welcome!", '$', "Name"));
return 0;
}
Try this !!!
#include <stdio.h>
#include <string.h>
char* replace(char* str, char* a, char* b)
{
int len = strlen(str);
int lena = strlen(a), lenb = strlen(b);
for (char* p = str; p = strstr(p, a); ++p) {
if (lena != lenb) // shift end as needed
memmove(p+lenb, p+lena,
len - (p - str) + lenb);
memcpy(p, b, lenb);
}
return str;
}
int main()
{
char str[80] = "Hello $,Welcome!";
printf("%s\n", replace(str, "$", "name"));
return 0;
}

Efficiently replace a substring in a string

I have made two functions that find a substring index and substitute that substring in the string. I'm glad I jury rigged this at all, given that similar questions previously asked were never answered/marked as closed without any help. Is there a cleaner method?
void destroy_substr(int index, int len)
{
int i;
for (i = index; i < len; i++)
{
string[i] = '~';
}
}
void find_substr_index(char* substr)
{
int i;
int j;
int k;
int count;
int len = strlen(substr);
for (i = 0; i < strlen(string); i++)
{
if (string[i] == substr[0])
{
for(j = i, k = 0; k < len; j++, k++)
{
if (string[j] == substr[k])
{
count++;
}
if (count == len)
destroy_substr((j - len + 1), len);
}
j = 0;
k = 0;
count = 0;
}
}
}
Your code seems like you're trying to re-inventing your own wheel.
By using standard C functions, which is strstr() and memset(), you can achieve the same result as you expected.
#include <stdio.h>
#include <string.h>
char string[] = "foobar foobar foobar";
char substr[] = "foo";
char replace = '~';
int main() {
int substr_size = strlen(substr);
// Make a copy of your `string` pointer.
// This is to ensure we can safely modify this pointer value, without 'touching' the original one.
char *ptr = string;
// while true (infinite loop)
while(1) {
// Find pointer to next substring
ptr = strstr(ptr, substr);
// If no substring found, then break from the loop
if(ptr == NULL) { break; }
// If found, then replace it with your character
memset(ptr, replace, substr_size);
// iIncrement our string pointer, pass replaced substring
ptr += substr_size;
}
printf("%s\n", string);
return 0;
}
How about this:
#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
char string[] = "HELLO hello WORLD world HELLO hello ell";
char substring[] = "ell";
int stringLength = strlen(string);
int substringLength = strlen(substring);
printf("Before: %s\n", string);
if(substringLength <= stringLength)
{
int i;
int j;
for(i = 0, j = stringLength - substringLength + 1; i < j; )
{
if(memcmp(&string[i], substring, substringLength) == 0)
{
memset(&string[i], '~', substringLength);
i += substringLength;
}
else
{
i++;
}
}
}
printf("After: %s\n", string);
return 0;
}
Key ideas are:
You only need to scan the string (stringLength - substringLength) times
You can use functions from string.h to do the comparison and to replace the substring
You can copy the new string in place. If you want to support insertion of longer strings you will need to manage memory with malloc()/realloc(). If you want to support insertion of smaller strings you'll need to advance the pointer to the beginning by the length of the replacement string, copy the rest of the string to that new location, then zero the new end of the string.
#include <stdio.h>
#include <string.h>
#include <err.h>
int main(int argc, char **argv)
{
char *str = strdup("The fox jumps the dog\n");
char *search = "fox";
char *replace = "cat";
size_t replace_len = strlen(replace);
char *begin = strstr(str, search);
if (begin == NULL)
errx(1, "substring not found");
if (strlen(begin) < replace_len)
errx(1, "replacement too long");
printf("%s", str);
memcpy(begin, replace, replace_len);
printf("%s", str);
return 0;
}

Number of times one string occurs within another in C

I have written a function that uses the strstr() function to determine if string2 has any matches in string1. This works fine (I also convert both strings to lower case so that I can do a case-insensitive match.). However, strstr() only finds the first time the match occurs. Is there a way to use it to find each time a match occurs? For example:
string1[] = "ABCDEFABC";
string2[] = "ABC";
Would return 0 as a match is found in position 0, and 6 as it's found again in position 6?
Here's the original function as written (including call to function in main):
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char *strstrnc(const char *str1, const char *str2);
int main()
{
char buf1[80],buf2[80];
printf("Enter the first string to be compared: ");
gets(buf1);
printf("Enter the second string to be compared: ");
gets(buf2);
strstrnc(buf1,buf2);
return 0;
}
char *strstrnc(const char *buf1, const char *buf2)
{
char *p1, *ptr1, *p2, *ptr2, *loc;
int ctr;
ptr1 = malloc(80 * sizeof(char));
ptr2 = malloc(80 * sizeof(char));
p1 = ptr1;
p2 = ptr2;
for (ctr = 0; ctr < strlen(buf1);ctr++)
{
*p1++ = tolower(buf1[ctr]);
}
*p1 = '\0';
for (ctr = 0; ctr < strlen(buf2);ctr++)
{
*p2++ = tolower(buf2[ctr]);
}
*p2 = '\0';
printf("The new first string is %s.\n", ptr1);
printf("The new first string is %s.\n", ptr2);
loc = strstr(ptr1,ptr2);
if (loc == NULL)
{
printf("No match was found!\n");
}
else
{
printf("%s was found at position %ld.\n", ptr2, loc-ptr1);
}
return loc;
}
Given
char string1[] = "ABCDEFABC";
char string2[] = "ABC";
strstr(string1, string2) will return a pointer to the first element of string1.
Would return 0 as a match is found in position 0, and 6 as it's found again in position 6?
In order to check for multiple occurrences of string2 in string1, you'll have to call strstr multiple times with an offset from string1. E.g.
char* found = strstr(string1, string2);
while ( found != NULL )
{
// Search for string2 after applying an offset.
int offset = 1;
found = strstr(found+offset, string2);
}
I think this is what you need:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main(void){
char string1[] = "ABCDABCEFABC";
char string2[] = "ABC";
char *s1, *s2, *s3;
size_t lenstring1 = strlen(string1);
size_t lenstring2 = strlen(string2);
if (lenstring2 < 1){
printf("There is no substring found"); /* or what ever */
exit(1);
}
size_t i=0,j=0;
int found=0;
s1 = string1;
s2 = string2;
for(i = 0; i < lenstring1; i++){
if(*s1 == *s2){
s3 = s1;
for(j = 0;j < lenstring2;j++){
if(*s3 == *s2){
s3++;s2++;
}else{
break;
}
}
s2 = string2;
if(j == strlen(string2)){
found = 1;
printf("%s found at index : %zu\n",string2,i+1);
}
}
s1++;
}
if(found == 0){
printf("No match Found");
}
return 0;
}
Output:
ABC found at index : 1
ABC found at index : 5
ABC found at index : 10
If the string2 is empty you will get:
There is no substring found

How to retrieve multiple substrings of a string and write them into one string in C?

How can I split a const char * string in the fastest possible way.
char *inputStr="abcde";
char buff[500];
I would like to have in buffer the following formatted string, format of which must be:
IN('a','ab','abc','abcd','abcde')
I'm learning C and new to the language. I have no clue where to start on this splitting problem.
I don't think you can do this particularly "fast", it seems like it's quite heavily limited since it needs to iterate over the source string many times.
I'd do something like:
void permute(char *out, const char *in)
{
const size_t in_len = strlen(in);
char *put;
strcpy(out, "IN(");
put = out + 3;
for(i = 1; i < in_len; ++i)
{
if(i > 1)
*put++ = ',';
*put++ = '\'';
memcpy(put, in, i);
put += i;
*put++ = '\'';
}
*put++ = ')';
*put++ = '\0';
}
Note that this doesn't protect against buffer overrun in the output.
You could use strcpy, strcat/strncat and a simple loop:
#include <stdio.h>
#include <string.h>
int main(void) {
char* inputStr = "abcde";
char buff[500];
// start the formatted string:
strcpy(buff,"IN(");
int i, len = strlen(inputStr);
for (i = 0; i < len; ++i) {
strcat(buff, "'");
strncat(buff, inputStr, i + 1);
strcat(buff, "'");
// if it is not last token:
if (i != len - 1)
strcat(buff, ",");
}
// end the formatted string:
strcat(buff,")");
printf("%s", buff);
return 0;
}
outputs the desired IN('a','ab','abc','abcd','abcde')
To give you a start, consider the following code:
char buffer[64];
const char str[] = "abcde";
for (size_t i = 1; i <= strlen(str); ++i)
{
strncpy(buffer, str, i);
buffer[i] = '\0'; /* Make sure string is terminated */
printf("i = %lu, buffer = \"%s\"\n", i, buffer);
}
The above code should print
i = 1, buffer = "a"
i = 2, buffer = "ab"
i = 3, buffer = "abc"
i = 4, buffer = "abcd"
i = 5, buffer = "abcde"
If you are looking for something like this in C++:-
#include <iostream>
#include <string.h>
using namespace std;
int main() {
const char *inputStr = "abcde"; //const to remove warning of deprecated conversion
char buff[500];
int count = 0;
for (int i = 0; i < (int) strlen(inputStr); i++) { //cast it to int to remove
// warning of comparison between signed and unsigned
for (int j = 0; j <= i; j++) {
buff[count++] = inputStr[j];
}
buff[count++] = ',';
}
buff[--count] = '\0';
cout << buff;
return 0;
}
Output - a,ab,abc,abcd,abcde

Replace char in string with another string in C

I'm trying to replace ' ' (space) with '___' (triple underscore) in C.
Here is my code:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *a = "12 34 56";
int a_l = strlen(a);
printf("str1: \"%s\" (%d)\n", a, a_l);
char *b = "___";
int b_l = strlen(b);
printf("str2: \"%s\" (%d)\n", b, b_l);
for (int i = 0; i < a_l; i++) {
if (a[i] == ' ') {
char *o = malloc(a_l + b_l);
strncpy(o, a, i);
strncpy(o + i, b, a_l);
//strncpy help
printf("out: \"%s\"\n", o);
}
}
return 0;
}
I think that it is right so far, but I need to replace the comment line with correct strncpy (take the rest of string a (excluding space) and append it to string o). So the output should be like this:
str1: "12 34 56" (8)
str2: "___" (3)
out: "12___34 56"
out: "12 34___56"
If there are other mistakes in my code, please tell me.
UPD: This shouldn't replace all spaces in a loop. If the source string contains 8 spaces, there should be 8 lines printed and in each line only one space should be replaced.
You are overcomplicating this so much that I just TL;DR.
Some remarks that you might surely want to read, learn, embrace well and use:
I. int is not for string lengths and stuff. size_t is for string lengths and stuff.
II. String literals cannot be modified, so using the legacy char * type for assigning them to a variable is no good by any means, const-qualify that poor pointer base type.
III. Use VLAs instead of malloc() if dynamic memory management is not really needed (we're not living in 1989 anymore).
IV. NUL-terminate your strings because C stdlib routines expect you to do so.
int main()
{
const char *in = "foo bar baz";
int nspc = 0;
for (const char *p = strchr(in, ' '); p; p = strchr(p + 1, ' '))
nspc++;
char buf[strlen(in) + nspc * 2 + 1];
memset(buf, 0, sizeof(buf));
const char *s = in;
for (const char *p = strchr(s, ' '); p; p = strchr(s, ' ')) {
strncat(buf, s, p - s);
strcat(buf, "___");
s = p + 1;
}
const char *end = in + strlen(in);
strncat(buf, s, end - s);
printf("%s\n", buf);
return 0;
}
You can try this one. The problem comes from the fact that a_l + b_l in your malloc is always the same value. It doesn't affect by number of spaces.
int count = 0, index = 0;
for (int i = 0; i < a_l; ++i) {
if (a[i] == ' ') {
count++;
}
}
const char *o = malloc(a_l + 2 * count + 1); // 2 is because you add 3 new symbols, but remove 1 so 3 - 1 = 2
memset(o, 0, sizeof(o));
for (int i = 0; i < a_l; ++i) {
if (a[i] != ' ')
o[index++] = a[i];
else {
o[index++] = '_';
o[index++] = '_';
o[index++] = '_';
}
}
Without using library function!!!
while(*string)
{
if(*string=='\\')
{
*string++;
while(repl_len--)
*dest++ = *repl_string++;
}
else
{
*dest++ = *string++;
}
repl_string = temp_repl_string;
repl_len = temp_repl_len;
}
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
size_t my_strlen(const char *str, size_t *spc){
size_t len;
*spc = len = 0;
while(*str){
++len;
if(*str++ == ' ')++*spc;
}
return len;
}
int main(void){
char *a = "12 34 56";
size_t spc;
int a_l = my_strlen(a, &spc);
printf("str1: \"%s\" (%d)\n", a, a_l);
char *b = "___";
int b_l = strlen(b);
printf("str2: \"%s\" (%d)\n", b, b_l);
char *p, *o = malloc(a_l - spc + spc * b_l + 1);
p=o;
for (int i = 0; i < a_l; i++) {
if(a[i] == ' ') {
strncpy(p, b, b_l);
p += b_l;
} else {
*p++ = a[i];
}
}
*p = '\0';
printf("out: \"%s\"\n", o);
free(o);
return 0;
}
I see that many answers have been added, but this may have been done very simply with a single loop. Since the string is very short, you can sacrifice memory over CPU and allocate an array 3 times +1 bigger than the original string, and be sure that it won't be overflowed:
const char* string= "12 34 56";
size_t length= strlen(string);
char result[length*3 +1];
unsigned int index= 0;
memset(result, '\0', length*3+1);
for(int i=0; i<length; i++)
{
if(string[i]!= ' ')
{
result[index++]= string[i];
}
else
{
strcat(result,"___");
index+= 3;
}
}
I found another thread and after reading the answers, I figured out that the right line should look like this:
strncpy(o + i + b_l, a + i + 1, a_l - i);
And after few fixes that were suggested in this thread my code looks like this:
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
char *a = "12 34 56";
size_t a_l = strlen(a);
printf("str1: \"%s\" (%d)\n", a, a_l);
char *b = "___";
size_t b_l = strlen(b);
printf("str2: \"%s\" (%d)\n", b, b_l);
for (int i = 0; i < a_l; i++) {
if (a[i] == ' ') {
char *o = malloc(a_l + b_l);
strncpy(o, a, i);
strcpy(o + i, b);
strncpy(o + i + b_l, a + i + 1, a_l - i);
printf("out: \"%s\"\n", o);
free(o);
}
}
return 0;
}
And this prints the desired putput.

Resources