Eerror message in function 'main' [closed] - c

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 8 years ago.
Improve this question
Program to take the input and output the reverse of it:
#define MAX 1000
int readtext(char[],int); /*used to store text in array and
returns the size of the line*/
void reverse(char[]); /*used to reverse the text in the line and
returns 0*/
int main(void)
{
char text[MAX];
printf("Enter text, press Ctrl+d when done \n"); /*prompt user input*/
while((redtext(text, sizeof text)>0)) /*loop repeats until text size is >0*/
{
reverse(text);
printf("%s\n\n",text);
}
return 0;
}
int readtext(char a[],int len)
{
int letchar,i;
for(i=0;i<len-1 && (letchar=getchar())!=EOF && letchar!='\n';i++) /*for loop repeats until end of line*/
a[i]=letchar;
if(letchar=='\n') /*checks if letchar is \n. if true, changes it to null and returns i value*/
a[i++]=letchar;
a[i]='\0';
return i;
}
void reverse(char a[])
{
char t;
int x,y;
for(y=0;a[y]!='\0';y++) /*loop used to get the last element of the array*/
--y;
for(x=0;x<y;x++) /*loop used to reverse the array 'a'*/
{
t=a[x];
a[x]=a[y];
a[y]=t;
--y;
}
}
expected input/output:
happy birthday
yadhtrib yppah
I am getting this error message, but do not know what it means:
/tmp/ccA71SDX.o: In function `main':
1-19.c:(.text+0x63): undefined reference to `redtext'
collect2: ld returned 1 exit status

You have made a mistake in function call (redtext instead of readtext). However you can use my solution:
#include <stdio.h>
#include <strings.h>
#define MAXSTRLEN 256
void Reverse(char* str);
int main()
{
printf("Enter string below:\n");
char str[MAXSTRLEN];
fgets(str, MAXSTRLEN, stdin);
Reverse(str);
printf("Result:\n%s\n", str);
return 0;
}
void Reverse(char* str)
{
char tmp;
int length = strlen(str) - 1;
int i;
for(i = 0; i < length / 2; i++)
{
tmp = str[i];
str[i] = str[length - i - 1];
str[length - i - 1] = tmp;
}
}

Related

