If statement only prints first line [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 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);

Related

segmentation fault in C programme [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
#include<stdio.h>
int encryption_check(char* s , char* t)
{
printf("%s",s);
int i=0 , diff = t[0] - s[0];
while(s[i]!='\0' && t[i]!='\0')
{
if(t[i]-s[i]<=0)
{
return -1;
}
else if(t[i]-s[i]!=diff)
{
return -1;
}
}
return diff;
}
int main()
{
int d = 0;
char s[]= "abc";
char t[]= "def"; // s=plain text and t=cipher text
d = encryption_check(s,t);
printf("%d",d);
return 0;
}
encryption_check() returns the difference between ciphered and plain text.
This is giving segmentation fault for some reason.
Also I would like to know how to pass strings of random (not fixed) length to encryption_check().
If not getting into details of what your encryption_check should actually do and keep it short, your while loop can't terminate as your i never changes after being set to 0.
On the first run, s[0] = 'a' and t[0] = 'd' so t[0] - s[0] = 3 and so the conditions in while will be true. On the consequent runs, as you don't iterate i, the loop won't stop.
To fix it, just add i++ as below:
while(s[i]!='\0' && t[i]!='\0') {
if(t[i]-s[i]<=0) {
return -1;
} else if(t[i]-s[i]!=diff) {
return -1;
}
i++;
}

Add letter if it's duplicate C [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 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];
}

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 =))

