function to separate a string in c - c

#include <stdio.h>
#include <string.h>
void separate(char s[20], char dummy[10], char* p){
strcpy(dummy, s);
for (int i = 0; i < strlen(dummy); i++){
if (dummy[i] == ' '){
dummy[i] = '\0';
}
}
*p = strchr(s, ' ');
p++;
}
int main(){
char s[10];
char dummy[10];
char l;
gets(s);
separate(s, dummy, &l);
puts(dummy);
puts(l);
}
I'm having trouble passing the last name to the main function as a string, the goal is to separate a string that consists of someone's first and last name.

#include <stdio.h>
#include <string.h>
void separate(char name[], char first[], char last[]){
strcpy(first, name);
for (int i = 0; i < strlen(first); i++){
if (first[i] == ' '){
first[i] = '\0';
}
}
char *p = strchr(name, ' ');
p++;
strcpy(last, p);
}
int main(){
char n[10], f[10], l[10];
gets(n);
separate(n, f, l);
puts(f);
puts(l);
}
I figured it out, thanks for the help anyway.

Here is an example of using fgets:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFSIZE 20
#define NAMESTRLEN 10
void separate(char buffer[], char firstname[], char lastname[]);
int
main(void) {
char buffer[BUFFSIZE], firstname[NAMESTRLEN], lastname[NAMESTRLEN];
size_t slen;
printf("Enter first and last name: ");
if (fgets(buffer, BUFFSIZE, stdin) == NULL) {
printf("Error reading into buffer.\n");
exit(EXIT_FAILURE);
}
slen = strlen(buffer);
if (slen > 0) {
if (buffer[slen-1] == '\n') {
buffer[slen-1] = '\0';
} else {
printf("Exceeded buffer length: %d.\n", BUFFSIZE);
exit(EXIT_FAILURE);
}
}
if (!*buffer) {
printf("Nothing entered.\n");
exit(EXIT_FAILURE);
}
separate(buffer, firstname, lastname);
printf("Firstname = %s\n", firstname);
printf("Lastname = %s\n", lastname);
return 0;
}
void
separate(char buffer[], char firstname[], char lastname[]) {
int i;
char *last;
const char sep = ' ';
for (i = 0; buffer[i] != sep; i++) {
firstname[i] = buffer[i];
}
firstname[i] = '\0';
last = strchr(buffer, sep);
last++;
strcpy(lastname, last);
}

Related

Reading in strings without header <string.h>

I am a newbie in C and for an exercise I have to write a program, where I can read in strings. If my reserved memory (length BUFFER_SIZE) isn't enough, it should reserve memory in increments of +=BUFFER_SIZE, as long as needed to read the string. I tried to write some functions to get this done, but it doesn't work. Can somebody please help me?
My code:
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#define BUFFER_SIZE 10
size_t string_length(char *string)
{
size_t i = 0;
while(string[i] != '\0')
{
i++;
}
return i;
}
void string_concatenate(char *string, char *string_to_chain)
{
size_t length = string_length(string);
for(size_t i = 0; *(string_to_chain + i) != '\0'; i++, length++)
{
string[length] = string_to_chain [i];
}
string[length] = '\0';
}
char *string_search(char *string, char character)
{
do
{
if (*string == character)
{
return (char*)string;
}
} while (*string++);
return NULL;
}
char *get_line()
{
int line_size = BUFFER_SIZE;
char* line = malloc(line_size * sizeof(char));
if(line == NULL)
{
return NULL;
}
printf("Bitte Text eingeben: \n");
fgets(line, BUFFER_SIZE, stdin);
char *new_line_character = string_search(line, '\n');
while(new_line_character == NULL)
{
line_size += BUFFER_SIZE;
line = realloc(line, (line_size * sizeof(char)));
if(line == NULL)
{
return NULL;
}
char *new_line = line + BUFFER_SIZE - 1;
fgets(new_line, line_size, stdin);
new_line_character = string_search(line, '\n');
}
*new_line_character = '\0';
return line;
}
}
int main(void) {
char *string = get_line();
printf("s%\n", string);
}

Lowercase to Uppercase in C

