Add letter if it's duplicate C [closed] - c

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I work on playfair cipher and I have one problem.
I need to add letter X in my string if the next letter it's duplicate.
Example before: HELLOWORLD
separe the string in 2 pairs (HE LL OW OR LD) and if it's duplicate add X. (I need to add X only if the pair is duplicate.
After: HE LX LO WO RL D -> HELXLOWORLD
My code:
for (j = 0, i = 0; i < len_text; i++, j++) {
if (i % 2 == 0) {
if (my_text[i] == my_text[i+1]) {
text_x[j] = my_text[i];
i++;
text_x[j+1] = 'X';
j++;
}
else {
text_x[j] = my_text[i];
}
}
else if (i % 2 != 0) {
text_x[j] = text[i];
}
}
My code don't works normally. Can you help me? Thank you.

If I'm understanding your problem correctly, you could do something like this. The way you're currently doing it looks like it could potentially go out of bounds with i. Make sure that text_x is a buffer big enough to hold these extra chars you're inserting (i.e. len_text + len_text / 2 at max).
for (j = 0, i = 0; i < len_text - 1; i+=2, j+=2) {
text_x[j] = my_text[i];
if (my_text[i] == my_text[i+1]) {
text_x[j+1] = 'X';
j++;
}
text_x[j+1] = my_text[i+1];
}

Related

