C function to remove instances of one char from a string - string

I did it right but it only works if the base string is one word:
#include <stdio.h>
void withoutString(char *new, char *base, char removee){
while(*base){
if(*base!=removee){
*new++=*base;}
base++;
}
*new='\0';
}
int main()
{
char b[16],n[16],r;
printf("Please enter string: ");
scanf(" %s", b);
printf("Which character do you want to remove? \n");
scanf(" %c",&r);
withoutString(n, b, r);
printf(" The new string is %s", n);
return 0;
}
How do I make it work for more words?

Read and understand the documentation of scanf. Good sources for documentation on the C standard functions are the C standard itself and POSIX.
You will then notice that scanf is not the best function to read a whole line, so look for fgets next.

Related

Program keeps giving me the "core dump", though i can't find why

So, i wrote this program which receives as input a string, which consists of surname and name, written like this: "james:lebron". The program should copy surname in the surname variable of the struct, same goes for the name. No compilation errors, just the core dump error. Somebody can please explain me why?
#include <stdio.h>
#include <string.h>
struct author{
char *name;
char *surname;
};
typedef struct author author_t;
author_t separate(char *string);
int main(void){
char *name_surname;
printf("Inserisci nome e cognome: ");
gets(name_surname);
separate(name_surname);
}
author_t separate(char *string){
int i, n=0;
author_t temp;
for(i=0; i<(strlen(string)-1); i++){
while(string[i]!=':')
n++;
strncpy(temp.surname, string, n);
strncpy(temp.name, &string[n+2], (strlen(string)-n-2));
}
return(temp);
}
off teh top of my head Id say incrementing n is not a good idea as n i steh number of items to copy not the position in the string.
struct author{
char *name;
char *surname;
};
....
int i, n=0;
author_t temp;
for(i=0; i<(strlen(string)-1); i++){
while(string[i]!=':')
n++;
strncpy(temp.surname, string, n);
strncpy(temp.name, &string[n+2], (strlen(string)-n-2));
}
You cannot copy into tmp.surname or temp.name, as you did not reserve any memory yet. (Note that strncpy does not reserve memory, it just copies.) char *name only reserves space for a pointer, not the actual string. User char name[30] or so, or assign memory later like this:
author_t auth;
auth.name = (char *) malloc(30);
(same for surname, of course)

printf function won't print reversed string

I have to write a function which gets a string and then reverses it. I wrote this code, which gave me no compilation errors:
#include <stdio.h>
#include <string.h>
char *reverse(char *str);
int main(void){
char string[100];
printf("Insert the string to be inverted: ");
gets(string);
reverse(string);
printf("Inverted string is: %s\n", string);
}
char *reverse(char *str){
char h;
int i, j;
for(i=0, j=strlen(str); i<j; i++, j--){
h=str[i];
str[i]=str[j];
str[j]=h;
}
}
Only problem I get is that, apparently, printf won't print the reversed string. Can anyone help me?
The reversed string has the terminator \0 at position 0.
strlen(str) gives you the size of the string, i.e. the index of the terminating null character.
You don't want to swap the null terminator, so strlen(str) - 1 is the index of the last character you want to swap.
Therefore, j should start at strlen(str) - 1.

splitting a line and printing it takes results in a core dumped

