I am trying to write a function that will allow me to use a function I wrote called strLength to count a number of characters passed, then will mallocate the number in addition to a NULL terminator, then copy the characters and return the copy.
so far I have:
int strLength(char* toCount)
{
int count = 0;
while(*toCount != '\0')
{
count++;
toCount++;
}
return count;
}
char* strCopy(char *s)
{
int length = strLength(s);
char *copy = malloc(length+1);
while(s != '\0')
{
s++;
}
return copy;
}
strCopy is the function I need help with. I also can not use strcpy or memcpy, I'm just writing this on my own to create my own string library. I think before the s++ I should have something along the lines of copy += s but I am not sure that would work.
I am a bit of a newbie, so please bear with me
Copy from the end to the beginning looks like a quick approach.
Check for a NULL allocation.
char* strCopy(char *s) {
int length = strLength(s) + 1;
char *copy = malloc(length);
if (copy != NULL) {
while (length > 0) {
length--;
copy[length] = s[length];
}
}
return copy;
}
Try this. Its working for me. I hope this helps.
char* strCopy(char *s)
{
char *copy = (char*) malloc(strLength(s) + 1);
int index = 0;
while(s[index] != '\0')
{
copy[index] = s[index];
index++;
}
return copy;
}
Related
I am in the stage of preparing myself for exams, and the thing that I m least proud of are my skills with strings. What I need to do is remove a word from a sentence, without using <string.h> library at all.
This is what I've got so far. It keeps showing me that certain variables are not declared, such as start and end.
#include <stdio.h>
/* Side function to count the number of letters of the word we wish to remove */
int count(char *s) {
int counter = 0;
while (*s++) {
counter++;
s--;
return counter;
}
/* Function to remove a word from a sentence */
char *remove_word(const char *s1, const char *s2) {
int counter2 = 0;
/* We must remember where the string started */
const char *toReturn = s1;
/* Trigger for removing the word */
int found = 1;
/* First we need to find the word we wish to remove [Don't want to
use string.h library for anything associated with the task */
while (*s1 != '\0') {
const char *p = s1;
const char *q = s2;
if (*p == *q)
const char *start = p;
while (*p++ == *q++) {
counter2++;
if (*q != '\0' && counter2 < count(s2))
found = 0;
else {
const char *end = q;
}
}
/* Rewriting the end of a sentence to the beginning of the found word */
if (found) {
while (*start++ = *end++)
;
}
s1++;
}
return toReturn;
}
void insert(char niz[], int size) {
char character = getchar();
if (character == '\n')
character = getchar();
int i = 0;
while (i < size - 1 && character != '\n') {
array[i] = character;
i++;
character = getchar();
}
array[i] = '\0';
}
int main() {
char stringFirst[100];
char stringSecond[20];
printf("Type your text here: [NOT MORE THAN 100 CHARACTERS]\n");
insert(stringFirst, 100);
printf("\nInsert the word you wish to remove from your text.");
insert(stringSecond, 20);
printf("\nAfter removing the word, the text looks like this now: %s", stringFirst);
return 0;
}
your code is badly formed, i strongly suggest compiling with:
gcc -ansi -Wall -pedantic -Werror -D_DEBUG -g (or similar)
start with declaring your variables at the beginning of the function block, they are known only inside the block they are declared in.
your count function is buggy, missing a closing '}' (it doesn't compile)
should be something like
size_t Strlen(const char *s)
{
size_t size = 0;
for (; *s != '\n'; ++s, ++size)
{}
return size;
}
implementing memmove is much more efficient then copy char by char
I reformatted you code for small indentation problems and indeed indentation problems indicate real issues:
There is a missing } in count. It should read:
/* Side function to count the number of letters of the word we wish to remove */
int count(char *s) {
int counter = 0;
while (*s++) {
counter++;
}
return counter;
}
or better:
/* Side function to count the number of letters of the word we wish to remove */
int count(const char *s) {
const char *s0 = s;
while (*s++) {
continue;
}
return s - s0;
}
This function counts the number of bytes in the string, an almost exact clone of strlen except for the return type int instead of size_t. Note also that you do not actually use nor need this function.
Your function insert does not handle EOF gracefully and refuses an empty line. Why not read a line with fgets() and strip the newline manually:
char *input(char buf[], size_t size) {
size_t i;
if (!fgets(buf, size, stdin))
return NULL;
for (i = 0; buf[i]; i++) {
if (buf[i] == '\n') {
buf[i] = '\0';
break;
}
}
return buf;
}
In function remove_word, you should define start and end with a larger scope, typically the outer while loop's body. Furthermore s1 should have type char *, not const char *, as the phrase will be modified in place.
You should only increment p and q if the test succeeds and you should check that p and q are not both at the end of their strings.
last but not least: you do not call remove_word in the main function.
The complete code can be simplified into this:
#include <stdio.h>
/* Function to remove a word from a sentence */
char *remove_word(char *s1, const char *s2) {
if (*s2 != '\0') {
char *dst, *src, *p;
const char *q;
dst = src = s1;
while (*src != '\0') {
for (p = src, q = s2; *q != '\0' && *p == *q; p++, q++)
continue;
if (*q == '\0') {
src = p; /* the word was found, skip it */
} else {
*dst++ = *src++; /* otherwise, copy this character */
}
}
*dst = '\0'; /* put the null terminator if the string was shortened */
}
return s1;
}
char *input(char buf[], size_t size) {
size_t i;
if (!fgets(buf, size, stdin))
return NULL;
for (i = 0; buf[i]; i++) {
if (buf[i] == '\n') {
buf[i] = '\0';
break;
}
}
return buf;
}
int main() {
char stringFirst[102];
char stringSecond[22];
printf("Type your text here, up to 100 characters:\n");
if (!input(stringFirst, sizeof stringFirst))
return 1;
printf("\nInsert the word you wish to remove from your text: ");
if (!input(stringSecond, sizeof stringSecond))
return 1;
printf("\nAfter removing the word, the text looks like this now: %s\n",
remove_word(stringFirst, stringSecond));
return 0;
}
Your start and end pointers are defined within a block which makes their scope limited within that block. So, they are not visible to other parts of your code, and if you attempt to reference them outside their scope, the compiler will complain and throw an error. You should declare them at the beginning of the function block.
That said, consider the following approach to delete a word from a string:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int delete_word(char *buf,
const char *word);
int main(void)
{
const char word_to_delete[] = "boy";
fputs("Enter string: ", stdout);
char buf[256];
fgets(buf, sizeof(buf), stdin);
if (delete_word(buf, word_to_delete))
{
printf("Word %s deleted from buf: ", word_to_delete);
puts(buf);
}
else
{
printf("Word %s not found in buf: ", word_to_delete);
puts(buf);
}
system("PAUSE");
return 0;
}
int chDelimit(int ch)
{
return
(ch == '\n' || ch == '\t') ||
(ch >= ' ' && ch <= '/') ||
(ch >= ':' && ch <= '#') ||
(ch >= '[' && ch <= '`') ||
(ch >= '{' && ch <= '~') ||
(ch == '\0');
}
char *find_pattern(char *buf,
const char *pattern)
{
size_t n = 0;
while (*buf)
{
while (buf[n] && pattern[n])
{
if (buf[n] != pattern[n])
{
break;
}
n++;
}
if (!pattern[n])
{
return buf;
}
else if (!*buf)
{
return NULL;
}
n = 0;
buf++;
}
return NULL;
}
char *find_word(char *buf,
const char *word)
{
char *ptr;
size_t wlen;
wlen = strlen(word);
ptr = find_pattern(buf, word);
if (!ptr)
{
return NULL;
}
else if (ptr == buf)
{
if (chDelimit(buf[wlen]))
{
return ptr;
}
}
else
{
if (chDelimit(ptr[-1]) &&
chDelimit(ptr[wlen]))
{
return ptr;
}
}
ptr += wlen;
ptr = find_pattern(ptr, word);
while (ptr)
{
if (chDelimit(ptr[-1]) &&
chDelimit(ptr[wlen]))
{
return ptr;
}
ptr += wlen;
ptr = find_pattern(ptr, word);
}
return NULL;
}
int delete_word(char *buf,
const char *word)
{
size_t n;
size_t wlen;
char *tmp;
char *ptr;
wlen = strlen(word);
ptr = find_word(buf, word);
if (!ptr)
{
return 0;
}
else
{
n = ptr - buf;
tmp = ptr + wlen;
}
ptr = find_word(tmp, word);
while (ptr)
{
while (tmp < ptr)
{
buf[n++] = *tmp++;
}
tmp = ptr + wlen;
ptr = find_word(tmp, word);
}
strcpy(buf + n, tmp);
return 1;
}
If you have to do it manually, just loop over the indicies of your string to find the first one that matches and than you’ll have a second loop that loops for all the others that matches and resets all and jumps to the next index of the first loop if not matched something in order to continue the searching. If I recall accuretaly, all strings in C are accesible just like arrays, you’ll have to figure it out how. Don’t afraid, those principles are easy! C is an easy langugae, thiught very long to write.
In order to remove: store the first part in an array, store the second part in an array, alloc a new space for both of them and concatinate them there.
Thanks, hit the upvote button.
Vitali
EDIT: use \0 to terminate your newly created string.
I need help with a couple of things:
I'm trying to delete a word from a pointer and put it in a new pointer with a new length but i am not able to copy it to the new pointer
I'm not sure when should I use the free() function.
when I use the free(str) in the delete function it crashes.
After I copy the "str" to the "newStr" what is the best way to copy the "newStr" back to the "str" with the new length?
Please help me understand it, I'm new with this and I googled it, I tried looking here and didn't find something that could help me.
void delete(char *str)
{
int i, indexStart = 0, indexEnd = 0, wordlen = 0, newLen = 0, len = 0;
printf("Enter the index of the word that you want to remove: ");
scanf("%d", &i);
indexs(i, str,&indexStart,&indexEnd,&wordlen);
len = strlen(str);
newLen = len - wordlen - 1;
char *newStr = (char*)malloc(newLen * sizeof(char));
if (newStr == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
for (int j = 0; j < len; j++)
{
if (j< (indexStart - 1) || j > indexEnd)
{
*newStr = *str;
newStr++;
}
str++;
}
free(str);
//free(newStr);
printf("The new string: %s\n", newStr);
}
void main()
{
char *str = (char*)malloc(1 * sizeof(char));
if (str == NULL)
{
printf("Error! memory not allocated.");
exit(0);
}
text(str);
if (str != NULL)
{
delete(str);
}
free(str);
system("pause");
}
According to the structured programming paradigm your functions should solve small separate tasks. But your function delete() prints to the console, scans input, allocates new string and fills this new string. But what's event worse is the call exit() in this function. If something went wrong, function must to return an error, but not to stop the program. Also names of functions should relfect what they do.
Use free() for every memory block allocated by malloc().
So, this is a working code:
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
const char *findWord(const char *str, const char *delimiters, unsigned index) {
// validate input parameters
if (!str || !delimiters) {
return NULL;
}
// count words
unsigned count = 0;
while (*str && count < index) {
// skip delimiters
while (*str && !strchr(delimiters, *str)) {
str++;
}
if (*str) {
count++;
}
// skip word
while (*str && strchr(delimiters, *str)) {
str++;
}
}
// if index is too big returns NULL
return *str ? str : NULL;
}
unsigned countLengthOfWord(const char *str, const char *delimiters) {
// validate input parameters
if (!str || !delimiters) {
return 0;
}
// count length
unsigned length = 0;
while (*str && !strchr(delimiters, *str++)) {
length++;
}
return length;
}
char *cutWord(char *str, const char *delimiters, unsigned index) {
// validate input parameters
if (!str) {
return NULL;
}
str = (char *)findWord(str, delimiters, index);
// if index is too big, return NULL
if (!str) {
return NULL;
}
// allocate new string for word
unsigned length = countLengthOfWord(str, delimiters);
char *word = malloc(length + 1);
// check if allocation was successfull
if (!word) {
return NULL;
}
// copy word
strncpy(word, str, length);
word[length] = '\0';
// cut word from string
const char *ptr = str + length;
while (*ptr) {
*str++ = *ptr++;
}
*str = '\0';
return word;
}
int main() {
char str1[] = "Hello, my world!";
char str2[] = "Hello, my world!";
char str3[] = "Hello, my world!";
char *word1 = cutWord(str1, " ,!", 0);
char *word2 = cutWord(str2, " ,!", 1);
char *word3 = cutWord(str3, " ,!", 2);
if (word1) {
printf("word: %s\nstring: %s\n\n", word1, str1);
}
if (word2) {
printf("word: %s\nstring: %s\n\n", word2, str2);
}
if (word3) {
printf("word: %s\nstring: %s\n\n", word3, str3);
}
// release allocated memory
free(word1);
free(word2);
free(word3);
getchar();
return 0;
}
Is it possible to use strtok or some other string function to cut the string until the point where last delimiter is found.
Specific example would be date; I would like to transform "4.1.2017." to "4.1.2017" - without the dot at the end.
If you have a single delimiter, use strrchr to find its last occurrence in the string:
char str[] = "quick.brown.fox";
char *ptr = strrchr(str, '.');
if (ptr) {
*ptr = '\0';
}
printf("%s\n"' str);
This produces the following output:
quick.brown
Like I explained in My comment (if you don't want to use strrchr or you cannot for some reasons) I'll create a Function which checks the position of that delimiter like this:
int my_strrchr(const char *ptr, const char delimiter){
if (ptr == NULL ){
printf("Error, NULL Pointer\n");
return -1;
}
if ( *ptr == '\0' ){
printf("Error, the Buffer is Empty\n");
return 0;
}
int i = 0;
int ret = 0;
while( ptr[i] != '\0' ){
if ( ptr[i] == delimiter ){
ret = i;
}
i++;
}
return ret;
}
And use it Like this:
#include <stdio.h>
int main(void){
char arr[] = "4.1.2017.";
char delimiter = '.';
int len;
if( (len = my_strrchr(arr, delimiter)) > 0){
while ( arr[len] != '\0'){
arr[len] = '\0';
}
printf("%s\n", arr);
}
}
See DEMO.
Any way this is only to get you an Idea and as you can see I use no standard Functions here.
I am trying to create a function that removes a specified character from a string.
I am only halfway done with the code because I got stuck when I am trying to replace the character to delete with nothing. I just realized I can't put "nothing" in an element of an array so my plan just got ruined.
I figure that I have to loop through the whole string, and when I find the character I want to remove I have to remove it by moving all of the elements that are in front of the "bad" character one step back. Is that correct?
#include <stdio.h>
#include <string.h>
void del(char string[], char charToDel)
{
int index = 0;
while(string[index] != '\0')
{
if(string[index] == charToDel){
string[index] = string[index+1];
}
index++;
}
printf("%s", string);
}
int main(void)
{
char string[] = "Hello world";
del(string, 'l');
return 0;
}
I want to make this program without pointers. Just plain simple code.
I added another while loop that moves every character in the loop to the left but it doesn't seem to work since the output is just plain blank.
int index = 0;
while(string[index] != '\0')
{
if(string[index] == charToDel)
{
while(string[index] != '\0')
{
string[index] = string[index+1];
}
}
index++;
}
printf("%s", string);
}
Johathan Leffler's Method?
char newString[100];
int index = 0;
int i = 0;
while(string[index] != '\0')
{
if(string[index] != charToDel)
{
newString[i] = string[index];
index++;
i++;
}
i++;
index++;
}
printf("%s", newString);
}
This gives me a lot of weird characters...
char const *in = string;
char *out = string;
while (*in) {
if (*in != charToDel)
*out++ = *in;
++in;
}
*out = '\0';
or without pointers
size_t in = 0;
size_t out = 0;
while (string[in]) {
if (string[in] != charToDel)
string[out++] = string[in];
++in;
}
string[out] = '\0';
The problem is that, when you are assigning string[index+1] to string[index], the next l from the string took place of previous one and index incremented to its next value by 1and this l is not deleted by your function. You should have to fixed that.
As suggested by Jonathan Leffler and Gabson; you can do it by coping the string to itself as;
void del(char string[], char charToDel)
{
int index = 0, i = 0;
while(string[index] != '\0')
{
if(string[index] != charToDel){
string[i++] = string[index];
}
index++;
}
string[i] = '\0';
printf("%s", string);
}
I'm trying to tokenize a sting and here is my attempt.
char new_str[1024];
void tokenize_init(const char str[]){//copy the string into global section
strcpy(new_str,str);
}
int i = 0;
char *tokenize_next() {
const int len = strlen(new_str);
for(; i <= len; i++) {
if ( i == len) {
return NULL;
}
if ((new_str[i] >= 'a' && new_str[i] <= 'z') ||
(new_str[i] >= 'A' && new_str[i] <= 'Z')) {
continue;
}else {
new_str[i] = '\0';
i = i + 1;
return new_str;
}
}
return NULL;
}
//main function
int main(void) {
char sentence[] = "This is a good-sentence for_testing 1 neat function.";
printf("%s\n", sentence);
tokenize_init(sentence);
for (char *nt = tokenize_next();
nt != NULL;
nt = tokenize_next())
printf("%s\n",nt);
}
However, it just print out the first word of the sentence(which is "This") and then stop. Can someone tell me why? My guess is my new_str is not persisent and when the main function recall tokenize_next() the new_str become just the first word of the sentence. Thanks in advance.
The reason that it only prints out "This" is because you iterate to the first non-letter character which happens to be a space, and you replace this with a null terminating character at this line:
new_str[i] = '\0';
After that, it doesn't matter what you do to the rest of the string, it will only print up to that point. The next time tokenize_next is called the length of the string is no longer what you think it is because it is only counting the word "This" and since "i" has already reached that amount the function returns and so does every successive call to it:
if ( i == len)
{
return NULL;
}
To fix the function you would need to somehow update your pointer to look past that character on the next iteration.
However, this is quite kludgy. You are much better off using one of the mentioned functions such as strtok or strsep
UPDATE:
If you cannot use those functions then a redesign of your function would be ideal, however, per your request, try the following modifications:
#include <string.h>
#include <cstdio>
char new_str[1024];
char* str_accessor;
void tokenize_init(const char str[]){//copy the string into global section
strcpy(new_str,str);
str_accessor = new_str;
}
int i = 0;
char* tokenize_next(void) {
const int len = strlen(str_accessor);
for(i = 0; i <= len; i++) {
if ( i == len) {
return NULL;
}
if ((str_accessor[i] >= 'a' && str_accessor[i] <= 'z') ||
(str_accessor[i] >= 'A' && str_accessor[i] <= 'Z')) {
continue;
}
else {
str_accessor[i] = '\0';
char* output = str_accessor;
str_accessor = str_accessor + i + 1;
if (strlen(output) <= 0)
{
str_accessor++;
continue;
}
return output;
}
}
return NULL;
}
//main function
int main(void) {
char sentence[] = "This is a good-sentence for_testing 1 neater function.";
printf("%s\n", sentence);
tokenize_init(sentence);
for (char *nt = tokenize_next(); nt != NULL; nt = tokenize_next())
printf("%s\n",nt);
}