I've been running this for a couple of hours, and continue to get the wrong output, and can't seem to find out why. It seems as if everything should work, but I consistently get a weird character for the first time I implement this, and the tokens are not getting moved around in the way that they should be. This is just practice code, to be implemented in assembly language.
char get_line(void){
//Char array, buf space 80, int array, hold the numerical value,
char arr[80];
int int_arr[80];
char arr_print[80];
//Two points to compare whether the value in the given array changed.
int compare;
int compare_2;
//Array points, indexes and size counter.
int count = -1;
int i = 0;
int j = 0;
int k;
gets(arr);//Unsafe version of code, but for this implementation negligible.
while( (arr[i] != NULL) && (i < 80) && arr[i] != '\n'){
//Runs through and sets the value based on specs, #'s =1, alpha =2, ...
//For the comparison with the below code.
if(isalpha(arr[i])){
int_arr[i] = 2;// printf("%c: 2", arr[i]);
compare = 2;
}else if(isdigit(arr[i])){
int_arr[i] = 1;// printf("%d: 1", arr[i]);
compare = 1;
}else if(arr[i] == '$'){
int_arr[i] = 5;// printf("%c: 5", arr[i]);
compare = 5;
}else if(arr[i] == '#'){
int_arr[i] = 6;// printf("%c: 6", arr[i]);
compare = 6;
}else if(arr[i] == '(' || arr[i] == ')' || arr[i] == ',' ||
arr[i] == '.' || arr[i] == ':'){
int_arr[i] = 4;// printf("%c: 4", arr[i]);
compare = 4;
}else if(arr[i] == '*' || arr[i] == '+' || arr[i] == '-' ||
arr[i] == '/'){
int_arr[i] = 3;//printf("%c: 3", arr[i]);
compare = 3;
}else if(isspace(arr[i]))
int_arr[i] = 5;//Ignore the spaces in this implementation.
/*
Runs the comparison point to assure that the
tokens are matched up and grouped as needed.
*/
if(compare_2 == 0 || (compare != compare_2)){
if(compare_2 != 0){
for(k=0; k<=j ;k++)
printf("%c", arr_print[k]);
j=0;
}
printf("\t\t%d \n", compare_2);
compare_2 = compare;
}else if( isspace(arr[i]) == 0 ){
arr_print[j] = arr[i];
// printf("\t\t\t\t\t%c | %d\n", arr_print[j],j);
j++;
}
i++;
count++;
}
printf("\n\n");
//Code for previous implementation in C
for(i=0; i<80 && arr[i] != NULL; i++)
printf("%c", arr[i]);
printf("\n");
for(i=0; i< count+1; i++)
printf("%d", int_arr[i]);
printf("\n");
if(i == 0 || count == -1) return '#';
return arr[count];
}
I think this is what you needed
Try this code
char get_line(void)
{
//Char array, buf space 80, int array, hold the numerical value,
char arr[80];
int int_arr[80];
char arr_print[80];
//Two points to compare whether the value in the given array changed.
int compare=0;
int compare_2=0;
//Array points, indexes and size counter.
int count = -1;
int i = 0;
int j = 0;
int k = 0;
int l = 0; // I add an int l for
gets(arr);//Unsafe version of code, but for this implementation negligible.
//while( (arr[i] != NULL) && (i < 80) && arr[i] != '\n'){
while( i < 80 && arr[i] != '\0')
{
//Runs through and sets the value based on specs, #'s =1, alpha =2, ...
//For the comparison with the below code.
if(isalpha(arr[i]))
{
int_arr[i] = 2;// printf("%c: 2", arr[i]);
compare = 2;
}
else if(isdigit(arr[i]))
{
int_arr[i] = 1;// printf("%d: 1", arr[i]);
compare = 1;
}
else if(arr[i] == '$')
{
int_arr[i] = 5;// printf("%c: 5", arr[i]);
compare = 5;
}
else if(arr[i] == '#')
{
int_arr[i] = 6;// printf("%c: 6", arr[i]);
compare = 6;
}
else if(arr[i] == '(' || arr[i] == ')' || arr[i] == ',' || arr[i] == '.' || arr[i] == ':')
{
int_arr[i] = 4;// printf("%c: 4", arr[i]);
compare = 4;
}
else if(arr[i] == '*' || arr[i] == '+' || arr[i] == '-' || arr[i] == '/')
{
int_arr[i] = 3;//printf("%c: 3", arr[i]);
compare = 3;
}
else if(isspace(arr[i]))
int_arr[i] = 5;//Ignore the spaces in this implementation.
/*
Runs the comparison point to assure that the
tokens are matched up and grouped as needed.
*/
if(compare_2 == 0 || (compare != compare_2))
{
if(compare_2 != 0)
{
for(k=0; k<=j ;k++)
printf("%c", arr[l+k]); // arr_print replaced by arr
j=0;
l+=k; // int l to go through array arr
}
printf("\t\t%d \n", compare_2);
compare_2 = compare;
}
else if( isspace(arr[i]) == 0 )
{
arr_print[j] = arr[i];
//printf("\t\t\t\t\t%c | %d\n", arr_print[j],j);
j++;
}
i++;
count++;
}
printf("%c", arr[count]); // Repeated code to print the last element
printf("\t\t%d \n", compare_2);
compare_2 = compare;
printf("\n\n");
//Code for previous implementation in C
for(i=0; i<80 && arr[i] != NULL; i++)
printf("%c", arr[i]);
printf("\n");
for(i=0; i< count+1; i++)
printf("%d", int_arr[i]);
printf("\n");
if(i == 0 || count == -1) return '#';
return arr[count];
}
Related
the push to vaccinate children has taken on fresh urgency amid concerns that the new omicron variant of the virus first identified in southern africa and hong kong in late november will spread quickly in the united states causing a surge in infections already back on the rise from the easily transmitted delta variant given the pervasiveness of delta and prospects of new variants spreading in the united states having as much immunity in the population as possible is critical said dr amesh adalja senior scholar at the johns hopkins center for health security
This is my assignment:
replace multiple spaces to one space between words and delete unnecessary spaces at the beginning and the end.
count the words
print edited string
dont use a new string, just edit.
I can't find problem. It should count the words but it can not do. Help me, please.
//Counting words program C
#include <stdio.h>
#define N 5000
int main(void) {
FILE *fp;
char text[N];
int k, d, leng, spacecount = 0;
int m, j, z, i, p, n;
if ((fp = fopen("soru.txt", "r")) == NULL) {
printf("Dosya acma hatasi!");
return 1;
}
fgets(text, N - 1, fp);
while (k < N && text[k] != '\0') {
leng++;
k++;
}
z = leng;
for (i = 0; i < leng; i++) {
if (i = 0 && text[i] == ' ') {
z--;
for (m = 0; m < leng; m++) {
text[m] = text[m + 1];
}
i--;
text[z] == '\0';
} else
if (text[i] ==' ' && text[i + 1] == ' ') {
z--;
for (j = i; j < leng; j++) {
text[j + 1] = text[j + 2];
}
i--;
text[z] == '\0';
} else
if (text[i] == ' ' && text[i + 1] == '\0') {
z--;
for (j = i; j < leng; j++) {
text[j] = text[j + 1];
}
i--;
text[z] == '\0';
} else
if (text[i] == '\0') {
break;
}
}
while (text[d] != '\0') {
if (text[d] == ' ')
spacecount++;
d++;
}
printf("kelime sayisi: %d" , spacecount + 1);
printf("\n cikti:%s ", text);
fclose(fp);
return 0;
}
I can't find problem. It should count the word but it can not do. Help me, please
for(i=0; i < leng; i++) {
if(i=0 && text[i]== ' '){
z--;
for(m=0; m< leng; m++ ){
text[m] = text [m+1];}
i--;
}
else if(1<i<z && text[i] ==' ' && text[i+1] == ' ' ){
z--;
for(j=i; j<leng ; j++) {
text[j+1] = text [j+2];}
i--;
}
else if(i=z && text[i] ==' ' && text[i+1] == '\0' ){
z--;
for(j=i; j<leng ; j++) {
text[j] = text [j+1]; }
i--;
}
},// I think problem in here. Endless loop
Your code is too complicated. You can solve the problem with 2 index variables: one to read the characters from the input line, one to write the relevant characters into the same buffer.
You would keep track of the previous character, starting with space, and detect the beginning of words as the current character is not a space following a space. You would thus count the words and only output a space before each word except the first on a line.
Here is a modified version:
//Counting words program C
#include <stdio.h>
#define N 5000
int main(void) {
FILE *fp;
char text[N];
int total_words = 0;
if ((fp = fopen("soru.txt", "r")) == NULL) {
printf("Dosya açma hatası!\n");
return 1;
}
while (fgets(text, N, fp) != NULL) {
int len = strlen(text);
int word_count = 0;
char c, lastc = ' ';
int i, j;
// strip the trailing newline
if (len > 0 && text[len - 1] == '\n') {
text[--len] == '\0';
}
for (i = j = 0; i < len; i++) {
c = text[i];
if (c != ' ') {
if (lastc == ' ') {
if (word_count > 0) {
// output a space between words
text[j++] = ' ';
}
word_count++;
}
text[j++] = c; // copy the non space character
}
lastc = c;
}
text[j] = '\0'; // set the null terminator
printf("kelime sayısı: %d\n", word_count);
printf("çıktı: %s\n", text);
total_words += word_count;
}
fclose(fp);
printf("\ntoplam kelime sayısı: %d\n", total_words);
return 0;
}
Note a silly bug in your code: if (i = 0 && text[i] == ' ') is parsed as if ((i = (0 && (text[i] == ' '))) != 0) which is always false and sets i to the value 0. C expression syntax is very powerful but somewhat error prone and confusing. I advise you to use -Wall or -Weverything as a compiler option to let the compiler warn about potential mistakes.
Similarly, you should not write if (1<i<z && ...: 1<i<z is parsed as 1<(i<z) which is always false. You must write 1 < i && i < z or more idiomatically i > 1 && i < z
Im taking a numerical input in my command line arguments and if there is a '+' or '-' character im trying to strip it away and only leave the numbers. Ive managed to strip the leading character but whenever i try assigning the value of the string with atoi without the leading character i keep getting a value of 0. Wondering what i could be doing wrong because whenever i print out the string its the same exact string except no leading character so shouldnt the atoi function convert that to a number? The ReadLine() function takes in a string and dynamically allocates memory depending on the size of the user input.
char *val = ReadLine();
int i, size = strlen(val);
int count = 0;
int num = 10;
char *newVal = (char*)malloc(sizeof(char) * 10);
for(i = 0; i < size; i++) //checks once to make sure its proper formatting, if count == size then input is correct
{
if((val[0] == '+' && i == 0) || (val[0] == '-' && i == 0))
{
//printf("check\n");
count++;
i++;
}
if(val[i] >= '0' && val[i] <= '9')
{
count++;
}
}
printf("the count is %d\n\nand the size is %d\n", count, size);
while(count != size) ///input check to make sure user is inputting correct values.
{
//repeat
count = 0;
val = ReadLine();
size = strlen(val);
for(i = 0; i < size; i++)
{
if((val[0] == '+' && i == 0 ) || (val[0] == '-' && i == 0)){
//printf("check\n" );
count++;
i++;
}
if(val[i] >= '0' && val[i] <= '9')
{
//printf("%c\n", val[i]);
count++;
}
}
printf("count: %d\nsize: %d\n", count,size);
}
char numb[size-1];
if(val[0] == '+')
{
//char *fin = (char*)malloc(sizeof(char) * size - 1);
for(i = 1; i < 2; i++)
{
strcat(numb,&val[i]);
count++;
break;
}
printf("value of numb = %s\n", numb);
newVal = (char*)realloc(newVal,sizeof(size-1));
newVal = numb;
printf("Value of newVal = %s\n", newVal);
num = atoi(newVal);
printf("The string is %s", newVal);
return num;
}
else if (val[0] == '-')
{
for(i = 1; i < 2; i++)
{
strcat(numb,&val[i]);
break;
count++;
}
printf("value of numb = %s\n", numb);
newVal = (char*)realloc(newVal,sizeof(size-1));
newVal = numb;
printf("Value of newVal = %s\n", newVal);
num = -1 * atoi(newVal);
printf("the num is%d\nstring is : %s",num, numb);
return num;
}
//now convert that string to integer whether its plus or negative.
num = atoi(val);
return num;
This is the input that im using and this is what it prints back.
Please enter a value
-1248
the count is 5
and the size is 5
value of numb = 1248
Value of newVal = 1248
the num is 0
string is : 1248
The value you put was: 0
Consedering 2 strings, I want to do a program in C that returns the number of possible matches between this 2 strings.
For example
$>./a.out "abc" "a*"
1
Because there is only 1 possible match : *="bc"
$>./a.out "abc" "a**"
3
Because there is only 3 possible matches : (*="bc", *="") ; (*="", *="bc") ; (*="b", *="c")
$>./a.out "ab cool ab cool ab" "ab*ab*"
2
Because there is 2 possible matches : (*=" cool ", *=" cool ab") ; (*=" cool ab cool ", *="")
I did a function "int match(char*, char*)" that returns true when there exists a match and false when there does not exist.
But i would like now to count the possible matches.
Any advices or suggestions?
int match(char* string, char* star) {
if (string[0] == '\0' && star[0] == '\0')
return 1;
else if (star[0] == '*')
return match(string, star+1);
else if (string[0] == '\0')
return 0;
else if (string[0] == star[0]){
if (star[-1] == '*') {
if (!match(string+1, star+1))
return match(string+1, star);
}
return match(string+1, star+1);
}
else if (string[0] != star[0] && star[-1] == '*')
return match(string+1, star);
else if (string[0] != star[0] && star[-1] != '*')
return 0;
}
int count_match(char* string, shar* star) {
int i;
for (i = 0; star[i] != '\0'; i += 1) {
if (star[i] != '*') {
if (star[i] != string[i])
return 0;
}
else {
int count = 0;
int j;
for (j = i; string[j] != '\0'; j += 1) {
count += count_match(string + j; star + i + 1);
count += count_match(string + j; star + i + 1);
return count;
}
}
return (string[i] == star[i]);
}
i'm new in c programming and i need some help with this function because i cant figure it out,
i need to make a function that receives a string and prints out the similar words (the order of the letters ,the amount of the letters and if the letters are capital or small doesn't matter) for example:
if received "Nanny have you any cheap peach?"
the output is:
Nanny any
cheap peach
i can't use pointers ,and cant use string.h library.
i tried and came up with this but i had no luck on figuring it out
void FindSimilarWords(char str2[]){
int f,i,j,last,count=0,count1=0,k,letter,temp=0;
char word1[wordsize],word2[wordsize];
for (i = SIZE2 - 1; i >= 0; i--)
{
if (str2[i] != ' ' && str2[i] != '\0')
{
last = i;
break;
}
}
for (i = 0; i<= last; i++)
{
k = 0;
j = i;
do {
word1[k] = str2[j];
k++;
j++;
} while (str2[j] != ' '&&str2[j] != '\0');
word1[k] = '\0';
for (letter =last; letter >= j-1; letter--)
{
temp = letter;
while (temp != ' ')
{
count1++;
temp--;
}
f = 0;
for (k--; k >= 0; k--)
{
if (str2[j] == word1[k])
{
count++;
word2[f] = str2[j];
f++;
}
}
if (count == count1)
printf("%s %s\n", word1, word2);
else
while (letter != ' ')
letter--;
}
while (str2[i] != ' ')
i++;
}
}
Ok, I'm sorry in advance, I know there is a thousand examples of how do-while loops work and how to exit them. I swear I've tried them all. For the life of me, I cannot get this to exit when the user enters a 0. There has to be something I'm over-looking. How I got the one to work inside the else if loop but the main one is beyond me. I would appreciate any help or direction.
int main()
{
char connectBoard[6][7];
int i , j;
char c;
int turn = 1;
char player1 = 'x';
char player2 = 'o';
int spot;
for( i = 0; i < 6; i++) //sets up 2D array board
{
for( j = 0; j < 7; j++)
{
c = '.';
connectBoard[i][j] = c;
}
}
for( i = 0; i < 6; i++)// prints out 2D array board
{
for( j = 0; j < 7; j++)
{
printf("%c", connectBoard[i][j]);
}
printf("\n");
}
printf("=======\n");
printf("1234567\n");
do{
if( turn%2 != 0 ) //player1 turn
{
for( i = 5; i < 6; i++)
{
for(j = 0; j < 1; j++)
{
printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player1);
scanf("%d", &spot);
j = spot - 1;
if( connectBoard[i][j] == '.' )
{
connectBoard[i][j] = 'x';
}
else if( connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o' )
{
do
{
i--;
}while(connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o');
connectBoard[i][j] = 'x';
if( i == -1 )
{
printf("***Bad entry, try again: ");
scanf("%d", &spot);
j = spot - 1;
i = 5;
connectBoard[i][j] = 'x';
}
}
turn++;
}
break;
}
printf("\n");
for( i = 0; i < 6; i++)// prints out 2D array board
{
for( j = 0; j < 7; j++)
{
printf("%c", connectBoard[i][j]);
}
printf("\n");
}
printf("=======\n");
printf("1234567\n");
}
if( turn%2 == 0 ) //player2 turn
{
for( i = 5; i < 6; i++)
{
for(j = 0; j < 1; j++)
{
printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player2);
scanf("%d", &spot);
j = spot - 1;
if( connectBoard[i][j] == '.' )
{
connectBoard[i][j] = 'o';
}
else if( connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o' )
{
do
{
i--;
}while(connectBoard[i][j] == 'x' || connectBoard[i][j] == 'o');
connectBoard[i][j] = 'o';
if( i == -1)
{
printf("***Bad entry, try again: ");
scanf("%d", &spot);
j = spot - 1;
i = 5;
connectBoard[i][j] = 'o';
}
}
turn++;
}
break;
}
printf("\n");
for( i = 0; i < 6; i++)// prints out 2D array board
{
for( j = 0; j < 7; j++)
{
printf("%c", connectBoard[i][j]);
}
printf("\n");
}
printf("=======\n");
printf("1234567\n");
}
}while( spot != 0 );
return 0;
}
After reading the spot variable, you can check if the value is 0 and break:
printf(" Player %c, drop your piece in which column (1-7 or 0 to quit): ", player2);
scanf("%d", &spot);
if (spot == 0)
return 1;
j = spot - 1;
and change your do while to a while loop to avoid confusion.
Use a variable only for that loop. You are using spot inside your loop to do other things, use it only for the while purpose.
Then, you know the diference between do/while and while? I dont understand very well your program, but if you are reading a spot, you can read it at beggining and outside the loop and then check it with a while loop. Its the same but maybe its more easy for you.
Why are you using spot for while loop. as per C language, It stores a garbage value.
Even if you use, please do not set values to it. Please check basic C variables declaration.