When I try to read a line from standard input and split it into words, after removing the /n character, I get a core dumped error. Could anyone explain me the reason? What is the correct way to do this?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LINE_LEN 50
#define MAX_PARTS 50
int main ()
{
char* token;
char *str;
char* arr[MAX_PARTS];
int i,j;
printf("Write a line: \n $:");
fgets(str, LINE_LEN, stdin);
str = strncpy(str, str, strlen(str)-1);
fflush(stdin);
i=0;
token = strtok(str, " ");
while( token != NULL )
{
arr[i] = token;
printf("%s",arr[i]);
i++;
token = strtok(NULL," ");
}
return 0;
}
You are printing the NULL pointer on your last pass through the while() loop. You probably need to reverse the printf() and strtok() calls like this:
while( token != NULL )
{
arr[i] = token;
printf("%s",arr[i]); # Must come first to avoid printing a NULL on final loop
i++;
token = strtok(NULL," ");
}
You are reading into unallocated memory.
char *str;
This declares a pointer str, which is pointing nowhere. (In fact, it points to a random location, but "nowhere" stops the guys who try to second-guess undefined behaviour.)
fgets(str, LINE_LEN, stdin);
This writes to the location str is pointing at, which is nowhere (see above). This is undefined behaviour. If your program happens to survive this (instead of SEGFAULTing right there), you cannot rely on it behaving in any sane manner from this point on.
While we're at it:
fflush(stdin);
Note that the C standard does not define the behaviour of fflush() when called on input streams, i.e. while this is well-defined under Linux (which does define this behaviour), this is a non-standard, non-portable construct that could well crash on other platforms.

Get certain parts out of a string using C

Evening everyone hope on of you gurus can help. I am trying to find the answer to this issue I need to read the data out of the string below by searching the tags. i.e IZTAG UKPART etc however the code I am using is no good as it only stores the 1st part of it for example UKPART = 12999 and misses out the -0112. Is there a better way to search strings ?
UPDATE SO FAR.
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main ()
{
// in my application this comes from the handle and readfile
char buffer[255]="TEST999.UKPART=12999-0112...ISUE-125" ;
//
int i;
int codes[256];
char *pos = buffer;
size_t current = 0;
//
char buffer2[255];
if ((pos=strstr(pos, "UKPART")) != NULL) {
strcpy (buffer2, pos); // buffer2 <= "UKPART=12999-0112...ISUE-125"
}
printf("%s\n", buffer2);
system("pause");
return 0;
}
NOW WORKS BUT RETURN WHOLE STRING AS OUTPUT I NEED TO JUST RETURN UKPART FOR EXAMPLE THANKS SO FAR :-)
strstr() is absolutely the right way to search for the substring. Cool :)
It sounds like you want something different from "sscanf()" to copy the substring.
Q: Why not just use "strcpy ()" instead?
EXAMPLE:
char buffer[255]="IZTAG-12345...UKPART=12999-0112...ISUE-125" ;
char buffer2[255];
if ((pos=strstr(pos, "UKPART")) != NULL) {
strcpy (buffer2, pos); // buffer2 <= "UKPART=12999-0112...ISUE-125"

Program to see the bytes from a file internally

Do you know if exist one program or method to see (secuences of)bytes from a text,html file?
Not to see characters, rather see the complete sequence of bytes.
recommendations?
yes, it is called hex editor... Hundreds of those exist out there.
Here are some: http://en.wikipedia.org/wiki/Comparison_of_hex_editors
A common hex editor allows you to view any file's byte sequence.
If you just want to see the existing bytes (without changing them) you can use a hex-dump program, which is much smaller and simpler than a hex editor. For example, here's one I wrote several years ago:
/* public domain by Jerry Coffin
*/
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char **argv) {
unsigned long offset = 0;
FILE *input;
int bytes, i, j;
unsigned char buffer[16];
char outbuffer[60];
if ( argc < 2 ) {
fprintf(stderr, "\nUsage: dump filename [filename...]");
return EXIT_FAILURE;
}
for (j=1;j<argc; ++j) {
if ( NULL ==(input=fopen(argv[j], "rb")))
continue;
printf("\n%s:\n", argv[j]);
while (0 < (bytes=fread(buffer, 1, 16, input))) {
sprintf(outbuffer, "%8.8lx: ", offset+=16);
for (i=0;i<bytes;i++) {
sprintf(outbuffer+10+3*i, "%2.2X ",buffer[i]);
if (!isprint(buffer[i]))
buffer[i] = '.';
}
printf("%-60s %*.*s\n", outbuffer, bytes, bytes, buffer);
}
fclose(input);
}
return 0;
}

Resources