Having trouble with recursion [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 1 year ago.
Improve this question
I am trying to make a program which prints all the possible permutations of the string "A?B?AB?" via replacing question marks with A or B. I can't understand why my code works the way it does and need help improving it.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char* rec(char s[8]){
int i=0;
for(;i<strlen(s);i++){
if(s[i]=='?'){
s[i]='A';
rec(s);
s[i]='B';
rec(s);
s[i]='?';
}
}
for(int k=0;k<strlen(s);k++){
if(s[k]=='?')
i=-1;
}
if(i!=-1)
printf("%s\n",s);
return s;
}
int main(){
char s[8]="A?B?AB?";
rec(s);
}
This should do the trick:
#include <stdio.h>
#include <string.h>
void rec(char *str, int size, int depth){
for (int i = depth; i < size; i++){
if (str[i] == '?'){
str[i] = 'A';
rec(str, size, i + 1);
str[i] = 'B';
rec(str, size, i + 1);
str[i] = '?';
return;
}
}
printf("%s\n", str);
}
int main(){
char s[8] = "A?B?AB?";
rec(s, strlen(s), 0);
}
It's much like August's solution, but I did decide to do some looping until it found the next question mark. That should avoid having too big of a callstack, which could lead to stack overflow, with really big strings. (Note: I didn't test it, so there could still be some minor problems)
You only need to look at one character at a time. Try this:
#include <stdio.h>
void PrintPermutationsFrom(int i, char s[])
{
if (s[i] == '?') {
s[i] = 'A';
PrintPermutationsFrom(i + 1, s);
s[i] = 'B';
PrintPermutationsFrom(i + 1, s);
s[i] = '?';
} else if (s[i] != '\0') {
PrintPermutationsFrom(i + 1, s);
} else {
puts(s);
}
}
void PrintPermutations(char s[])
{
PrintPermutationsFrom(0, s);
}
int main(void)
{
char s[] = "A?B?AB?";
PrintPermutations(s);
return 0;
}
I can't understand why my code works the way it does and need help improving it.
The rec code does simply too much in that after having replaced a ? with both A and B and called itself, it continues iterating over s and generates further output. That is too much because after the first found ?, the recursive invocations already have handled all following ? and generated all arrangements. To correct this, just insert a break; after s[i]='?';.

warning array comparison between pointer and integer [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
So i have this problem where i have to input a string of unknown size with only lowercase letters then output the number of distinct letters.this is the main program
#include <stdio.h>
#include <ctype.h>
int test(char *T);
int main(){
char T[100];int i=-1,j=0,nd=0;
do{
gets(T);
}while((test(T))==1);
do {
i++;
j=i;
do{j++;
}while ((T[i]!=T[j])||((T[j])!=""));
if (T[j]=="")
nd++;
}while (T[i+1]!="");
and this is my function test
int test(char *T){
int i=-1,s;
do {
i++;
}while (((islower(T[i])==1))||(T[i]==""));
if ((T[i]=="")&&(i!=0))
s=0;
else s=1;
return s;
}
the problem is that i get a lot of warnings "comparison between integer and pointer" everytime i compare a char of the array T and i don't knowhow to fix that.your help would be much appreciated.
Update:So i tried fixing the program following your advices and this is the new main program
#include <stdio.h>
#include <ctype.h>
int test(char *T);
int main(){
char T[100];int i=-1,j=0,nd=0;
do{
gets(T);
}while((test(T))==1);
do {
i++;
j=i;
do{j++;
}while ((T[i]!=T[j])||((T[j])!='\0'));
if (T[j]=='\0')
nd++;
}while (T[i+1]!='\0');
printf("%d",nd);}
and this is function test
int test(char *T){
int i=-1,s;
do {
i++;
}while (((islower(T[i])==1))||(T[i]=='\0'));
if ((T[i]=='\0')&&(i!=0))
s=0;
else s=1;
return s;
}
I don't get anymore warnings and the program gets compiled with no problems but after i input the string in the execution nothing happens.
You function test should return 1 if the string contains only lowercase letters, and 0 otherwise. Unfortunately, it is not doing that.
You should first test if the character is a letter and then if it's a lowercase letter. Or more efficiently, you test if the character is in the range 'a' to 'z'.
Another problem of your code is the use of do while loops which makes the code difficult to understand and executes the loop once.
Here is a better implementation of the test function:
int test(char *T){
// reject empty strings
if(T[0] == '\0')
return 0;
// reject strings containing non lowercase letter
for(int i = 0; T[i] != '\0'; i++)
if((T[i] < 'a') || (T[i] > 'z'))
return 0;
// string is not empty and contains only lowercase letters
return 1;
Counting the different letters can be made more readable by using a for loop instead of a go while loop.
int nd = 0;
for(int i = 0; T[i] != '\0'; i++) {
for(int j = 0; j < i; j++) {
if(T[j] == T[i])
break; // quit inner loop
nd++;
}
}
This code examine each letter and see if it has been seen before. It is thus different from yours.
A problem in your code is the test (T[i]!=T[j])||((T[j])!='\0'). It should be && instead of ||, and testing if the end of string is reached should be performed first. The test should be (T[j]!='\0')&&(T[i]!=T[j]).
So my code is finally working here's the final main program
#include <stdio.h>
#include <ctype.h>
int test(char *T);
int main(){
char T[100];int i,j,nd=0;
do{
gets(T);
}while((test(T))==0);
for(i = 0; T[i] != '\0'; i++) {
j=i;
do{
j++;
}while ((T[j]!='\0')&&(T[j]!=T[i]));
if (T[j]=='\0')
nd++;
}
(#chmike i used the code you posted with a little adjustment on the loop)
and for the function test i used the code that posted #chmike as well.
Huge thanks to all of you guys for the help you provided :)

Is there a way to print only the first word of a string with gets() in C? [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 2 years ago.
Improve this question
I am very new to programming and i wonder if there is a way to print out the first word of a string with gets() in C?
void printFirstWord(char string[])
{
int i;
for(i = 0; i < (string[i] != '\0'); i++)
{
if(isalpha(string[i]))
printf("%c", string[i]);
}
}
int main()
{
char string[MAX];
printf("Type in a scentence");
gets(string);
printFirstWord(string);
return 0;
}
This is the function that i have written and called in main right now. Is it because i have isalpha in the function?
In your implementation, you might add the following line in the loop:
if (string[i] == ' ')
break;
also, fix your loop parameters e.g. like this:
for (i = 0; i < strlen(string); i++)
Overall implementation in you way will be as below.
Consider choosing another design according to comments you got, e.g. not using gets.
void printFirstWord(char string[])
{
int i;
for (i = 0; i < strlen(string); i++)
{
if (isalpha(string[i]))
printf("%c", string[i]);
if (string[i] == ' ')
break;
}
}
int main()
{
#define MAX 100
char string[MAX];
printf("Type in a scentence\n");
gets_s(string, MAX);
printFirstWord(string);
getchar();
return 0;
}
I just found a way with isblank(); function, hope it helps to anybody :)
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main (){
int length, number, counter, position;
char name[50];
printf("Please type your complete name:\n");
gets(name);
//strlen();
//Returns the length of the given null-terminated byte string, that is, the number of characters in a character array
length=strlen(name);
//Counts each position until it finds a space
for(counter=0;counter<length;counter++)
{
if(isblank(name[counter]))
position=counter;
}
//Prints each character until the counter reaches the position number given by the counter variable
printf("\nThe first word you typed is: ");
for(number=0; number<=position; number++){
printf("%c", name[number]);
}
}

string palindrome in 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 7 years ago.
Improve this question
I am writing a program of string pallindrome, code is compiling successfully but on running it accepting the string but nothing after that, the output window stays on hold with cursor blinking, help me what is wrong with this code.
I am using dev-c++
gets(ch); // the program stops here
p=ch;
while(ch!='\0')
{ p++;
size++;
}
for(i=0;i<20;i++)
{
if(c[i]==c[j])
printf("string is pallindrome");
else printf("string is not pallindrome");
}
getch();
return 0;
}
Here is the problem:
while(ch!='\0')
ch is a char array, and you are comparing it with a single char.
Also, size is not initialised.
I would suggest something like this:
size=0;
while(ch[size]!='\0')
{ p++;
size++;
}
or, using the pointer method:
while(*p!=0)
{
p++;
size++;
}
Also, instead of printing inside the for loop (which would make it print several times), use a flag variable.
You only need one loop, for example while (i < i).
Look at this example that will do the job:
#include <stdio.h>
#include <string.h>
/* in c99 use <stdbool.h> instead*/
typedef int bool;
#define true 1;
#define false 0;
int main(void)
{
char ch[20];
puts("enter the string: ");
gets(ch);
size_t size = strlen(ch);
bool pallindrome = true;
int j = size-1;
int i = 0;
while (i < j)
{
if(ch[i] != ch[j]) {
pallindrome = false;
break;
}
++i;
--j;
}
if (pallindrome)
printf("\"%s\" is pallindrome\n", ch);
else
printf("\"%s\" is not pallindrome\n", ch);
getch();
return 0;
}

C programming - Trying to make Vigenere encryption/decryption program [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 8 years ago.
Improve this question
Can you take a look on this code please?
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char encrypt(char *abc, int *key,char text, int counter)
{
int i;
int encryptedletter;
for(i=0;i<=25;i++)
{
if(text==abc[i])
{
encryptedletter = (i + key[counter])%26 ;
return abc[encryptedletter];
}
}
}
char decrypt(char *abc, int *key,char text, int counter)
{
int i;
int decryptedletter;
for(i=0;i<=25;i++)
{
if(text==abc[i])
{
decryptedletter = (i-key[counter])%26 ;
return abc[decryptedletter];
}
}
}
int main(void)
{
char text[100];
char encryped[100];
char decrypted[100];
char key[20];
int i,z,q,keylength,counter=0;
char abc[27]= "abcdefghijklmnopqrstuvwxyz"; //ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .";
int keyint[70];
printf("Give Text: ");
gets(text);
printf("Give Password: ");
gets(key);
keylength = strlen(key);
for(z=0;z<strlen(key);z++)
{
for(i=0;i<=25;i++)
{
if(key[z]==abc[i]) keyint[z] = i;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~ENCRYPTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for(i=0;i<strlen(text);i++)
{
if(counter>=keylength) counter=0;
encryped[i] = encrypt(abc,keyint,text[i],counter);
counter++;
}
encryped[strlen(text)]='\0';
printf("\nEncrypted text: %s\n", encryped);
//~~~~~~~~~~~~~~~~~~~~~~~~~~DECRYPTION~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
counter =0;
for(q=0;q<=strlen(text);q++)
{
if(counter>=keylength) counter=0;
decrypted[q] = decrypt(abc,keyint,encryped[q],counter);
counter++;
printf("%c", decrypted[q]);
}
return 0;
}
I'm trying to make an encryption/decryption program based on Vigenere.
I think it's working fine.. but when letters of text or password are higher than 'T'(not sure) on my abc array then i get back wrong decrypted text.
you need to consider the case when you never enter if(text==abc[i]) within encrypt() and decrypt(). what is the return value that you write into the encrypted/decrypted in that case?
you might want to return text unencrypted so that you preserve spaces, punctuation etc, and thus ensure that both encrypted and decrypted messages are of length strlen(text)
finally, decryptedletter = ((i-key[*counter])%26) can become negative so that you can't use it as an index for abc[decryptedletter]. in that case, you need to "wrap it around" e.g. by
if(decryptedletter<0) decryptedletter += 26;

Resources