code text into morse using c language - c

The main idea of the program is to code text into morse and decode morse into text.
But the program is broke, i don't know why the program doesn't work when i write the space between two words, and i have this problem with the code and decode.
void encode_out(const char *s){
for(;;++s){
char ch = *s;
if(ch == '\0')
break;
if(isalpha(ch)){
ch = toupper(ch);
fputs(table[ALPHA][ch - 'A'], stdout);//`-'A'` depend on the sequence of character code
} else if(isdigit(ch))
fputs(table[NUM][ch - '0'], stdout);
else if(ch == ' ')
fputc('/', stdout);//need rest space skip ?
else
;//invalid character => ignore
fputc(' ', stdout);
}
fputc('\n', stdout);
}
static void decode_out_aux(MTree *tree, const char *s){
if(tree == NULL) return;
if(*s == '\0')
fputc(tree->value, stdout);
else if(*s == '/')
fputc(' ', stdout);
else if(*s == '.')
decode_out_aux(tree->dot, ++s);
else if(*s == '-')
decode_out_aux(tree->bar, ++s);
}
void decode_out(const char *s){
char *p;
while(*s){
p = strchr(s, ' ');
if(p){
if(p-s != 0){
char code[p-s+1];
memcpy(code, s, p-s);
code[p-s]='\0';
decode_out_aux(root, code);
}
s = p + 1;
} else {
decode_out_aux(root, s);
break;
}
}
fputc('\n', stdout);
}
static void insert_aux(MTree **tree, char ch, const char *s){
if(*tree == NULL)
*tree = calloc(1, sizeof(**tree));
if(*s == '\0')
(*tree)->value = ch;
else if(*s == '.')
insert_aux(&(*tree)->dot, ch, ++s);
else if(*s == '-')
insert_aux(&(*tree)->bar, ch, ++s);
}
static inline void insert(char ch, const char *s){
if(*s == '.')
insert_aux(&root->dot, ch, ++s);
else if(*s == '-')
insert_aux(&root->bar, ch, ++s);
}
void make_tree(void){
root = calloc(1, sizeof(*root));
//root->value = '/';//anything
int i;
for(i = 0; i < 26; ++i)
insert('A'+i, table[ALPHA][i]);
for(i = 0; i < 10; ++i)
insert('0'+i, table[NUM][i]);
}
static void drop_tree_aux(MTree *root){
if(root){
drop_tree_aux(root->dot);
drop_tree_aux(root->bar);
free(root);
}
}
void drop_tree(void){
drop_tree_aux(root);
}

There are probably some other issues, but at least you need to fix this:
void encode_out(const char *s)
In this function, s is an input parameter and it looks certain that you are modifying its content, therefore s cannot have const modifier. Change that to:
void encode_out(char *s)
Look for other functions in your program that have similar problems, such as decode_out

char *text;
char *morse;
...
scanf("%s", &text);
encode_out(&text);
printf("Enter the morse code: ");
scanf("%s", &morse);
decode_out(&morse);
text and morse are pointers, do not add reference operator.
text is uninitialized pointer, it cannot be used in scanf. The usage is as follows:
char text[100];
scanf("%s", text);
For the rest of the code, it is unlikely to work and over complicated. Use a translate function to compare each morse code.
char translate(const char* buf)
{
for (int i = 0; i < 26; i++)
if (strcmp(buf, alpha[i]) == 0)
return 'A' + (char)i;
if (strcmp(buf, "/") == 0)
return ' ';
for (int i = 0; i < 10; i++)
if (strcmp(buf, num[i]) == 0)
return '0' + (char)i;
//to indicate an error:
return '?';
}
Use strtok to break the morse code in to parts, translate each part and add to output:
char *decode(const char* src)
{
char *code = malloc(strlen(src) + 1);
char *dst = malloc(strlen(src) + 1);
strcpy(code, src);
int dst_len = 0;
char *token = strtok(code, " ");
while (token)
{
dst[dst_len++] = translate(token);
token = strtok(NULL, " ");
}
free(code);
dst[dst_len] = 0;
return dst;
}
int main(void)
{
char *dst = decode(".... . .-.. .-.. --- / .-- --- .-. .-.. -..");
printf("%s\n", dst);
free(dst);
return 0;
}
output: "HELLO WORLD"

Related

Remove a word from a sentence (string)

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.

How to print only the first sentence in string [C]?

