Jump to content

programming with character strings

- - - - -

  • Please log in to reply
13 replies to this topic

#1
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Can somebody please give me a hand on this.I don't know how to create this programme.

Make a program that will turn backwards the spelling of the word being input (in roman letters).

Example: EATTAE


hint: put the suffix and read each letter as one unit


eg: copy the first letter to the 5th letter
x[0]= x[4];



*when u make the circular letters , don't forget to set the end part of the word

#2
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
Have you looked at : Strings library - C++ Reference

My idea is to check string length(there is a function for that in string library), then use swap(also in string) on first and last char. then swap second and last -1 ... and so on(for loop will do it) until you have one or 0 characters left. Dont forget to subtract 1 from string length cause you are starting from 0 not from 1.
In my opinion this way is much more easier than using copy.
Remebre about KISS & DRY

#3
Mephala

Mephala

    Newbie

  • Members
  • Pip
  • 6 posts
I think the key point here is to understand you can refer to a character in any string as if you are referring an element in an array. For example;

string myString = "pencil";

in this case; myString[0]=p , myString[1]=e... etc;

then all you have to do is using this syntax to reverse a string.

A way of doing this could be like this:

string a; // We want this to be reversed.
string b=""; // This will be the solution. Initially it contains no chars.
int i=0;
int k=a.length()-1; // -1 is because first element's index is 0, instead of 1.
while(k>=0)
{
b[i]=a[k];
k--;
i++;
}

#4
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
I think, performance wise, it's better to use (reverse) iterators as arguments to 2nd string's constructor.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#5
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
This is what I have tried to do by using the reverse function but when I enter a string in the program,it stops working and freezes.What is wrong?

#include<string.h>
#include<stdio.h>
int main(){
char *str,*rev;
int i,j;
printf("\nEnter a string:");
scanf("%s",str);
for(i=strlen(str)-1,j=0;i>=0;i--,j++)
rev[j]=str[i];
rev[j]='\0';
strcat(str,rev);
printf("\n%s",str);
return 0;
}

#6
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
You just declared pointers to char datatype, you didn't actually reserve space to store data.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#7
notes

notes

    Learning Programmer

  • Members
  • PipPipPip
  • 48 posts
If you are implementing string.h library why you are using 'char' data type ? Its possible to do it that way, but why invent a wheel again ? You've got everything needed included and you are not using it.
Remebre about KISS & DRY

#8
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
Can you explain to me more clearly please?

#9
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
First question: is this C or C++? Appropriate answers will be completely different.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#10
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts
This is c++ but I have just solved it. :)
However, can you explain this part here,
for(i=strlen(str)-1,j=0;i>=0;i--,j++)

#11
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
The comma operator lets you do two things as a "statement", so you increment j AND decrement i on every loop.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#12
aruwin

aruwin

    Learning Programmer

  • Members
  • PipPipPip
  • 42 posts

WingedPanther said:

The comma operator lets you do two things as a "statement", so you increment j AND decrement i on every loop.

What about this one?What does this mean and what is the purpose?

rev[j]='\0';




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users