Reputation: 3
I have written some code that makes a character that the user chooses to be erased from the string and now i want to add a character in the end that the user chooses. Its the first time i do something like this so i am really lost on what needs to be changed/added. I think i need to use strcpy to copy the string that the user writes and then make the new string to somehow be added to the first string, this is the part im lost on becuase i want to use the same code and just make some changes so the difference can be seen between adding a character and removing a character.
Upvotes: 0
Views: 92
Reputation: 84561
It is apparent that you are having a bit of difficulty grasping the use of user input functions and the overall logic of your program. What do you think the following does?
printf("Enter a sentence: \n");
fgets(str, 100, stdin);
if (fgets(str, 100, stdin) == NULL) {
...
How many sentences is the user expected to enter? Calling fgets
twice does not appear to be what you intend -- once is enough (remove the one you fail to check the return on) While duplicating the fgets
call was not what you needed, using fgets
for user-input was the correct choice.
(also note: do not return negative values to your shell, e.g. return -1;
, instead simply return 1
to indicate an error condition (or the macro EXIT_FAILURE
provided in stdlib.h
, which is curiously defined as 1
)
While we are talking about defined constants, avoid the use of magic-numbers in your code (e.g. in char str[100];
, 100
is a magic-number). Instead:
#define MAXC 100 /* if you need a constant, #define one (or more) */
...
char str[MAXC] = "";
Since you declare str
as an array of 100
characters, you know that you do not need to prompt for entry of a character to add if you already have a string of 99 chars
plus the nul-terminating character. So you need to save the length of the string. Your use of strcspn
is a good way to trim the trialing '\n'
from str
, but you are already scanning forward in str
to find the '\n'
and nul-terminate the string there -- and -- strcspn
returns the number of characters of the initial segment of str
that does not include '\n'
(i.e. it will return the length, so simply save it)
str[(len = strcspn(str, "\n"))] = 0; /* save len */
The remainder of the code is simply to check the len < MAXC - 1
to ensure that there is sufficient room to add the character, and then prompt and call getchar
, add the new character at str[len]
and then nul-terminate str
again at len+1
. You could do something similar to:
if (len < MAXC - 1) {
printf ("\nEnter character to add: ");
if ((r = getchar()) != EOF) {
str[len++] = r;
str[len] = 0;
printf ("This is the sentence: '%s' (len: %zu)\n", str, len);
}
else
fputs ("(user canceled input.)\n", stderr);
}
else {
fputs ("\nerror: insufficient space to add char.\n", stderr);
return 1;
}
(note: by incrementing len
when the new character is added, you preserve the length of the new string in len
and it is saved for later use if needed.)
Putting it altogether, you would have something like the following:
#include <stdio.h>
#include <string.h>
#define MAXC 100 /* if you need a constant, #define one (or more) */
int main (void) {
char str[MAXC] = "";
size_t len;
int r;
printf("Enter a sentence: "); /* no newline needed */
if (fgets(str, MAXC, stdin) == NULL) {
fputs ("fgets failed\n", stderr); /* no need for fprintf */
return 1; /* do not return negative values to your shell */
}
str[(len = strcspn(str, "\n"))] = 0; /* save len */
printf ("This is the sentence: '%s' (len: %zu)\n", str, len);
if (len < MAXC - 1) {
printf ("\nEnter character to add: ");
if ((r = getchar()) != EOF) {
str[len++] = r;
str[len] = 0;
printf ("This is the sentence: '%s' (len: %zu)\n", str, len);
}
else
fputs ("(user canceled input.)\n", stderr);
}
else {
fputs ("\nerror: insufficient space to add char.\n", stderr);
return 1;
}
}
(just a note, if you are not providing a conversion within your output format string, e.g. %s
or %d
, etc.., then there is no need to call printf
or fprintf
. You can simply call puts
or fputs
to output a literal string. A good compiler will make that optimization for you.)
Example Use/Output
$ ./bin/straddchr
Enter a sentence: 123456
This is the sentence: '123456' (len: 6)
Enter character to add: 7
This is the sentence: '1234567' (len: 7)
(and if you originally enter a 99
character string, you will not be prompted to enter an additional character and an error message will display of "insufficient space to add char."
)
Look things over and let me know if you have further questions.
Upvotes: 1
Reputation: 67475
Appends char to the C string.
dynamic - 1 if realloc needed. 0 if str has been statically or dynamically allocated and has enough space to accommodate the new char.
char *appendchar(char *str, int ch, int dynamic)
{
char *tmp;
if(dynamic)
{
tmp = realloc(str, strlen(str) + 2);
str = tmp;
}
if(str)
{
size_t len = strlen(str);
str[len] = ch;
str[len + 1] = 0;
}
return str;
}
Upvotes: 1