Example:
void stringEvaluation(char *name){
if (strstr(name, "Tall") != NULL)
--here I would like to print only "John Doe"--
}
int main{
char name[160 + 1];
scanf("%[^\n]%*c", name);
stringEvaluation(name);
return 0;
}
and this is the input
"John Doe (45). Black. Tall. Brown eyes"
Your request is unclear. For example, do as follows.
#include <stdio.h>
#include <string.h>
#include <ctype.h>
void stringEvaluation(const char *s){
const char *p;
if(s == NULL || *s == '\0'){
puts("invalid format.");
return ;
}
(p = strchr(s, '(')) || (p = strchr(s, '.'));
if(p == NULL){
puts("invalid format.");
return ;
}
//find end
while(!isalpha((unsigned char)p[-1]))
--p;
while(s < p)
putchar(*s++);
}
int main(void){
char name[160 + 1] = "";
scanf("%160[^\n]%*c", name);
stringEvaluation(name);
return 0;
}
I am using strtok() to get the name. Please note, this will work only if you have "()" in your string.
void stringEvaluation(char *name){
if (strstr(name, "Tall") != NULL)
{
char *nameEnd = strtok(name, "("); //Returns address of '('
if(NULL != nameEnd)
{
int nameLength = nameEnd - name - 1; // 1 for space after name.
char *onlyName = malloc((nameLength + 1) * sizeof(char)); // 1 for terminating '\0'
if(NULL != onlyName)
{
//Copy name part.
strncpy(onlyName, name, nameLength);
onlyName[nameLength] = '\0'; //Make onlyName a string
printf("Name: [%s]\n", onlyName);
free(onlyName);
onlyName = NULL;
}
}
}
}
I have assumed that name will be at first place in your input string. Then following small logic will work for you.
void stringEvaluation(char *name){
char *p;
char OutPut[50]={0};
if (strstr(name, "Tall") != NULL)
{
p = strstr(name," ");
p++;
p = strstr(p," ");
strncpy(OutPut,name,(p-name));
printf("name=%s\n",OutPut);
}
}

How to get a pointer back from a void function after it iterates through a string pointer

I'm trying to write a code that goes through a given string using a pointer to parse it.
The original code I wrote worked fine but it was... redundant so I tried making it into a function call to make it more concise. Here is what i have:
char inputArray[300];
char buffer[300];
char username[100];
char password[100];
char name[100];
int i=0;
void repeat(char *to)
{
while(*to!='=')
{
to++;
}
}
void array(char *mm,char *tt)
{
i=0;
while(*tt!='+')
{
mm[i]=*tt;
tt++;
i++;
}
}
int main()
{
printf("give me the shit in this fashion: username=?+password=?+real=?\n");
scanf("%s",inputArray);
strcpy(buffer,inputArray);
char *tok=buffer;
repeat(tok);
tok++;
array(username,tok);
repeat(tok);
tok++;
array(password,tok);
tok++;
repeat(tok);
tok++;
array(name,tok);
}
For some reason it won't give me back the pointer array tok where it left off from the previous function call. why is that? it acts as if after calling it the pointer starts back from the beginning.
Functions receive copies of their arguments. Original arguments remain unaffected.
Giving something back has a special syntax in C: the return statement. Thus
char* repeat (char *to) // <- this function gives back a char*
{
while (*to != '=')
{
to++;
}
return to; // <- giving something back
}
Call it like this:
tok = repeat(tok);
Treat array in the same fashion.
Note 1, this function will result in *undefined behaviour if the string doesn't contain '='.
Note 2, it is also possible to pass a pointer to tok as the other answer suggests, but for sake of clarity it is only recommended to use this style when you need to return more than one thing from a function.
just change your repeat to this:
void repeat(char **to) {
while (**to != '=') {
(*to)++;
}
}
and call it like this:
repeat(&tok);
and always check for errors:
if (scanf("%299s", inputArray) != 1){
printf("incorrect input\n");
return 1;
}
and your sample code (and add check for errors in array and repeat to not go out of bounds):
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
char inputArray[300];
char buffer[300];
char username[300];
char password[300];
char name[300];
int i = 0;
void repeat(char **to) {
while (**to != '=') {
(*to)++;
}
}
void array(char *mm, char *tt){
i = 0;
while (*tt != '+') {
mm[i] = *tt;
tt++;
i++;
}
}
int main() {
printf("give me the shit in this fashion: username=?+password=?+real=?\n");
if (scanf("%299s", inputArray) != 1){
printf("incorrect input\n");
return 1;
}
inputArray[299] = 0;
strcpy(buffer, inputArray);
char *tok = buffer;
repeat(&tok);
tok++;
array(username, tok);
repeat(&tok);
tok++;
array(password, tok);
tok++;
repeat(&tok);
tok++;
array(name, tok);
}
and you may use this to not go out of bounds:
#include <inttypes.h>
#include <stdio.h>
#include <stdint.h>
char* read_str(char *src, char *dst){
char *p, *q;
p = src;
while (*p != 0 && *p != '=') p++;
if (*p == 0) {
*dst = 0;
return NULL; // '=' not found
}
p++;
q = p;
while (*q != 0 && *q != '+') q++;
//if (*q == 0) return NULL;// '+' not found
while (p <= q) *dst++ = *p++;
dst--;
*dst = 0;
q++;
return q;
}
#define MAX_LEN 100
int main() {
char username[MAX_LEN];
char password[MAX_LEN];
char name[MAX_LEN];
char inputArray[MAX_LEN] = "username=Alex+password=123+real=Alex";
char *p = inputArray;
p = read_str(p, username);
if (p == NULL)return 1; // error
p = read_str(p, password);
if (p == NULL)return 1; // error
read_str(p, name);
printf("username: %s \n", username);
printf("password: %s \n", password);
printf(" name: %s \n", name);
}