remove duplicates in a string with a buffer [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 1 year ago.
Improve this question
I’m trying to remove duplicates in a string using a map. Running it through GDB I'm not able to figure out where the code is failing, though the logic to me seems right.
Could anyone please point out the mistake?
int main() {
char *str="I had my morning tea";
int len = strlen(str);
int dupArr[256] = {0};
//Build the map
int i=0;
for(i;i<256;i++)
dupArr[str[i]]++;
//If the count is 1 then print that value.
i=0;
for(i;i<256;i++) {
if(dupArr[str[i]] == 1) {
printf("%c\n",str[i]);
}
}
}
output
I h y o r i g t % c 4 # } ` 8 � F J
I get up to 't' ,which is correct but then i see magic chars.
Your string has length of len but you are traversing till 256 which is out of bound.
Use len when inserting into the hash.
int i=0;
for(i;i<LEN;i++)
dupArr[str[i]]++;
Also if your are checking the duplicates then it should be bigger than 1 since your are ++ the first encountered char
if(dupArr[str[i]] > 1)
In addition to Mark Ezberg's good answer, note that dupArr[str[i]]++; poses a problem when str[i] < 0.
Better to treat the characters as unsigned char:
int dupArr[UCHAR_MAX + 1] = {0};
....
dupArr[(unsigned char) str[i]]++;
Rolling this and other ideas together:
int main(void) {
char *str="I had my morning tea";
size_t dupArr[UCHAR_MAX + 1] = {0};
unsigned char *s = (unsigned char *) str;
while (*s) {
dupArr[*s]++;
s++;
}
for(unsigned i = 0; i <= UCHAR_MAX; i++) {
// A duplicate is when dupArr[i] is _more_ than 1.
if(dupArr[i] > 1) {
printf("%c\n",str[i]);
}
}
}

If statement only prints first line [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I have a problem my if statement only seems to be able to do the first line of the code
sentence is HELLO
it should print HOBELLOOB, but all it does is repaces all vowels with O
for (int i = 0; i <= sizeof(sentence); i++)
{
if (sentence[i]=='A' || sentence[i]=='E' || sentence[i]=='I' || sentence[i]=='O' || sentence[i]=='U' || sentence[i]=='Y')
{
temp[i] = ob[0];
temp[i+1] = ob[1];
temp[i+2] = sentence[i];
}
else
{
temp[i]=sentence[i];
}
}
printf("\n%s",temp);
You need two counter for the indexes. Otherwise the contents in temp will be overwritten on the next loop.
int j=0;
for (int i = 0; i <= sizeof(sentence); i++)
{
if (sentence[i]=='A' || sentence[i]=='E' || sentence[i]=='I' || sentence[i]=='O' || sentence[i]=='U' || sentence[i]=='Y')
{
temp[j++] = ob[0];
temp[j++] = ob[1];
temp[j++] = sentence[i];
}
else
{
temp[j++]=sentence[i];
}
}
printf("\n%s",temp);

Search a Word in a 2D characters array [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 2 years ago.
Improve this question
Given a 2D array of size 100 x100 of characters and a word (1D character array), find the occurrences of given word in 2D array (search only left to right horizontally).
char data[100][100] =
{
"ACFRTBOOK",
"BOPNBOOKQUIZGEEK",
"IDEQAPRACTICE"
};
char Word[4] = "BOOK";
Output:
pattern found at 0, 5
pattern found at 1, 4
This might help:
int k = 0, n = 0;
char word[] = "BOOK";
for (int i = 0; i < size - 1; i++)
{
for (int j = 0; data[i][j] != '\0'; j++)
{
n = j;
while (data[i][n] == word[k] && word[k] != '\0')
{
n++;
k++;
if (word[k] == '\0')
{
printf("Found at %i, %i", i, j)
}
}
k = 0;
}
}
Firstly, you cannot initialize a char array as a String like you did with char Word[4] = "BOOK"; this would have to be char word[4] = {'b','o','o','k'}; The same applies to your 2D array named data.
So here is how you would do such a thing. I added in //comments to explain, but the general idea is to turn the arrays into Strings as these are easier to use when locating another set of characters.
Please let me know if you need further assistance. I hope this helps.
public void findWord(char[] word; char[][] data){
String w = new String(word); //it is much easier to work with strings for a searching problem like this
int x=0,y=0;
for(char[] i:data){//iterates through each row of data
String d = new String(i);
while(d.contains(w)){
x+=i.indexOf(w);//locates the pattern
System.out.println("pattern found at " + x + ", " + y);
x++;
d=s.substring(x);//this removes the previous occurance of the patern so that the index of can find a new repeat in the same row
}
y++;
x=0;//reset for next array
}
}

What is wrong with my C code? C code error [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 4 years ago.
Improve this question
I am very new in C programming. can someone please tell me what is wrong with this code I am trying to run this code.
int main(void)
{ char source[10];
char *dest; size_t i;
strcpy(source, "0123456789");
dest = malloc(strlen(source));
for (i = 1; i <= 11; i++) {
dest[i] = source [i];
}
dest[i] = '\0';
printf("dest = %s", dest); return 0; 13. }
Thanks a lot in advance
Firstly, allocate enough memory for source to hold 0123456789. And this dest[i] = '\0'; causes undefined behavior as here you are trying to access(dest[10]) something which you didn't allocate. So allocate enough memory for dest to store '\0' at the end. For e.g
dest = malloc(strlen(source) + 1);/* +1 is for \0 char as strlen(source) doesn't include \0 */
Secondly, don't think that source[0] is integer zero 0 its a character zero i.e '0'(ascii - 48). So you no need to start rotating form i=1, rotate from i=0 and upto '\0'.
This
for (i = 1; i <= 11; i++) { /* array index starts from 0 */
dest[i] = source [i];
}
Should be
for (i = 0;source[i]; i++) { /* when \0 encounters, loop terminates */
dest[i] = source [i];
}

Markov chain (c code on windows fails) [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am trying to get work this piece of code from Kernighan's book The practice of programming on my workstation(windows 7 + vs2015 community edition)
I get a strange error.
void generate(int nwords) {
State *sp;
Suffix *suf;
char *prefix[NPREF];
char *w = NULL;
int i, nmatch;
for (i = 0; i < NPREF; i++)
prefix[i] = NONWORD;
for (i = 0; i < nwords; i++) {
sp = lookup(prefix, 0);
nmatch = 0;
for (suf = sp->suf; suf != NULL; suf = suf->next) {
if (rand() % ++nmatch == 0) {
w = suf->word;
}
if (nmatch == 0)
printf("internal error: no suffix %d %s", i, prefix[0]);
if (strcmp(w, NONWORD) == 0)
break;
printf("%s ", w);
memmove(prefix, prefix + 1, (NPREF - 1) * sizeof(prefix[0]));
prefix[NPREF - 1] = w;
}
}
}
for (suf = sp->suf; suf != NULL; suf = suf->next)
Unhandled exception at 0x000000013F5C1564 in CompareCandCsharp.exe:
0xC0000005: Access violation reading location 0x0000000000000010.
My implementation is similar to described here - Working with arrays and reading text files
Seems algorithm works - but on my computer it fails. I can't find mindfull porpose for this. Could you please give your suggestions.
After several hours of debugging I have found a small mistake in expected method.
for (suf = sp->suf; suf != NULL; suf = suf->next) {
if (rand() % ++nmatch == 0) {
w = suf->word;
}
There is no bracket after if in this line so all other code in method try set w many times and of course it leads to memory errors =). Thanx for minusing my question before reading it =))

Resources