|
 |
|
 |
10-24-2006, 09:50 AM
|
#1 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 7
|
i need help to convert characters
am a beginner in programming C and am trying out some questions in books...
i want to create a small encryption program that will convert a character such as 'a' to the next character of the alphabet that is 'b'
this is what ive done till now:
i want to know how can i make the program accept many inputs..
n how can i ignore blank spaces and special characters?
Code:
#include <stdio.h>
#include <stdlib.h>
int main( void )
{
char name[BUFSIZ] = {'\0'} ;
printf ( "Enter the string: " ) ;
fgets( name, BUFSIZ, stdin ) ;
int i = 0 ;
for( i = 0; i < strlen( name ) - 1; ++i )
{
name[i] = name[i] + 1 ;
}
printf( "%s", name ) ;
|
|
|
10-25-2006, 10:29 AM
|
#2 (permalink)
|
|
Code Monkey
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
|
Many inputs? Do you mean after you hit a carrage return?
To ingore blank spaces and special characters you need to test for them right before you increment name, or might be easier to test if they are ligitmate characters.
Toe
__________________
|
|
|
10-25-2006, 10:47 AM
|
#3 (permalink)
|
|
Code Monkey
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
|
hmmm...not allowed to edit my above post?
For many inputs try a loop. Look up while and do/while loops.
__________________
|
|
|
10-25-2006, 11:28 AM
|
#4 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Theres a limitation on editing previus posts, the edit feature will be disabled after 10 minuts from post time..
It's due to those thinking "I got the answer I wanted, so now i'll just edit my orriginal post, since it's of no further interest..." Which kinda makes it hell to search and explain something when referencing to old threads, when all of a sudden they're not displaying anything you orriginaly wanted to point out in them...
|
|
|
10-26-2006, 12:35 PM
|
#5 (permalink)
|
|
C++ Beginner
Join Date: Jul 2005
Location: Ottawa
Posts: 73
|
Neeven:
Try out the isspace() function
#include <ctype.h>
int j;
int isspace(int c);
isspace will test for a character that is a standard whitespace (space,tab,return,newline,vertical tab,etc).
return: returns non-zero for true, 0 for false.
should be pretty simple to implement in your code, try something like this:
Code:
if ((j = isspace(c)) == -1)
break;
else
name[i] = name[i] + 1;
similarly i'm pretty sure there is an isalpha() which behaves the same way to ensure you are not changing special symbols...
see http://www-ccs.ucsd.edu/c/ctype.html for more information on all the functions you can use with ctype
Last edited by Deliverance; 10-26-2006 at 12:40 PM.
Reason: just to add a little snippet...
|
|
|
10-28-2006, 06:35 AM
|
#6 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 7
|
Deliverance: i have tried this but it still doesnt work..can you tell me wats wrong?
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#define INPUT 512
int main(int argc, char *argv[])
{
int j;
int c;
int isspace(int c);
char name[INPUT] = {'\0'} ;
printf ( "Enter the string: " ) ;
fgets( name, INPUT, stdin ) ;
int i = 0 ;
for( i = 0; i < strlen( name ) - 1; ++i )
{
if ((j = isspace(c)) == -1)
break;
else
name[i] = name[i] + 1;
}
printf( "%s", name ) ;
system("PAUSE");
return 0;
}
|
|
|
10-28-2006, 09:34 AM
|
#7 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
You need to test for the current location in your string, not c.
Code:
if ((j = isspace(name[i])) == -1)
And delete the int isspace(int c); definition inside your main() function, it has no use, since including ctype.h will already do that.
Perhaps you even want to turn the whole situation around with something like:
Code:
for( i = 0; i < strlen( name ) - 1; ++i )
if (isalpha(name[i]) || isdigit(name[i]))
name[i] = name[i] + 1;
So it's only alphanumeric charactors that are beeing transformed.
Or perhaps even, stop relying on a system provided function and hardcode it yourself, like:
Code:
for( i = 0; i < strlen( name ) - 1; ++i )
if ((name[i] >= 'a' && name[i] <= 'z') || (name[i] >= 'A' && name[i] <= 'Z'))
name[i] = name[i] + 1;
|
|
|
10-28-2006, 10:29 PM
|
#8 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 7
|
thks redhead..
can u help me with loops...
which loop should i write for the program to accept an input and give the output,accept another input and display the output and so on...
how should i write the loop?
|
|
|
10-29-2006, 02:42 AM
|
#9 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Code:
#include <stdio.h>
#include <string.h>
#define INPUT 512
int main()
{
int i;
char name[INPUT] = {'\0'} ;
while(1)
{
printf ( "Enter the string (quit to quit): " ) ;
fgets( name, INPUT, stdin ) ;
if(!strncmp(name, "quit", 4))
break;
for( i = 0; i < strlen( name ); ++i )
if ((name[i] >= 'a' && name[i] <= 'z') ||
(name[i] >= 'A' && name[i] <= 'Z'))
name[i] = name[i] + 1;
printf ( "Transformed string: %s\n ", name) ;
}
return 0;
}
Last edited by redhead; 10-29-2006 at 03:05 AM.
|
|
|
10-29-2006, 02:57 AM
|
#10 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Or perhaps a more elaborate version, if you want to catch any type of mistakes which the previus one dosnt prevent, like changing 'z' to '}' since thats the very next char regarding to the integer-value this program is relying on.
Code:
#include <stdio.h>
#include <string.h>
#define INPUT 512
int main()
{
int i, j;
char name[INPUT] = {'\0'} ;
char accepted[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
int length = strlen(accepted);
while(1)
{
printf ("Enter the string (quit to quit): " ) ;
fgets( name, INPUT, stdin ) ;
if(!strncmp(name, "quit", 4))
break;
for(i=0; i < strlen( name ); ++i )
for(j=0; j < length; ++j)
if (name[i] == accepted[j])
{
name[i] = accepted[(j+1)%length];
break;
}
printf ( "Transformed string: %s\n ", name) ;
}
return 0;
}
This version will allways try and exchange the shifted char with the very next alphanumeric char found as accepted.
|
|
|
10-29-2006, 06:56 AM
|
#11 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 7
|
there's a problem- 'z' should be converted to 'a' and not 'A'...
|
|
|
10-29-2006, 07:03 AM
|
#12 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 7
|
redhead: can you explain me what each line of the code does plz...im having problems undestandin 'em
Code:
while(1)
{
printf ("Enter the string (quit to quit): " ) ;
fgets( name, INPUT, stdin ) ;
if(!strncmp(name, "quit", 4))
break;
for(i=0; i < strlen( name ); ++i )
for(j=0; j < length; ++j)
if (name[i] == accepted[j])
{
name[i] = accepted[(j+1)%length];
break;
}
printf ( "Transformed string: %s\n ", name) ;
}
return 0;
|
|
|
10-29-2006, 07:23 AM
|
#13 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
A loop with no terminating clause, it will run forever
Code:
printf ("Enter the string (quit to quit): " ) ;
fgets( name, INPUT, stdin ) ;
Request user for input, to manipulate
Code:
if(!strncmp(name, "quit", 4))
break;
Check the userinput to see if it contains the word "quit" if so, terminate the never ending loop by making a break call, which in machine terms gives it a jump instruction to skip out of the current loop
Code:
for(i=0; i < strlen( name ); ++i )
Cyckle through the userinput letter by letter
Code:
for(j=0; j < length; ++j)
cyckle through the accepted chars letter by letter
Code:
if (name[i] == accepted[j])
{
Should our current userinput letter be pressented in the accepted char array
Code:
name[i] = accepted[(j+1)%length];
break;
}
Replace it with the next char in our accepted array, taking into count that we will not exceed the array size, and merely wrap around and pick the char from the beginning of the array, should we reach the end of the array.
The break instruction here, will terminate the inner loop (the one cyckling through our accepted chars) since we've found a match theres no reason to continue searching.
Code:
printf ( "Transformed string: %s\n ", name) ;
}
When we reach the end of the outer loop (the one cyckling through userinput) print the resulting name, which by now should contain the altered string. Terminate the main() function.
Last edited by redhead; 10-29-2006 at 07:36 AM.
|
|
|
10-29-2006, 07:35 AM
|
#14 (permalink)
|
|
Newbie
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
|
Quote:
|
there's a problem- 'z' should be converted to 'a' and not 'A'...
|
Then you need to make two seperate letter searches, one looking for lowercase letters, and one looking for uppercase, something like this could be the answer
Code:
#include <stdio.h>
#include <string.h>
#define INPUT 512
int main()
{
char name[INPUT] = {'\0'};
int i;
while(1)
{
printf ("Enter the string (quit to quit): " ) ;
fgets( name, INPUT, stdin ) ;
if(!strncmp(name, "quit", 4))
break;
for(i=0; i < strlen( name ); ++i )
{
/* if we're ranging from 'a'/'A' to 'y'/'Y' it is "safe"
* to simply just add one to the current value
*/
if((name[i] >= 'a' && name[i] < 'z') ||
(name[i] >= 'A' && name[i] < 'Z'))
name[i] = name[i] +1;
else
{
/* we need to catch the two borderline cases, where
* simply adding one would cause us to skip past
* any accepted chars.
*/
switch(name[i])
{
case 'z':
name[i] = 'a';
break;
case 'Z':
name[i] = 'A';
}
}
}
printf ( "Transformed string: %s\n ", name) ;
}
return 0;
}
|
|
|
10-31-2006, 06:43 AM
|
#15 (permalink)
|
|
Recruit
Join Date: Oct 2006
Posts: 7
|
how do i make the following program run forever...
Code:
#include <stdio.h>
main()
{
char line[100];
int count;
printf("Enter a line of text below:\n\n");
scanf("%[^\n]", line);
printf("\n\n");
printf("\nEncoded text:\n\n");
for(count=0; line[count]!='\0'; ++count)
{
if(((line[count]>='A') && (line[count]<'Z')) || ((line[count]>='a') && (line[count]<'z')))
putchar(line[count]+1);
else if (line[count]=='z')
putchar('a');
else if (line[count]=='Z')
putchar('A');
else putchar(line[count]);
}
printf("\n\n");
getch();
return 0;
}
Last edited by redhead; 10-31-2006 at 08:52 AM.
|
|
|
| Thread Tools |
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -8. The time now is 08:53 PM.
|
Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting
|
 |
|