I have two string shown below:
char code *text_to_compare = "TesT";
char code *dictionary = "TesTT,Tes,Tes,TesT.";
In a part of the program I used the following code where it increments the pointers for both strings to point to the next characters.
ch_A = text_to_compare[i++];
ch_B = dictionary[j++];
Why is pointer j being incremented but pointer i is remaining as it was?
Thanks in advance.
EDIT: Below is the full code. The aim of this project is to compare a string with a list of words. Integer i is not incrementing only after the program enters the else statement.
#include <string.h>
char code *text_to_compare = "TesT";
char code *dictionary = "TesTT,Tes,Tes,TesT.";
int bring_characters(char pdata *, char pdata *, char ch_A, char ch_B, char i,
char j);
void main(void) {
unsigned char ch_A;
unsigned char ch_B;
unsigned char i = 0;
unsigned char j = 0;
char pdata N1;
char pdata N2;
int result;
ch_A = text_to_compare[i]; // take a caharacter from the text
ch_B = dictionary[j];
result = bring_characters(&N1, &N2, ch_A, ch_B, i, j);
if (result == 0) {
while (1)
;
}
else {
while (1)
;
}
while (1)
;
}
int bring_characters(char pdata *N1, char pdata *N2, char ch_A, char ch_B,
char i, char j) {
do {
if (ch_A == ch_B) {
ch_A = text_to_compare[i++]; // take a caharacter from the text
ch_B = dictionary[j++];
if ((ch_A == '\0') && ((ch_B == ',') || (ch_B == '.'))) {
while (1)
; // load idata-------------------------------------------------------------------------------------------------
}
}
else {
i = 0; // refresh pointer
ch_A = text_to_compare[i]; // take a caharacter from the text
ch_B = dictionary[j++];
}
} while (ch_B != '.');
return (0);
}
Whew, there's a lot going on here! Now that you've added the full code it looks like in your attempt to move on to the next word you have prevented yourself from moving on.. you'll need to do some major revisions to get this guy working.
The first thing you need to do is figure out how you would do this on paper, then step by step try to reproduce that in your code.
Here's a function to kickstart you:
int find_next_match(char toFind, int startingPosition, char* mainString){
int counter = startingPosition;
char buf = mainString[counter];
while(buf != NULL){
if (buf == toFind){
return counter;
}
counter++;
buf = mainString[counter];
}
return -1; //error
}
You can use something like this to find the next instance of the first character in your string, then you can implement a loop to determine if that is a match.
Good luck, you can do it!
Related
I have been trying to convert a string in array of integers, floats and characters. While I could get it work for integers and floats, there is some problem for characters.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char *s1;
int k, no=5;
char* variable = "R1,R2,R3,R4,R5";
void* value;
s1 = calloc(no,sizeof(char)*81);
for (k=0; k<no; k++) s1[k] = strdup(mchar);
ListChar(variable, s1, no, ",");
memcpy(value, s1, no*sizeof(char)*81);
free(s1);
int i;
for (i = 0; i < no; i++)
printf("%s", value[i]);
printf("\n");
return 0;
}
In the header file I have
#define mchar "A...(81times)"
Implementation:
int ListChar(char *buf, char *list, int maxloop, char* delim)
{
int n = 0;
char *s,*t;
s= strdup(buf);
t= strtok(s,delim);
while ( t && (n<maxloop))
{
if (list!=NULL) list[n] =strdup(t);
n++;
t=strtok(NULL,delim);
}
free(s);
return(n);
}
During the calloc memory assignment when I watch s1 its 0xsomeadress ""
After the for loop s1 becomes 0xsomeadress "Garbage value 81 times"
When s1 is assigned to list its still reads the same garbage value.
And when list [n] = strdup(t) list[0] reads the first block of garbage value like -21 '\221 ṗ'.
t is getting delimited correctly. I even tried initializing char *s1[81] = {"something"} and looping it on j but it wont work, same problem, and I need to free s1 at the end because this function runs for number of times. I did it for integers and floats by list[n]=atoi(t) it works fine. Can anyone suggest me something?
There seems to be a fundamental misunderstanding about how strings work. Your s1 clearly needs to be a char ** and the usage of strdup is incorrect. If s1 is of type char *, then s1[k] is of type char. But strdup returns a char *, so s1[k] = strdup ... is clearly an error which your compiler ought to warn you about. Perhaps you want something like:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void * xmalloc(size_t s);
void
ListChar(const char *buf, char **list, int maxloop, int delim)
{
char set[] = {delim, 0};
for( int n = 0; n < maxloop; n += 1 ){
size_t len = strcspn(buf, set);
list[n] = xmalloc(len + 1);
memcpy(list[n], buf, len);
buf += len + 1;
}
}
int
main(int argc, char **argv)
{
int delim = ',';
(void)argc; /* Suppress compiler warning */
while( *++argv ){
char **s1;
int k, num = 1;
char *input = *argv;
for( const char *p = input; *p; p += 1 ){
if( *p == delim ){
num += 1;
}
}
s1 = xmalloc(num * sizeof *s1);
ListChar(input, s1, num, delim);
for( int i = 0; i < num; i += 1 ){
printf("%s\n", s1[i]);
}
free(s1);
}
return 0;
}
void *
xmalloc(size_t s)
{
void *rv = malloc(s);
if( rv == NULL ){
perror("malloc");
exit(EXIT_FAILURE);
}
return rv;
}
Note that the above code scans each string twice, which is not ideal. Rather than scanning the string to find the number of delimiters and then parsing the string, it would be better to do both in one pass. But for the purposes of demonstrating how to break up the string, that seems like unnecessary complexity. (Though it's actually simpler, IMO)
A task I have is to recreate the function strrchr. I figured out a way to iterate backward from the input given and stopping at the character I need to stop at, but the string came out backwards obviously. I already had created a function to reverse a string so I used that to reverse it back to normal. It works, but somewhere in my while loop when stopping at the character, it adds extra characters. Please help! I don't understand why!
#include <stdio.h>
#include <string.h>
// #include <stddef.h>
int
main () {
char* my_strrchr(char* param_1, char param_2)
{
int i = strlen(param_1) - 1;
int q = 0;
char new[strlen(param_1)];
char *new_ptr = new;
while (i >= 0) {
new[q] = param_1[i];
printf("%c\n", new[q]);
if (param_1[i] == param_2) {
i = 0;
}
i--;
q++;
}
int size = strlen(new_ptr) - 1;
for (int i = 0, q = size; i < q; i++, q--) {
char temp = new_ptr[i];
new_ptr[i] = new_ptr[q];
new_ptr[q] = temp;
}
printf("%s", new_ptr);
return (char *)new_ptr;
}
char *phrase = "C Language is HARD.";
char c = 'g';
my_strrchr(phrase, c);
return 0;
}
You don't need to do anything fancy. Just walk the string from the beginning, updating a variable with the address of the character you're looking for every time it's found, and return it when you hit the end of the string (Unlike strchr(), where you return after the first match). That way you only need one pass through the string instead of the two times it takes if you first find the length and then go backwards.
#include <stdio.h>
#include <stdlib.h>
// Really should return a const char*. Silly standard.
char *my_strrchr(const char *s, int c) {
const char *pos = NULL;
while (*s) {
if ((unsigned char)*s == (unsigned char)c) pos = s;
s++;
}
if (c == 0) {
// If searching for '\0', return a pointer to the one
// at the end of the string
return (char *)s;
} else {
return (char *)pos;
}
}
int main(void){
const char *foo = "the quite wet duck quacks a lot";
puts(my_strrchr(foo, 'q'));
return 0;
}
EDIT: I should add how I have this all set up. The struct definition and prototypes are in mystring.h. The function definitions are in mystring.c. The main is in mystringtest.c. For mystring.c and mystringtest.c, I have #include "mystring.h" at the top. I'm compiling like gcc -o test.exe mystring.c mystringtest.c. Not sure if any of that matters, but I'm new with C so I'm just trying to include everything.
I have a good deal of experience with Java but am pretty new to C. I imagine this is related to pointers and memory but I'm totally at a loss here for what's going on. Here's my code:
typedef struct {
char *chars;
int length;
int maxSize;
} String;
int main() {
char *a;
a = readline();
String *s = newString(a);
int b = length(s);
printf("length is %d \n", b);
}
I run the program and enter "hello" (as prompted by readline()). I've stepped through the program and after length(s), s->chars is still a pointer to the array of chars 'hello'. After the print statement, s->chars becomes a pointer to the array of chars 'Length is %d \n'. I'm totally at a loss for what I'm doing wrong. I'm working on a virtual machine if that matters at all. Any help is greatly appreciated. I'll give the code for newString and length too.
int length(String *s) {
char *temp = s->chars;
char b = *temp;
int count;
if (b == '\0') { count = 0; }
else { count = 1; }
while (b != '\0') {
b = *(temp+count);
count++;
}
return count;
}
String *newString(char *s) {
String st;
st.length = 20;
st.maxSize = MAXCHAR;
char *temp = malloc(20 * sizeof(char));
char b = *s;
int count = 0;
while (b != '\0') {
*(temp + count) = b;
count++;
b = *(s+count);
if (count == st.maxSize) { break; }
if (count == st.length) {
st.length = st.length + 20;
temp = realloc(temp, st.length * sizeof(char));
}
}
st.chars = temp;
return &st;
}
String *newString(char *s) {
String st;
...
return &st;
}
You are returning a pointer to a local variable. After newString returns, the local variable no longer exists, so you have a dangling pointer.
Either allocate st with malloc, or return it by value.
you must null terminate the string after the while loop, you have not left space for the null terminator. Also I don't see why you need to realloc
//using strlen will eliminate the need for realloc, +1 is for the null terminator
int len = strlen(s)
char *temp = malloc((len * sizeof(char)) +1);
//null terminate
*(temp+count) = '\0';
st.chars = temp;
Im trying to copy part of a string to another string using pointers. My resulting string starts to copy at the correct place though it doesn't stop after exceeding the count. Also the string isn't copy from the source string rather than from the result parameter
#include <stdio.h>
char *getSub(const char *orig, int start, int count, char *res);
int main(void)
{
const char orig[] = "one two three";
char res[] = "123456789012345678";
printf("%s\n",getSub(orig, 4, 3, res));
return 0;
}
char *getSub(const char *orig, int start, int count, char *res)
{
const char *sCopy = orig;
while (*orig)
{
if (start >= (orig - sCopy)) && (res-sCopy < count))
{
*res++ = *orig++;
}
else
*orig++;
}
return res;
}
The big mistake is that you're calculating the difference of two unrelated pointers, res - sCopy (I suppose sourceCopy is also sCopy in the real code, or the other way round). Calculating the difference of pointers is only meaningful if both pointers point into (or one past the end of) the same array. As written, whether anything gets copied at all depends on the arbitrary locations of the two arrays.
if (start >= (orig - sourceCopy)) && (res-sCopy < c))
{
*res++ = *orig++;
}
else
*orig++;
anyway, that doesn't count how many characters are copied if any are copied at all.
Another mistake is that you don't 0-terminate the copy.
A correct implementation would be
char *getSub(const char *orig, int start, int count, char *res)
{
char *from = orig, *to = res;
// check whether the starting position is within orig
for( ; start > 0; --start, ++from)
{
if (*from == 0)
{
res[0] = 0;
return res;
}
}
// copy up to count characters from from to to
for( ; count > 0 && *from; --count)
{
*to++ = *from++;
}
// 0-terminate
*to = 0;
// return start of copy, change to return to if end should be returned
return res;
}
There are at least two problems with your code.
res - sCopy makes no sense because they are pointing at different objects.
You haven't null-terminated the destination string.
#include <string.h>
char *getSub(const char *orig, int start, int count, char *res){
int i,j,len = strlen(orig), limit = start + count;
if(res == NULL) return NULL;
if(start >= len || start < 0 || orig == NULL){
*res = '\0';
return res;
}
for(j=0,i=start;i<len && i < limit;++i){
res[j++]=orig[i];
}
res[j]='\0';
return res;
}
How do you use a string array as a parameter in C? If I were to write a function with signature:
Guess i didnt explain myself very well... I'll post the code that i'm trying to get to work.
int format_parameters(char* str) {
char local_str[201] = "";
int i = 0;
int j = 0;
int flip = 0;
while(str[i]) {
if((str[i] == '"') && (flip == 0)) flip = 1;//Sentence allowed
else if((str[i] == '"') && (flip == 1)) flip = 0;//Sentence not allowed
if(flip == 1) append_char(local_str, str[i]);
//check if space
else if(flip == 0) {
int c = str[i];
if(!isspace(c)) append_char(local_str, str[i]);
else {
if((strlen(local_str) > 0) && (j < 4)) {
//local-str copied to param[j] here
//printf("j = %d %s\n",j,local_str);
local_str[0] = '\0';
j++;
}
}
}
i++;
}
//Add \0 to param
return flip;
}//end format_parameters
void append_char(char* str, char c) {
int len = strlen(str);
str[len] = c;
str[len+1] = '\0';
}//end append_char
int main() {
char str[200];
//str filled with stuff...
int x = format_parameters(str);
}
There should be a second (and third?) parameter in format_parameterssignature, a char* param[5] which should be readable from main.
Does this work?
#include <string.h>
#include <assert.h>
#include <stdio.h>
int format_parameters(char *str, char *param[], size_t nparam)
{
char **next = param;
char **end = param + nparam;
char *data = str;
assert(str != 0 && param != 0 && nparam != 0);
while (next < end && *data != '\0')
{
*next++ = data;
data = strchr(data, ' '); // Choose your own splitting criterion
if (data == 0)
break;
*data++ = '\0';
}
return(next - param);
}
int main(void)
{
char str[] = "a b c d";
char *param[5];
int nvals = format_parameters(str, param, 5);
int i;
for (i = 0; i < nvals; i++)
printf("Param %d: <<%s>>\n", i+1, param[i]);
return 0;
}
The return value is the number of parameters found. If you pass an empty string, that would be 0. Beware leading, trailing and repeated blanks; the code works - but maybe not as you want it to.
This is entirely about memory allocation.
If you allocate static memory for param before the function is called, the memory will exist in that scope.
Otherwise take a look at dynamic allocation, it will exist until you tell it to go away.
You have to create the char* param[] array outside the function and just pass it as a parameter:
int paramCount = countParameters(str); // you have to create this function
char* param[] = malloc(paramCount * sizeof(char*));
format_parameters(str, param);
and inside the function:
int format_parameters(char* str, char* param[])
{
int currentParamIndex = 0;
..........
//TODO: check if currentParamIndex < paramCount
char* currentParam = str + currentParamStart; // currentParamStart is the start index of the parameter in the str containing all parameters
param[currentParamIndex] = currentParam;
currentParamIndex++;
.............
}
And in order to write safe code you have to pass also the paramCount as a parameter to format_parameters so the function will not access an element out of the bounds of the array.
Or maybe you should just use getopt?
As Jonatahan pointed out, you need more parameters:
int format_parameters(char* strInput, char* paramOutput[], size_t cbMaxParams );
// return value is actual number of parameter strings in paramOutput
paramOutput is an array of pointers. So the caller has to provide an array of pointers and the called function has to allocate memory for the strings and set the pointers in the array:
// main:
#define SIZE 20
char * params[SIZE];
int result = format_parameters( szInput, params, SIZE );
// after use go through params and free all pointers
// function:
int format_parameters(char* strInput, char* paramOutput[], size_t cbMaxParams )
{
// ...
for( size_t i=0; (i<cbMaxParams) && (!noMoreParams); i++ )
{
// ...
paramOutput[i] = (char *)malloc( xxxx );
// ...
}
// ...
}