(I'm posting this again with some changes.) I'm creating a program that would convert all the letters in the words (173528 from a text file) from lower case to upper case letters. Here's my program
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define NWORDS 173528
typedef char String[29];
void
Cap(char *Words[] )
{
int i = 0;
while (Words[i] != '\0') {
Words[i] = Words[i] - 32;
i++;
}
}
void
Initialize(char *Words[])
{
int i;
String word;
char *pch;
for (i = 0; i < NWORDS; i++) {
scanf("%s", word);
pch = malloc(sizeof(char) * (strlen(word) + 1) );
if (pch == NULL) {
printf("Memory is no enough\n");
exit(1);
}
strcpy( pch, word);
Words[i] = pch;
}
}
void
Print(char *Words[])
{
}
void
Free(char *Words[])
{
}
int
main()
{
char *Words[NWORDS];
Initialize(Words);
Cap(Words);
Print(Words);
Free(Words);
return 0;
}
No compiler error but expected output wont display. Thank you in advance for your help!
while (Words[i] != '\0') {
Words[i] = Words[i] - 32;
The approach above is wrong. To access individual character, you need to use
Words[i][j] // i-th word, j-th letter in the word.

Trying to get the substring but the program is returning empty string

In the code below, in "parse" function I am trying to get substring from the string "line". I am successfully printing the "method" variable, but "requesttarget" and "httpversion" variables are empty for some reason.
(ps all these printf's are also inside my parse function)
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
//prototypes
bool parse(const char* line, char* abs_path, char* query);
int strindex(char** pos, const char* str);
void substr(int start, int end, char* holder, const char* line);
int main(void)
{
const char* line = "GET /hello.php?name=Alice HTTP/1.1";
char* abs_path = NULL;
char* query = NULL;
if(parse(line, abs_path, query))
{
printf("It works!\n");
}
}
bool parse(const char* line, char* abs_path, char* query)
{
char* space;
int firstspace;
int secondspace;
char* method = malloc(50 * sizeof(char));
char* requesttarget = malloc(50 * sizeof(char));
char* httpversion = malloc(50 * sizeof(char));
space = strchr(line, ' ');
printf("%p\n", space);
//checks if strchr returns
if(space == NULL)
{
return false;
}
//index in INT of the character
firstspace = strindex(&space, line);
printf("%i\n", firstspace);
//stores the method
substr(0, firstspace, method, line);
space = strrchr(line, ' ');
printf("%p\n", space);
//index in INT of the character
secondspace = strindex(&space, line);
printf("%i\n", secondspace);
//checks if strchr returns
if(space == NULL)
{
return false;
}
//firstspace should come before secondspace
if(firstspace > secondspace)
{
return false;
}
//stores request - target
substr(firstspace + 1, secondspace, requesttarget, line);
//stores http-version
substr(secondspace + 1, strlen(line), httpversion, line);
printf("method: %s\n", method);
printf("requesttarget: %s\n", requesttarget);
printf("httpversion: %s\n", httpversion);
return true;
}
int strindex(char** pos, const char* str)
{
for(int i = 0, n = strlen(str); i < n; i++)
{
if((str + i) == *pos)
{
return i;
}
}
return -1;
}
void substr(int start, int end, char* holder, const char* line)
{
//char* holder = malloc(50 * sizeof(char));
int i = start;
for(; i < end; i++)
{
holder[i] = line[i];
}
holder[i] = '\0';
//return holder;
}
void substr(int start, int end, char* holder, const char* line)
{
//char* holder = malloc(50 * sizeof(char));
int i = start, j=0;
for(; i < end; i++)
{
holder[j++] = line[i];
}
holder[j] = '\0';
//return holder;
}
you were not storing data in holder from 2nd iteration properly.
from 2nd iteration start = 3 and end = 25. While storing in holder your index starts from 3, which is correct for line but not for holder.
Add one more variable to start the index for holder from 0.
A smaller version: (untested)
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
int main(void)
{
const char* line = "GET /hello.php?name=Alice HTTP/1.1";
char method[32], request[1024], version[32], *src, *dest, *end;
for(src=line, end=(dest=method)+sizeof(method)-1 ; *src!='\0' && *src!=' ' && dest<end; src++, dest++) *dest=*src;
*dest='\0';
while(*src==' ') src++;
for(end=(dest=request)+sizeof(request)-1 ; *src!='\0' && *src!=' ' && dest<end; src++, dest++) *dest=*src;
*dest='\0';
while(*src==' ') src++;
for(end=(dest=version)+sizeof(version)-1 ; *src!='\0' && *src!=' ' && dest<end; src++, dest++) *dest=*src;
*dest='\0';
printf("method: %s\n", method);
printf("requesttarget: %s\n", request);
printf("httpversion: %s\n", version);
}

Remove '|' from a string

I wrote the below code which replaces '|' characters from the string.
#include <stdio.h>
#include <stdlib.h>
void remove_pipes(char*);
main (int argc, char **argv)
{
char string1[] = "|||||||||||||";
remove_pipes(string1);
printf("String1 = %s", string1);
char string2[] = "h|e|l|l|o";
remove_pipes(string2);
printf("String2 = %s", string2);
}
void remove_pipes(char* input)
{
for(; *input; input++)
{
if(*input == '|')
{
*input = ' ';
}
}
}
Now I need to modify this method to remove the '|' character from the string. I am not sure how to do that. Hope someone can give me some hint.
Use a char pointer to travel the input and modify it:
#include <stdio.h>
#include <stdlib.h>
void remove_pipes(char*);
main (int argc, char **argv)
{
char string1[] = "|||||||||||||";
printf("String1 = %s\n", string1);
remove_pipes(string1);
printf("String1 = %s\n", string1);
char string2[] = "h|e|l|l|o";
printf("String2 = %s\n", string2);
remove_pipes(string2);
printf("String2 = %s\n", string2);
}
void remove_pipes(char* input)
{
unsigned idx = 0;
char* aux = input;
for(; *input; input++)
{
if (*input != '|')
{
*(aux + idx++) = *input;
}
}
*(aux + idx) = '\0';
}

Using strtok in c

I need to use strtok to read in a first and last name and seperate it. How can I store the names where I can use them idependently in two seperate char arrays?
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="test string.";
char * test;
test = strtok (str," ");
while (test != NULL)
{
printf ("%s\n",test);
test= strtok (NULL, " ");
}
return 0;
}
Here is my take at a reasonably simple tokenize helper that
stores results in a dynamically growing array
null-terminating the array
keeps the input string safe (strtok modifies the input string, which is undefined behaviour on a literal char[], at least I think in C99)
To make the code re-entrant, use the non-standard strtok_r
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char** tokenize(const char* input)
{
char* str = strdup(input);
int count = 0;
int capacity = 10;
char** result = malloc(capacity*sizeof(*result));
char* tok=strtok(str," ");
while(1)
{
if (count >= capacity)
result = realloc(result, (capacity*=2)*sizeof(*result));
result[count++] = tok? strdup(tok) : tok;
if (!tok) break;
tok=strtok(NULL," ");
}
free(str);
return result;
}
int main ()
{
char** tokens = tokenize("test string.");
char** it;
for(it=tokens; it && *it; ++it)
{
printf("%s\n", *it);
free(*it);
}
free(tokens);
return 0;
}
Here is a strtok-free reimplementation of that (uses strpbrk instead):
char** tokenize(const char* str)
{
int count = 0;
int capacity = 10;
char** result = malloc(capacity*sizeof(*result));
const char* e=str;
if (e) do
{
const char* s=e;
e=strpbrk(s," ");
if (count >= capacity)
result = realloc(result, (capacity*=2)*sizeof(*result));
result[count++] = e? strndup(s, e-s) : strdup(s);
} while (e && *(++e));
if (count >= capacity)
result = realloc(result, (capacity+=1)*sizeof(*result));
result[count++] = 0;
return result;
}
Do you need to store them separately? Two pointers into a modified char array will yield two separate perfectly usable strings.
That is we transform this:
char str[] ="test string.";
Into this:
char str[] ="test\0string.";
^ ^
| |
char *s1 ----- |
char *s2 -----------
.
#include <stdio.h>
#include <string.h>
int main ()
{
char str[] ="test string.";
char *firstname = strtok(str, " ");
char *lastname = strtok(NULL, " ");
if (!lastname)
lastname = "";
printf("%s, %s\n", lastname, firstname);
return 0;
}
What about using strcpy:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#define MAX_NAMES 2
int main ()
{
char str[] ="test string.";
char *names[MAX_NAMES] = { 0 };
char *test;
int i = 0;
test = strtok (str," ");
while (test != NULL && i < MAX_NAMES)
{
names[i] = malloc(strlen(test)+1);
strcpy(names[i++], test);
test = strtok (NULL, " ");
}
for(i=0; i<MAX_NAMES; ++i)
{
if(names[i])
{
puts(names[i]);
free(names[i]);
names[i] = 0;
}
}
return 0;
}
It contains much clutter to maintain a complete program and clean its resources, but the main point is to use strcpy to copy each token into its own string.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
char** split(const char *str, const char *delimiter, size_t *len){
char *text, *p, *first, **array;
int c;
char** ret;
*len = 0;
text=strdup(str);
if(text==NULL) return NULL;
for(c=0,p=text;NULL!=(p=strtok(p, delimiter));p=NULL, c++)//count item
if(c==0) first=p; //first token top
ret=(char**)malloc(sizeof(char*)*c+1);//+1 for NULL
if(ret==NULL){
free(text);
return NULL;
}
strcpy(text, str+(first-text));//skip until top token
array=ret;
for(p=text;NULL!=(p=strtok(p, delimiter));p=NULL){
*array++=p;
}
*array=NULL;
*len=c;
return ret;
}
void free4split(char** sa){
char **array=sa;
if(sa!=NULL){
free(array[0]);//for text
free(sa); //for array
}
}
int main(void){
char str[] ="test string.";
char **words;
size_t len=0;
int i;
words = split(str, " \t\r\n,.", &len);
/*
for(char **wk = words; *wk ;wk++){
printf("%s\n", *wk);
}
*/
for(i = 0;i<len;++i){
printf("%s\n", words[i]);
}
free4split(words);
return 0;
}
/* result:
test
string
*/
Copy the results from strtok to a new buffer using a function such as
/*
* Returns a copy of s in freshly allocated memory.
* Exits the process if memory allocation fails.
*/
char *xstrdup(char const *s)
{
char *p = malloc(strlen(s) + 1);
if (p == NULL) {
perror("memory allocation failed");
exit(1);
}
strcpy(p, s);
return p;
}
Don't forget to free the return values when you're done with them.
IMO, you don't need (and probably don't want) to use strtok at all (as in, "for this, or much of anything else"). I think I'd use code something like this:
#include <string.h>
#include <stdlib.h>
static char *make_str(char const *begin, char const *end) {
size_t len = end-begin;
char *ret = malloc(len+1);
if (ret != NULL) {
memcpy(ret, begin, len);
ret[len]='\0';
}
return ret;
}
size_t tokenize(char *tokens[], size_t max, char const *input, char const *delims) {
int i;
char const *start=input, *end=start;
for (i=0; *start && i<max; i++) {
for ( ;NULL!=strchr(delims, *start); ++start)
;
for (end=start; *end && NULL==strchr(delims, *end); ++end)
;
tokens[i] = make_str(start, end);
start = end+1;
}
return i;
}
#ifdef TEST
#define MAX_TOKENS 10
int main() {
char *tokens[MAX_TOKENS];
int i;
size_t num = tokenize(tokens, MAX_TOKENS, "This is a longer input string ", " ");
for (i=0; i<num; i++) {
printf("|%s|\n", tokens[i]);
free(tokens[i]);
}
return 0;
}
#endif
U can do something like this too.
int main ()
{
char str[] ="test string.";
char * temp1;
char * temp2;
temp1 = strtok (str," ");
temp2 = strchr(str, ' ');
if (temp2 != NULL)
temp2++;
printf ("Splitted string :%s, %s\n" , temp1 , temp2);
return
}

Resources