Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums

Go Back   Code Forums > Application and Web Development > Standard C, C++

Reply
 
LinkBack Thread Tools Display Modes
Old 10-24-2006, 09:50 AM   #1 (permalink)
neeven
Recruit
 
Join Date: Oct 2006
Posts: 7
neeven is on a distinguished road
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 ) ;
neeven is offline   Reply With Quote
Old 10-25-2006, 10:29 AM   #2 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
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
__________________
toe_cutter is offline   Reply With Quote
Old 10-25-2006, 10:47 AM   #3 (permalink)
toe_cutter
Code Monkey
 
Join Date: Aug 2002
Location: Boston, MA
Posts: 79
toe_cutter is on a distinguished road
Send a message via ICQ to toe_cutter Send a message via AIM to toe_cutter Send a message via Yahoo to toe_cutter
hmmm...not allowed to edit my above post?

For many inputs try a loop. Look up while and do/while loops.
__________________
toe_cutter is offline   Reply With Quote
Old 10-25-2006, 11:28 AM   #4 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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...
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-26-2006, 12:35 PM   #5 (permalink)
Deliverance
C++ Beginner
 
Join Date: Jul 2005
Location: Ottawa
Posts: 73
Deliverance is on a distinguished road
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...
Deliverance is offline   Reply With Quote
Old 10-28-2006, 06:35 AM   #6 (permalink)
neeven
Recruit
 
Join Date: Oct 2006
Posts: 7
neeven is on a distinguished road
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;
}
neeven is offline   Reply With Quote
Old 10-28-2006, 09:34 AM   #7 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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;
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-28-2006, 10:29 PM   #8 (permalink)
neeven
Recruit
 
Join Date: Oct 2006
Posts: 7
neeven is on a distinguished road
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?
neeven is offline   Reply With Quote
Old 10-29-2006, 02:42 AM   #9 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 10-29-2006 at 03:05 AM.
redhead is offline   Reply With Quote
Old 10-29-2006, 02:57 AM   #10 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-29-2006, 06:56 AM   #11 (permalink)
neeven
Recruit
 
Join Date: Oct 2006
Posts: 7
neeven is on a distinguished road
there's a problem- 'z' should be converted to 'a' and not 'A'...
neeven is offline   Reply With Quote
Old 10-29-2006, 07:03 AM   #12 (permalink)
neeven
Recruit
 
Join Date: Oct 2006
Posts: 7
neeven is on a distinguished road
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;
neeven is offline   Reply With Quote
Old 10-29-2006, 07:23 AM   #13 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
Code:
while(1)
    {
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.
Code:
  return 0;
Terminate the main() function.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001

Last edited by redhead; 10-29-2006 at 07:36 AM.
redhead is offline   Reply With Quote
Old 10-29-2006, 07:35 AM   #14 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,720
redhead is on a distinguished road
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;
}
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 10-31-2006, 06:43 AM   #15 (permalink)
neeven
Recruit
 
Join Date: Oct 2006
Posts: 7
neeven is on a distinguished road
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.
neeven is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
regex for allowed characters in a form field DogTags PHP 1 03-13-2005 08:42 AM
ncurses won't display anything but keyboard characters! Hosiah Linux / BSD / OS X 3 02-01-2005 06:36 PM
convert a String to hex sde Java 2 06-03-2004 07:35 AM
Multi Byte Characters with PHP/MySQL sde PHP 1 03-21-2004 01:56 PM


All times are GMT -8. The time now is 08:53 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting