LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-19-2004, 04:55 AM   #1
egoleo
LQ Newbie
 
Registered: Mar 2004
Posts: 2

Rep: Reputation: 0
Reverse and Squeezing of text of character in C


is working alright but it doen't print the first characters. And you plse check them for me again


/*header file*/
#include <stdio.h>
#include <ctype.h>
#define MAX 1000
#define LEN 1000

/*global declaration*/
void show_menu();
void do_menu_choice(char ch);
char get_choice();
void convert(char str[]);
void rev(char string[50]);
int done;
char c[] = " ";

/*function main*/
int main(int argc, char **argv)
{

done = 0;


show_menu();
do_menu_choice(get_choice());




return 0;
}

void show_menu() {

/*menu print outs from A to F*/
printf(" \n");
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf("$ Choose A Menu A--F $\n");
printf("$ ..................... $\n");
printf("$ A Invert Character case of a line text $\n");
printf("$ B Reverse Words $\n");
printf("$ C Squeeze Spaces $\n");
printf("$ D Most frequently occuring letter pair $\n");
printf("$ E Trace of this program $\n");
printf("$ F Exit $\n");
printf("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$\n");
printf(" \n");

}

char get_choice()
{
char ch;

while(ch != 1) {

printf("Enter choice:\n");
ch = getchar();

if(ch >= 'A' && ch <= 'F')
return ch;
}
printf("Invalid choice\n");

}


void do_menu_choice(char ch)
{


switch(ch) {

case 'A':
printf("Enter a string\n\n");
scanf("%s\n", c);
convert(c);
break;
case 'B':
printf("Enter a string\n\n");
scanf("%s\n", c);
rev(c);
break;
case 'C':
printf("Hello\n");
break;
case 'D':
printf("Hoooo\n");
break;
case 'E':
printf("Tahhhaaa\n");
break;
case 'F':
printf("Bye \n");
done = 1;
break;
default:
printf("Funny when using windows\n");

}
}


/*function to convert character of text*/
void convert(char str[])
{


char ch[MAX];
int i;
scanf("%c",ch);

if (fgets(ch, MAX, stdin) !='\0')
{
for(i=0; ch[i]; ++i)
{
if(isupper( (int)ch[i]) )
{
ch[i] = tolower(ch[i]);
}

else
{
ch[i] = toupper(ch[i]);
}
}
printf("\nThe Converted text: %s \n",ch);
}
else
{
printf("\nNo input fetched!\n");
}


}


/*function to reverse character of text*/
void rev(char string[1000])
{
int i,j;
char text[LEN];
char r_text[LEN];
fgets(text, LEN, stdin);
i = strlen(text) - 1; // -1 removes \n
j = 0;

while(i--)
r_text[j++] = text[i];
r_text[j]='\0';

printf("%s\n",r_text);
}
 
Old 03-19-2004, 09:45 AM   #2
naflan
Member
 
Registered: May 2003
Location: NC, USA
Distribution: Slackware 14.0
Posts: 94

Rep: Reputation: 15
Code:
 printf("Enter a string\n\n");
scanf("%s\n", c);
rev(c);
scanf only gets one char. Use fgets.

in rev function:
1 you don't need variable text if you passed value to funtion in string[1000]. Use the local variable that you passed to the function.
2 you don't need this line if you've already gotten the input from user and passed to function: fgets(text, LEN, stdin); Put this line in place of scanf above and change rev(c) to rev(text);

I didn't review all of your code, but I'm not sure you'll need scanf anywhere. If you're getting stings, use fgets.
Get rid of #define LEN 1000 because you've already defined MAX as 1000. Replace LEN with MAX in your code.

Thats just a few of the problems
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
reverse order some lines of text files Melsync Programming 4 09-20-2005 04:40 PM
vi showing all text in red reverse video paultaylor Linux - Software 2 11-12-2004 09:36 AM
Reverse Sort Text File BxBoy Linux - General 1 08-02-2004 10:13 AM
Set text (runlevel 3) console character width davee Linux - Newbie 2 11-16-2003 03:33 PM
Bug A Javascript that will not allowed alphabet character type into a text box Linh Programming 2 09-22-2003 11:15 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:24 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration