i have this reverse word function. it simply adding word to new string in reverse order . the problem with this code is it not print the last word ( first word before reverse ) . i dont know why . please help fix my code
char str[100], revstr[100];
int i, j, index, len, startIndex, endIndex;
printf("\n Please Enter any String : ");
gets(str);
len = strlen(str);
index = 0;
endIndex = len - 1;
printf("\n ***** Given String in Reverse Order ***** \n");
for(i = len - 1; i > 0; i--)
{
if(str[i] == ' ')
{
startIndex = i + 1;
for(j = startIndex; j <= endIndex; j++)
{
revstr[index] = str[j];
index++;
}
revstr[index++] = ' ';
endIndex = i - 1;
}
}
printf("\n Given String in Reverse Order = %s", revstr);
return 0;
Here's a working code:
char str[100], revstr[100];
int i, j, index, len, startIndex, endIndex;
printf("\n Please Enter any String : ");
gets(str);
len = strlen(str);
index = 0;
endIndex = len - 1;
printf("\n ***** Given String in Reverse Order ***** \n");
for(i = len - 1; i >= -1; i--)
{
if(i == -1){
startIndex = i + 1;
for(j = startIndex; j <= endIndex; j++)
{
revstr[index] = str[j];
index++;
}
revstr[index++] = ' ';
endIndex = i - 1;
break;
}
if(str[i] == ' ')
{
startIndex = i + 1;
for(j = startIndex; j <= endIndex; j++)
{
revstr[index] = str[j];
index++;
}
revstr[index++] = ' ';
endIndex = i - 1;
}
}
printf("\n Given String in Reverse Order = %s", revstr);
The problem was that you were only including a word if there was a space before it. There will be no space before the first word therefore I made the code go before the first letter and always include the first word forcefully. This code can be broken if there are more spaces than needed in the text.
Related
For some reason when I change the copy of string s that is (string c), string s also gets changed and for loop j keeps on looping until it reached position zero in string alphabet which is 'a'.
int key = atoi(keys);
string s = get_string("plaintext: ");
string c = s;
int length = strlen(s);
string alphabet = "abcdefghijklmnopqrstuvwxyz";
string ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
int position = 0;
for (int i = 0; i < length; i++)
{
printf("Before Conversion: %c\n",s[i]);
for (int j = 0; j < 26; j++)
{
if(s[i] == alphabet[j])
{
position = (j + key) % 26;
c[i] = alphabet[position];
printf("Ascii: %i + Key: %i.. Position = %i\n",j,key,position);
position = 0;
printf("Not Converted: %c\n",s[i]);
printf("After conversion: %c\n\n",c[i]);
}
if(s[i] == ALPHABET[j])
{
position = (j + key) % 26;
c[i] = ALPHABET[position];
printf("Ascii: %i + Key: %i.. Position = %i\n",j,key,position);
position = 0;
printf("After conversion: %c\n\n",c[i]);
}
}
}
printf("ciphertext: %s\n",c);
Change:
string s = get_string("plaintext: ");
string c = s;
to
string s = get_string("plaintext: ");
string c = malloc(strlen(s) + 1);
strcpy(c, s);
That will make it work. The biggest problem here is that the course CS50 is using
typedef char* string;
to hide the complexity of pointers for new students, but in reality it just causes confusion. In order to fully understand why the above solution works, you need to understand pointers and dynamic allocation.
The purpose of my code is to tell the user what is the most common letter and the second most common letter in the string and replace them with each other, but my code doesn't work properly. It finds the most common letters but doesn't replace them or print them properly. What is the problem?
Example input:
i love this game i do do
Output (this line is printed):
Most common: i, 2nd most common: o
After that I expected this output, which is not printed:
o live thos game o di o di
Code:
int main()
{
char array[MAX_LEN] = {0};
int i = 0, j = 0;
int max = 0, secondMAX = 0, count = 0, save = 0;
char saveForNow = ' ', highest = ' ', secondHighest = ' ';
printf("Enter a string:\n");
fgets(array, MAX_LEN, stdin);
array[strcspn(array, "\n")] = 0;
for(i = 0; i < strlen(array); ++i)
{
for(j = 0; j < strlen(array); j++)
{
if(array[i] != ' ')
{
if(array[i] == array[j])
{
count++;
saveForNow = array[i];
}
}
}
if(count > max)
{
max = count;
highest = saveForNow;
}
else if((count < max) && (count > secondMAX))
{
secondMAX = count;
secondHighest = saveForNow;
}
count = 0;
}
//*I think the proplem is here
for(i = 0; i < strlen(array); i++)
{
if(array[i] == highest)
{
array[i] = secondHighest;
}
else if(array[i] == secondHighest);
{
array[i] = highest;
}
}
printf("Most common: %c, ", highest);
printf("2nd most common: %c\n", secondHighest);
printf("Swapped:\n");
printf("%s", array);
//*I think the proplem is here
}
As mentioned in the heading, I am having problems while extracting a word from a sentence. It is giving a "break-point error". Can anyone help me understand how am I messing up, as I am just a beginner.
#include<stdio.h>
#include<stdlib.h>
#include <string.h>
int main()
{
char* word_array[50], input[51], word[51], temp[51];
short i , j, k, flag;
i = j = k = flag = 0;
printf("Enter the sentence (max 50 characters):\n");
gets_s(input, 50);
printf("\nEnter the word to be searched : ");
gets_s(word, 50);
while (input[i] != NULL)
{
while (input[i] == ' ')// skip the spaces
i = i + 1;
k = 0;
while (input[i] != ' ' || input[i] != NULL)//extract the words
{
word_array[j] = (char*)malloc(50 * sizeof(char));
*(*(word_array + j) + k) = input[i];
k = k + 1;
i = i + 1;
}
*(*(word_array + j) + k) = '\0';
printf("\nProcessed word : %s", word_array[j]);//just for debugging
j = j + 1;
}
for (i = 0; i <= j; i++)
if (strcmp(word_array[i], word) == 0)
flag = flag + 1;
printf("\nThe word occurred in the sentence %hu times.\n\n", flag);
system("pause");
return 0;
}
I was asked to write a code that returns the maximum number of anagrams (word made by transposing the letters of another word like the word “secure” is an anagram of “rescue.”).
This is what I did:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char text[1000], angrm1[1000], angrm2[1000];
int i, j, k, count = 1, flag = 0, temp, size = 0, q = 1, counter = 0, max = 0;
char**point;
printf_s("Please enter the sentence, and then press Enter:\n");
gets(text);
strcpy_s(angrm1, 1000, text);
j = 1; i = 0;
for (i = 0; i < strlen(angrm1); i = k + 2) {
for (k = i; angrm1[k + 1] != ' '&&angrm1[k + 1]!=0; k++) {
if (angrm1[i] > angrm1[i + 1]) {
temp = angrm1[k];
angrm1[k] = angrm1[k + 1];
angrm1[k + 1] = temp;
}
}
Blockquote Arrange the words
size = strlen(angrm1);
}
for (i = 0; angrm1[i] != 0; i++)
{
if (angrm1[i] == ' ')
{
angrm1[i] = 0;
count++;
}
}
point = (char*)malloc(sizeof(char)*count);
for (i = 0; i < (size - 1); i++)
{
if (angrm1[i] == 0)
{
point[q] = &point[i + 1];
q++;
}
}
for (i = 0; i < counter; i++)
{
for (q = i + 1; q < counter; q++)
{
if (point[q] == 0 || point[i] == 0)
continue;
if (!strcmp(point[q], point[i]))
{
counter++;
point[q] = 0;
}
}
if (max < counter)
max = counter;
counter = 0;
}
printf_s("The maximum (not yet) number of anagram words in this sentence is %d", &max);
}
The first part works (in my opinion).
But when I check with the debugger, it skips the loop and I get junk.
Can someone please help me by pointing to the problem, and explaining how to fix it?
I am looking for a way to change words in string.
I want to change word to another word which have more symbols than first word.
First I count the symbols of the word which i want to change with another one,
than I count how many this word is in the text which is in my program as a char array(string) and now i want to change this word with the word which has more symbols.
There is the code:
#include "stdafx.h"
#include <iostream>
using namespace std;
int main()
{
Start:
printf("This program can change word or symbols combanation in text\n\n");
char text[550] = "There was a grocery shop in a town. Plenty of mice lived in that grocery shop.\nFood was in plenty for them. hey also wasted the bread\nbiscuits and fruits of the shop.\nThe grocer got really worried. So, he thought I should buy a cat\nand let it stay at the grocery. Only then I can save my things.\nHe bought a nice, big fat cat and let him stay there. The cat had a nice\nime hunting the mice and killing them.\nThe mice could not move freely now. They were afraid that\n\n";
printf("%s\n", text);
int symbols_in_text = 0;
for (int f = 0;f < 550;f++)
{
if (text[f])
{
symbols_in_text++;
}
else
{
break;
}
}
printf("Text has %i symbols\n\n", symbols_in_text);
char b[15];
printf("Which word would you like to chenge in text above?\n(Maximum quantity of Symbols is 15)\nType word or some symbol(s)\n--->>");
cin >> b;
int word_symbols=0;
for (int a = 0;a < 15;a++)
{
if (b[a])
{
word_symbols++;
}
else
{
break;
}
}
printf("Word which you have entered has %i symbols\n", word_symbols);
int word_in_text = 0;
for (int i = 0; i < 550 - word_symbols; i++)
{
int found = 1;
for (int j = 0; j < word_symbols; j++)
{
if (b[j] != text[i + j])
{
found = 0;
break;
}
}
if (found && text[i + word_symbols]==' ')
{
word_in_text++;
}
}
printf("Founded %i %s as the word an is Not the some part of the other word\n", word_in_text,b);
if (word_in_text != 0)
{
char input_word[15];
printf("Enter the word which you want to insert for first entered word\nWarning:You have to enter the world which have %i symbols\n--->>", word_symbols);
cin >> input_word;
int in_word_symbols = 0;
for (int a = 0;a < 15;a++)
{
if (input_word[a])
{
in_word_symbols++;
}
else
{
break;
}
}
printf("The word which you have entered has %i symbols\n\n", in_word_symbols);
if (word_symbols == in_word_symbols)
{
for (int i = 0; i < 550 - word_symbols; i++)
{
int found = 1;
for (int j = 0; j < word_symbols; j++)
{
if (b[j] != text[i + j])
{
found = 0;
break;
}
}
if (found != 0 && text[i + word_symbols] == ' ')
{
for (int j = 0; j < in_word_symbols; j++)
{
text[i + j] = input_word[j];
}
}
}
printf("The result is--->>\n%s\n\n//////////////////////////////////////////END OF////////////////////////////////////////\n\n", text);
}
else if (in_word_symbols > word_symbols)
{
int text_char_index = 0;
step1:
for (int count = 0; count < 550 - word_symbols; count++)
{
text_char_index++;
int found = 1;
for (int j = 0; j < word_symbols; j++)
{
if (b[j] != text[count + j])
{
found = 0;
break;
}
if (found != 0 && text[count + word_symbols] == ' ')
{
count = text_char_index-1;
goto index_changing;
insert:
for (int c = 0; c < in_word_symbols; c++)
{
text[count + c] = input_word[c];
}
if (count > 500)
{
goto printing_result;
}
else
{
continue;
}
}
}
}
index_changing:
for (int l = 466; l > text_char_index + word_symbols; --l)
{
text[l] = text[l + 1];
}
goto insert;
printing_result:
printf("The result is--->>\n%s\n\n//////////////////////////////////////////END OF////////////////////////////////////////\n\n", text);
}
}
goto Start;
return 0;
}
If I enter the word was ( - it is word which i want to change in text) and than I enter the word detected (which has more symbols than first entered word - an it is the word which i want to insert in the text in return "was")
Console output is: There detected
And I can't use in this code method or something like that, I can only use if() and for loop.
Can anyone help me to understand how to do it?
First let's get the ground rules straight -- you are not allowed to use string functions/methods in this exercise, you have to write the code out explicity using primarily if statements and loops.
Rather than create a whole separate block of code to deal with the replacement word being larger than the original, just deal with it as part of the original replacement code -- as well as deal with the possibility that the replacement word is smaller. Both require shifting the original text one way or the other based on the difference between the original word and the replacement.
Here is a rework of your code that does the above and makes a number of style changes and bug fixes. It also makes it a pure 'C' program:
#include <stdio.h>
#include <stdbool.h>
int main()
{
printf("This program can change word or character combinations in text.\n\n");
char text[1024] =
"There was a grocery shop in a town. Plenty of mice lived in that grocery shop.\n"
"Food was in plenty for them. They also wasted the bread, biscuits and fruits\n"
"of the shop. The grocer got really worried. So, he thought I should buy a cat\n"
"and let it stay at the grocery. Only then I can save my things. He bought a\n"
"nice, big fat cat and let him stay there. The cat had a nice time hunting mice\n"
"and killing them. The mice could not move freely now. They were afraid that\n";
while (true) {
printf("%s\n", text);
int characters_in_text = 0;
for (int f = 0; text[f] != '\0'; f++) // not allowed to use strlen()
{
characters_in_text++;
}
printf("Text has %i characters.\n\n", characters_in_text);
char word[17];
printf("Which word would you like to change in text above?\n");
printf("(Maximum quantity of characters is %d)\n", (int) sizeof(word) - 2);
printf("Type word or some character(s)\n");
printf("--->> ");
(void) fgets(word, sizeof(word), stdin);
int a, word_characters = 0;
for (a = 0; word[a] != '\n'; a++)
{
word_characters++;
}
word[a] = '\0';
if (word_characters == 0)
{
break; // exit program
}
printf("Word which you have entered has %i characters.\n", word_characters);
int words_in_text = 0;
for (int i = 0; i < characters_in_text - word_characters; i++)
{
bool found = true;
for (int j = 0; j < word_characters; j++)
{
if (word[j] != text[i + j])
{
found = false;
break;
}
}
if (found)
{
char next_letter = text[i + word_characters];
if (next_letter == ' ' || next_letter == '.' || next_letter == ',' || next_letter == '\n' || next_letter == '\0')
{
words_in_text++;
}
}
}
printf("Found %i instance(s) of %s that are not part of another word.\n", words_in_text, word);
char replacement_word[17];
printf("Enter the word which you want to insert for first entered word.\n");
printf("(Maximum quantity of characters is %d)\n", (int) sizeof(replacement_word) - 2);
printf("Type word or some character(s)\n");
printf("--->> ");
(void) fgets(replacement_word, sizeof(replacement_word), stdin);
int replacement_word_characters = 0;
for (a = 0; replacement_word[a] != '\n'; a++)
{
replacement_word_characters++;
}
replacement_word[a] = '\0';
printf("The word which you have entered has %i characters.\n\n", replacement_word_characters);
int text_shift = replacement_word_characters - word_characters;
for (int i = 0; i < characters_in_text - word_characters; i++)
{
bool found = true;
for (int j = 0; j < word_characters; j++)
{
if (word[j] != text[i + j])
{
found = false;
break;
}
}
if (found)
{
char next_letter = text[i + word_characters];
if (next_letter == ' ' || next_letter == '.' || next_letter == ',' || next_letter == '\n' || next_letter == '\0')
{
if (text_shift > 0)
{
for (int k = characters_in_text; k > i + word_characters - 1; k--)
{
text[k + text_shift] = text[k];
}
}
else if (text_shift < 0)
{
for (int k = i + word_characters; k < characters_in_text + 1; k++)
{
text[k + text_shift] = text[k];
}
}
characters_in_text += text_shift;
for (int j = 0; j < replacement_word_characters; j++)
{
text[i + j] = replacement_word[j];
}
}
}
}
}
return 0;
}
You can see how complicated this code gets without library functions -- testing for the possible punctuation that follows a word would be a lot easier with functions like ispunct() in ctype.h
You also computed a number of useful values in your original code but failed to use them appropriately later on in the code.
Yet to do: you've a lot of error checks to add to this code -- what if the replacement makes the text larger than the array that contains it? You check that a word isn't contained in another by testing the character after the word but fail to test the character before the work, e.g. 'other' vs. 'another'.