2
votes
2answers
114 views

string to integer (implement atoi)

Implement atoi to convert a string to an integer. Requirements for atoi: The function first discards as many whitespace characters as necessary until the first non-whitespace character is ...
3
votes
3answers
110 views

Length of last word

Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is ...
2
votes
2answers
105 views

Trim function in C

I am trying to write an idiomatic trim function in C. How does this look? Should I instead malloc the new string and return it? void trim(const char *input, char *result) { int i, j = 0; for (i = ...
0
votes
1answer
39 views

split up a string into whitespace-seperated fields

I am trying to improve my C skills, and I hope someone might be able to provide me with some feedback on the following code. I avoid strtok function intentionally. #define MAX_SIZE 1000 int ...
0
votes
0answers
50 views

custom cmdline implementation

The below program takes the inputted string from commandline and parses it and calls the corresponding function. commands are either single word or multiple words. say for e.g. -#version ...
1
vote
1answer
158 views

first non-repeated character in a string in c

Write an efficient function to find the first non-repeated character in a string. For example, the first non-repeated character in "total" is 'o' and the first non-repeated character in "teeter" is ...
7
votes
4answers
277 views

Count words in a C string

I have a string that must be splitted into an array of strings so using strtok makes perfect sense but before that I want to allocate the array of strings and thus I count how many words in it. ...
0
votes
2answers
209 views

Given a sequence of words, print all anagrams together

Given an array of words, print all anagrams together. For example, if the given array is {“cat”, “dog”, “tac”, “god”, “act”}, then output may be “cat tac act dog god”. My idea is to sort ...
7
votes
4answers
608 views

Reverse each word of a string in C

I had this interview question like a year ago, I was asked to code on a piece of paper how to reverse each word of a string. Since I am used to java I proposed the obvious answer of using split + ...
5
votes
2answers
707 views

reverse every word in a string(should handle space)

Examples: char test1[] = " "; char test2[] = " hello z"; char test3[] = "hello world "; char test4[] = "x y z "; Results: " " " olleh z" "olleh dlrow " "x ...
0
votes
2answers
133 views

symmetric string checking [closed]

I wrote this routine in C to check the symmetry of a string using only a stack. (This is absolutely for educational purpose in our Theory Of Computation Class) Anyways, does this look right? For some ...
2
votes
2answers
151 views

C simple text file i/o

I wanted to learn how to work with file I/O properly so i've found an assignment in my college papers and decided to write it. So here it is. //Write a C program which reads data about books and ...
2
votes
3answers
605 views

String in ANSI C?

I trying declare string in ANSI C and not sure what is the best way to do it. For example #include <stdio.h> #include <stdlib.h> #include <string.h> #include <stdarg.h> char ...
3
votes
3answers
938 views

Splitting a string into tokens in C

I am trying to improve my skill in C, and this time by getting away from strtok. The following code splits the input string into a token_list, an array of strings. #include <stdio.h> #include ...
0
votes
1answer
203 views

C-Style unsigned char parsing and manipulation in C/C++ - Proper algorithm correction and segmentation fault

Note that I'm using a C++ compiler ( hence, the cast on the calloc function calls) to do this, but the code is essentially C. Basically, I have a typedef to an unsigned char known as viByte, which ...
2
votes
2answers
237 views

Read until EOF and realloc as needed

This is a function that reads until EOF and reallocs as it goes. char * read_to_end(int fd, int *bread) { ssize_t size, nread, rc; char *buf, *tmp; nread = 0; size = MINLEN; buf ...
3
votes
1answer
201 views

Newbie C string manipulation

I'd just like to get some pointers (pun very much intended!) on my newbie-ish C here. The intention is to have an environment variable provide the "prefix" for any paths this app needs. So for ...
2
votes
2answers
311 views

Defensive programming in C

I wrote a function (get_last_word_of) that takes a C string and a destination buffer, and then copies the last word into the buffer. The code was originally C++, and was changed into C later. ...
5
votes
6answers
2k views

Finding the most frequent character in a string

This is in C/C++ (using a c-string as input). I'm curious if my solution could be more efficient than it currently is. char mostFrequent(char *bytes, int length) { char holder[26]; for (int i = ...
2
votes
2answers
89 views

Keyboard Printing with Teensy

I'm trying to write a piece of code that will mimic ASCII keyboard output. It uses the keyboard library from pjrc, but the application I'm working on requires outputting string sequences rather than ...
2
votes
1answer
208 views

binary searching strings

/*I wrote a quick and dirty program to binary search an ordered array (list) of C style strings. For example - {“1”, “12”, “23”, “124”} etc (though my input set is actually much different). NOTE: ...
2
votes
4answers
249 views

is it a good strlcat() implementation?

I'd like to know if i respect strlcat(3) behiour with my implementation, cause i'm not really sure to understand strlcat particular behaviour int my_strlcat(char *dest, char *src, int size) { int ...
4
votes
3answers
6k views

What's the best method to initialize character array in C and why? [closed]

I have seen C strings initialized to empty string and a null character. char str[10] = ""; or char str[10] = {'\0'} I am just confused which one is the best practice and I would like to know if ...
5
votes
6answers
202 views

Struct to web style string

I've had to make several functions to turn some structures into strings. I am a still green when it comes C so I am unsure if I am doing this a very awkward way. The system I am coding for does not ...