How to parse a string from fgets to be stored in argv[] for a simple command line interpreter?

I am attempting to create a simple shell program, and take input from the user using fgets(). I store the user input in a char[], and then attempt to parse it using a getword() method I wrote. However, I cannot figure out how to get the getword() method to parse the string. I can print out the commandLine char[] and see exactly what is typed at the prompt, but it does no get parsed. getword() returns the same string unparsed , and tries to store the whole thing in argv[0]. I have tested the getword() method alone and it returns what I want it to. Here is my current attempt:
The main shell program:
int main() {
pid_t pid, first, second;
mode_t fileBits;
size_t ln;
int argc, inputRedirect, firstStatus, i, execvpReturnVal;
int childOutput_fd;
long octalPermissionString;
char *home = getenv("HOME");
char *argv[MAXITEM];
char devNull[10] = "/dev/null";
char commandLine[STORAGE];
struct stat fileStats;
signal(SIGTERM, myhandler);
for (;;) {
background = 0;
printf("p2: ");
fflush(stdout);
/*---------FGETS PROMPT----------*/
fgets(commandLine, STORAGE, stdin);
commandLine[strlen(commandLine) - 1] = '\0';
printf("commandLine = [%s]\n", commandLine);
/*==============OTHER SHELL METHODS=============*/
parse() method that calls getword():
int parse(char *commandLine, char *argv[]) {
int argc = 0;
char *commandPointer;
argv[argc++] = commandLine;
printf("argv[%d] = [%s]\n", argc-1, commandLine);
do{
commandPointer = (char*)malloc(sizeof(char) * STORAGE);
argv[argc++] = commandPointer;
printf("argv[%d] = [%s]\n", argc-1, commandPointer);
getword(commandPointer);
}while(*commandPointer != '\0');
argc--;
argv[argc] = '\0';
return argc;
}
getword() parse method:
/*Function Prototypes*/
int tilde(char *p, int i);
int isMeta(int thisChar);
int getword(char *w) {
int currChar, nextChar, offset;
int index = 0;
int *tildeHelper;
currChar = getchar();
while(currChar == ' ' || currChar == '\t') {
currChar = getchar();
}
if(currChar == EOF)
return -1;
switch(currChar) {
case '\0':
w[index] = '\0';
return index;
case '\n':
w[index] = '\0';
return index;
case ';':
w[index] = '\0';
return index;
case '<':
w[index++] = currChar;
w[index] = '\0';
return index;
case '>':
w[index++] = currChar;
if((nextChar = getchar()) == '>')
w[index++] = currChar;
else {
ungetc(nextChar, stdin);
}
w[index] = '\0';
return index;
case '|':
w[index++] = currChar;
w[index] = '\0';
return index;
case '&':
w[index++] = currChar;
w[index] = '\0';
return index;
case '~':
tildeHelper = &index;
index = tilde(&w[index], *tildeHelper);
default:
w[index++] = currChar;
while((currChar = getchar()) != ' ' && currChar != '<' && currChar != '>' && currChar != '|' && currChar != ';' && currChar != '&' && currChar != '\t' && currChar != '\n' && currChar != '\0' && currChar != EOF && index <= STORAGE - 1) {
switch(currChar) {
case '~':
tildeHelper = &index;
index = tilde(&w[index], *tildeHelper);
break;
case '\\':
nextChar = getchar();
if(metaCharacter(nextChar))
w[index++] = nextChar;
else {
ungetc(nextChar, stdin);
w[index++] = currChar;
}
break;
default:
w[index++] = currChar;
}
}
ungetc(currChar, stdin);
w[index] = '\0';
return index;
}
}
int tilde(char *cp, int i) {
int *ip = &i;
char *p = cp;
char *o;
o = (strcpy(p, getenv("HOME")));
int offset = strlen(o);
*ip = *ip + offset;
return i;
}
int metaCharacter(int thisChar) {
int isMeta = 0;
switch(thisChar) {
case '~':
isMeta = 1;
break;
case '<':
isMeta = 1;
break;
case '>':
isMeta = 1;
break;
case '|':
isMeta = 1;
break;
case ';':
isMeta = 1;
break;
case '&':
isMeta = 1;
break;
case '\\':
isMeta = 1;
break;
}
return isMeta;
}
You are trying to read the command line from stdin twice. First, you read the whole line with fgets(), then, in getword you try to get the next character with getchar(). This tries, in effect, to read a second line. You should read the char buffer that contains the line instead.
Update your getword function to take a second argument src, the line to read. Then replace all calls to getline with *src++:
int getword(char *w, const char *src) {
int currChar, nextChar, offset;
int index = 0;
int *tildeHelper;
currChar = *src++;
while(currChar == ' ' || currChar == '\t') currChar = *src++;
/* ... */
}
Your parse function must then pass the command line to getword and use the return value from getword to update the string position:
int parse(char *commandLine, char *argv[])
{
int argc = 0;
int index = 0;
while (1) {
char *commandPointer = malloc(sizeof(char) * STORAGE);
index += getword(commandPointer, commandLine + index);
if (*commandPointer == '\0') break;
argv[argc++] = commandPointer;
}
return argc;
}
You then still have issues in the code, for example not getting the return value of getword right and not freeing the argument strings.
Also, for implementing a shell, have a look at getline, which is a better command-line input interface than plain old `fgets´.

String Search and format in C

Just a quick one: in C I have a buffer full of data like below:
char buffer[255]="CODE=12345-MODE-12453-CODE1-12355"
My question is how to search through this. For example for the CODE=12345, section bear in mind that the numbers change, so I would like to search like this CODE=***** using wildcard or preset amount of spaces after the CODE= part.
This method wont compile last one left to try
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main ()
{
char buf[255]="CODE=12345-MODE-12453-CODE1-12355";
#define TRIMSPACES(p) while(*p != '\0' && isspace((unsigned char)*p) != 0) ++p
#define NSTRIP(p, n) p += n
#define STRIP(p) ++p
char* getcode(const char *input)
{
char *p = (char*) input, *buf, *pbuf;
if((buf = malloc(256)) == NULL)
return NULL;
pbuf = buf;
while(*p != '\0') {
if(strncmp(p, "CODE", 3) == 0) {
NSTRIP(p, 4); //remove 'code'
TRIMSPACES(p);//trim white-space after 'code'
if(*p != '=')
return NULL;
STRIP(p); // remove '='
TRIMSPACES(p); //trim white-spaces after '='
/* copy the value until found a '-'
note: you must be control the size of it,
for avoid overflow. we allocated size, that's 256
or do subsequent calls to realloc()
*/
while(*p != '\0' && *p != '-')
*pbuf ++ = *p++;
// break;
}
p ++;
}
//put 0-terminator.
*pbuf ++ = '\0';
return buf;
}
//
}
You could use the sscanf() function:
int number;
sscanf(buffer, "CODE = %i", &number);
for that to work well your buffer has to be null terminated.
Another way to do it instead of sscanf():
char *input, *code;
input = strstr(buf, "CODE");
if(input == NULL) {
printf("Not found CODE=\n");
return -1;
}
code = strtok(strdup(input), "=");
if(code != NULL) {
code = strtok(NULL, "-");
printf("%s\n", code); // code = atoi(code);
} else {
//not found '='
}
Or more robust way.. a bit more complex:
#define TRIMSPACES(p) while(*p != '\0' && isspace((unsigned char)*p) != 0) ++p
#define NSTRIP(p, n) p += n
#define STRIP(p) ++p
char* getcode(const char *input, size_t limit)
{
char *p = (char*) input, *buf, *pbuf;
size_t i = 0;
while(*p != '\0') {
if(strncmp(p, "CODE", 3) == 0) {
NSTRIP(p, 4); //remove 'code'
TRIMSPACES(p);//trim all white-spaces after 'code'
/* check we have a '=' after CODE (without spaces).
if there is not, returns NULL
*/
if(*p != '=')
return NULL;
/* ok. We have.. now we don't need of it
just remove it from we output string.
*/
STRIP(p);
/* remove again all white-spaces after '=' */
TRIMSPACES(p);
/* the rest of string is not valid,
because are white-spaces values.
*/
if(*p == '\0')
return NULL;
/* allocate space for store the value
between code= and -.
this limit is set into second parameter.
*/
if((buf = malloc(limit)) == NULL)
return NULL;
/* copy the value until found a '-'
note: you must be control the size of it,
for don't overflow. we allocated 256 bytes.
if the string is greater it, do implementation with
subjecents call to realloc()
*/
pbuf = buf;
while(*p != '\0' && *p != '-' && i < limit) {
*pbuf ++ = *p++;
i ++;
}
*pbuf ++ = '\0';
return buf;
}
p ++;
}
return NULL;
}
And then:
char buf[255] = "foo baa CODE = 12345-MODE-12453-CODE-12355";
char *code = getcode(buf,256);
if(code != NULL) {
printf("code = %s\n", code);
free(code);
} else {
printf("Not found code.\n");
}
output:
code = 12345
Check out this online.
if you want to don't differentiate case, you can use the strncasecmp() that's POSIX function.
Assuming the CODE= part always comes at the beginning of the string, it's pretty easy:
sscanf(buffer, "CODE = %d", &number);
...but you want buffer to be char[255], not unsigned long.
Edit: If the CODE= part isn't necessarily at the beginning of the string, you can use strstr to find CODE in the buffer, do your sscanf starting from that point, then look immediately following that:
int codes[256];
char *pos = buffer;
size_t current = 0;
while ((pos=strstr(pos, "CODE")) != NULL) {
if (sscanf(pos, "CODE = %d", codes+current))
++current;
pos += 4;
}
Edit2:
For example, you'd use this something like this:
#include <stdio.h>
#include <string.h>
#include <windows.h>
int main ()
{
// This is full of other junk as well
char buffer[255]="CODE=12345 MODE-12453 CODE=12355" ;
int i;
int codes[256];
char *pos = buffer;
size_t current = 0;
while ((pos=strstr(pos, "CODE")) != NULL) {
if (sscanf(pos, "CODE = %d", codes+current))
++current;
pos += 4;
}
for (i=0; i<current; i++)
printf("%d\n", codes[i]);
return 0;
}
For me, this produces the following output:
12345
12355
...correctly reading the two "CODE=xxx" sections, but skipings over the "MODE=yyy" section.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char *getcode(const char *str, const char *pattern){
//pattern: char is match, space is skip, * is collect
static const char *p=NULL;
char *retbuf, *pat;
int i, match, skip, patlen;
if(str != NULL) p=str;
if(p==NULL || *p=='\0') return NULL;
if(NULL==(retbuf=(char*)malloc((strlen(p)+1)*sizeof(char))))
return NULL;
pat = (char*)pattern;
patlen = strlen(pat);
i = match = skip = 0;
while(*p){
if(isspace(*p)){
++p;
++skip;
continue;
}
if(*pat){
if(*p == *pat){
++match;
++p;
++pat;
} else if(*pat == '*'){
++match;
retbuf[i++]=*p++;
++pat;
} else {
if(match){//reset
pat=(char*)pattern;
p -= match + skip -1;
i = match = skip = 0;
} else //next
++p;
}
} else {
break;
}
}
if(i){//has match
retbuf[i++]='\0';
retbuf=realloc(retbuf, i);
return retbuf;
} else {
free(retbuf);
return NULL;
}
}
int main (){
char *code;
code=getcode("CODE=12345-MODE-12453-CODE1-12355", "CODE=*****");
printf("\"%s\"\n",code);//"12345"
free(code);
code=getcode(" CODE = 12345-MODE-12453-CODE1-12355", "CODE=*****");
printf("\"%s\"\n",code);//"12345"
free(code);
code=getcode("CODE-12345-MODE-12453-CODE1-12355", "CODE=*****");
if(code==NULL)printf("not match\n");//not match
code=getcode("CODE=12345-MODE-12453-CODE=12355", "CODE=*****");
printf("\"%s\"\n",code);//"12345"
free(code);
code=getcode(NULL, "CODE=*****");
printf("\"%s\"\n",code);//"12355"
free(code);
code=getcode("CODE=12345-MODE-12453-CODE1-12355", "CODE=*****");
printf("\"%s\"\n",code);//"12345"
free(code);
code=getcode(NULL, "CODE1-*****");
printf("\"%s\"\n",code);//"12355"
free(code);
return 0;
}

Resources