Intro to Programming in C - [Final Exam Sample] Palindrome Function [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 7 years ago.
Improve this question
I need help! I can't do it. I tried in many ways but non has worked.
At least can someone help me with the LOGIC. Below I will paste my code.
Develop a program with a function that receives an array of chars. The function has to modify the original and copy it exactly to another one but putting two asterisks (**) before each word that is a PALINDROME, a palindrome is a word which reads the same backward or forward (e.g ANNA, KAYAK).
For example,
my best friend anna has a red kayak
my best friend **anna has a red **kayak.
#include <stdio.h>
#include <stdlib.h>
void Cargar(char texto1[])
{
int i=0;
char letra;
do
{
letra=getche();
if(letra != '.')
{
texto1[i]=letra;
i++;
}
}
while (letra != '.' && i<99);
texto1[i]='\0';
}
int main()
{
char texto1[100], texto2[100];
printf("Ingrese texto: ");
Cargar(texto1);
int pos=0, esp_anterior=0, aux1=0, aux2=0, aux3=0, bandera=0;
int i, j, k;
for(pos = 0 ; texto1[pos] != '\0' ; pos++)
{
if( texto1[pos] == ' ')
{
if(bandera==0)
{
if( hay_palindromo(texto1, pos, esp_anterior) == 1)
{
texto2[0]='*';
texto2[1]='*';
for(i=2, j=0; j<pos ; j++ , i++)
{
texto2[i]=texto1[j];
}
aux1=i;
aux2=j;
}
else
{
for(i=0; i<pos; i++)
{
texto2[i]=texto1[i];
}
aux3=i;
}
bandera = 1;
esp_anterior = pos;
}
else
{
if(bandera == 1)
{
if( hay_palindromo(texto1, pos, esp_anterior) == 1)
{
texto2[aux1]='*';
texto2[aux1+1]='*';
for(i=aux1+2, j=aux2; j<pos ; j++ , i++)
{
texto2[i]=texto1[j];
}
aux1=i;
aux2=j;
}
else
{
for(i=aux3; i<pos; i++)
{
texto2[i]=texto1[i];
}
aux3=i;
}
esp_anterior=pos;
}
}
}
}
printf("\n%s", texto2);
return 0;
}
int hay_palindromo(char texto1[], int pos, int esp_anterior)
{
int i, j, bandera=0;
for(i=esp_anterior, j=pos; i<pos; i++, j--)
{
if(texto1[i] == texto1[j])
{
bandera=1;
}
else
{
bandera=0;
break;
}
}
if(bandera==1)
{
return 1;
}
else
{
return 0;
}
}
I would suggest a logic like that:
Take string, separate it by a delimiter - " " (space) in your case with strtok.
Then, send each delimited word to a function that determines whether the string is a palindrome or not.
If the string is palindrome, allocate enough space for the string + "", and copy "" + string into the allocated space, save the current position of your 'cursor' on that array.
Pseudo Example:
"Anna notpali"
char *new_str;
int cursor = 0;
is_palindrome("Anna")
Yes -> new_str = malloc(strlen("Anna") + strlen("**") + 1)
strcpy(&(new_str[cursor]),"**Anna");
cursor += strlen("**Anna");
is_palindrom("notpali")
No -> new_str = realloc(new_str,strlen(" notpali") + 1)
strcpy(&(new_str[cursor])," notpali");
cursor += strlen(" notpali");
// After going through all the words
new_str[cursor] = '\0'
etc, there might be some corner cases to take care of, but this is a basic logic I suggest to approach it.

Should I use 2 Prototypes, or just 1? [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 8 years ago.
Improve this question
I'm trying to create a scoring system for my game. Here is my code, and I'm using it as a prototype. However, every time I try to call it in the main function, my program just crashes. I'm using 2 of these prototypes (score1 and score2) because I need 2 different scores. Should I just use one? It's a dice game that shoots 6 dice, and I want to assign each number a value.
int score1( void) {
int roll1[6];
int sum = 0;
int i;
sum = roll1[0]+ roll1[1]+ roll1[2]+ roll1[3]+ roll1[4]+ roll1[5];
if( roll1[0]==1) {
roll1[0]=100;
}else if (roll1[0]== 5){
roll1[0]= 50;
}else if( (roll1[0]!=1) || ( roll1[0]!= 5) ){
roll1[0]=0;
}
if( roll1[1]==1) {
roll1[1]=100;
}else if (roll1[1]== 5){
roll1[1] = 50;
}else if( (roll1[1]!=1) || ( roll1[1]!= 5) ){
roll1[1] = 0;
}
if( roll1[2]==1) {
roll1[2] = 100;
}else if (roll1[2]== 5){
roll1[2] = 50;
}else if( (roll1[2]!=1) || ( roll1[2]!= 5) ){
roll1[2] = 0;
} if( roll1[3]==1) {
roll1[3] =100;
}else if (roll1[3]== 5){
roll1[3] = 50;
}else if( (roll1[3]!=1) || ( roll1[3]!= 5) ){
roll1[3] = 0;
}
if( roll1[4]==1) {
roll1[4] = 100;
}else if (roll1[4]== 5){
roll1[4] = 50;
}else if( (roll1[4]!=1) || ( roll1[4]!= 5) ){
roll1[4] = 0;
} if( roll1[5]==1) {
roll1[5] = 100;
}else if (roll1[5]== 5){
roll1[5] = 50;
}else if( (roll1[5]!=1) || ( roll1[5]!= 5) ){
roll1[5] = 0;
}
if((roll1[i]==roll1[i+1]) && (roll1[i+1]==roll1[i+2])) {
sum = (100*i)+roll1[i+3]+roll1[i+4]+roll1[i+5];
}
if((roll1[i]==roll1[i+1])&&(roll1[i+1]==roll1[i+2])&&(roll1[i+2]== roll1[i+3])) {
sum = (2*(100*i))+roll1[i+4]+roll1[i+5];
}
if((roll1[i]==roll1[i+1])&&(roll1[i+1]==roll1[i+2])&&(roll1[i+2]== roll1[i+3])&&(roll1[i+3]==roll1[i+4])) {
sum = (4*(100*i))+roll1[i+5];
}
if((roll1[i]==roll1[i+1])&& (roll1[i+1]==roll1[i+2])&&(roll1[i+2]== roll1[i+3])&& (roll1[i+3]==roll1[i+4])&& (roll1[i+4]== roll1[i+5])) {
sum = 8*(100*i);
}
if((roll1[0]==1)&& (roll1[1]==2) && (roll1[2]==3)&& (roll1[3]==4)&& (roll1[4]== 5) && (roll1[5]==6)){
sum = 1500;
}
return sum;
}
Bam! Reading from uninitialized values will KILL you every time:
int roll1[6];
...
sum = roll1[0]+ roll1[1]+ roll1[2]+ roll1[3]+ roll1[4]+ roll1[5];
...
What are the values of roll1[0]+... at this point? The compiler doesn't know and flies off into Undefined Behavior - a crash is an option. Until the values for each of the elements in roll1[6] have been set, trying to read from one without a value is bad (you see the result).
Two things. 1. Always compile with warnings. (-Wall -Wextra) at minimum. 2. Always initialize your variables, to zero if you have no other initial value. The following will prevent the undefined behavior.
int roll1[6] = { 0 };

Resources