comparing characters of two strings in c [closed] - c

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 days ago.
Improve this question
I am doing the CS50 problem set 2 wordle and I am stuck with the TODO: 5.
I have two words of the same length: the "guess" from the user and the "word", which they try to guess.
I am supposed to check if some letters of the "guess" are in the actual "word" and if they are in the correct spot.
I tried to make an array of characters which is basically the word itself in order to compare each letter of the guess with the actual word but everything I find is just looks like that:
char myString[] = "This is some text";
But the problem is that the words are saved as strings and I cannot do this:
char myString[] = guess;
It would be great if someone could help me with that problem.
char myString[] = "%s", guess;
but that does not work!

Related

assign zero to item in char array [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 months ago.
Improve this question
I have some materials showing this code. what does code means by making these assignments?
char inputfilename[128];
inputfilename[0] = 0;
char *argv[128];
*argv[1] = 0;
In C, character arrays are terminated by a null character (value 0). In both cases in your example, the code initializes the strings to "empty" (with a terminator in the first element). This would prove useful in any subsequent string operations (strcat, strcpy, etc.).

How can I delete the first zero(s) in a string? (Without using atoi) [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I was making a script that is calculating the additions between two natural numbers which decimal lengths should be smaller or same with 10000, and printing a result of the sum.
Of course, there ain't any variable type that can hold a integer which length is 10000 in C.
So, I made the program by utilizing the simple additions' calculating logic that all we learn in a school when we were young. And also, I just should use strings to get those gigantic numbers.
But some results were starting with zero. I knew why did the zero appeared there, but I did prefer to have a result that is like "1234", not "01234". By the way, all other stuffs were perfect.
I needed a function that gets input as string, and erases a single zero starts with a string if it exists.
And could you make it instead of me, please? You should probably consider that the strings we will deal with can have such a length that is smaller or same with 10000.
Maybe this:
char * f( char * str )
{
while ( *str == '0' && str[1] )
str++; // skips all zero-s when it is not last character in string
return str;
}

How to split a string into 3 parts in C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
the question is the following: "How do I split a string into 3 parts in C?"
The string is something similar to the following: "Roberta$$$Anna$$$$$$Massimo$$$"
I need to split it exactly after 10 characters (Roberta$$$, Anna$$$$$$, Massimo$$$), and please notice that they aren't separated by a spacebar, so I think I cannot use the strtok function or the library string.h to split them.
char source[] = "Roberta$$$Anna$$$$$$Massimo$$$";
char part1[11];
char part2[11];
char part3[11];
memmove(part1, &source[ 0], 10);
part1[10] = '\0';
memmove(part2, &source[10], 10);
part2[10] = '\0';
memmove(part3, &source[20], 10);
part3[10] = '\0';
You could use strncpy() or memcpy() instead of memmove().

Change value pointed to by strstr(); C [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I'm trying to make a program to find and replace some text in a string at the moment I'm trying to change "hello how are you" to "hello bow are you" as a test.
So firstly I find the "how" by using char *substring = strstr(mystring, newstr);
which returns a pointer to "(this position)how are you" now I have no idea how to change the next 3 letters. I can strlen(newstr) for the length of the string I'm replacing "how" with but I can't find a way to change mystring starting from the pointer newstr.
Change the first character by subscripting the substring.
substring[0] = 'b';
If you want to replace multiple characters, try a loop, or use memcpy. Don't use strcpy: you don't want the NUL terminator to be copied.
memcpy(substring, "how", 3);
*substring = 'b'; as posted by user EOF my solution was as so
for (int x = 0; x < strlen(newstring); x++){
*substring++ = newstring[x];
}

How to extract specified character string from string [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
char string[]="DGS021J0W0S1000.0S20000S3000.0S4000.0S50.00S60.00F";
how to get S[1-5]
thanks!
Use strncpy() standard function
char S[6] = {0};
strncpy(S, string+1 , 5);
If you want to copy from the beginning of the string to the 5th charachter, then your question should be
how to get S[0-4]
and not S[1-5] because array index in C start from 0 and not from 1. and the solution for this case will be
char S[6] = {0};
strncpy(S, string , 5);
I think you are looking for substring methods.
You can do it in two for loops in C.

Resources