C/String Handling . .
Expert: Prince M. Premnath - 1/29/2008
Questionthe program should read user input. the output would be like this:
Enter String: the quick brown fox //then press enter
hetay uickqay rownbay oxfay
/****the 1st letter of the word is transferred to the last and will be added a suffix AY.I have a program here but i don't know how to transfer the 1st letter to the last.i can only delete the 1st letter.I don't know what to do.pls i need i help!****////
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
clrscr();
char *str1;
char *str2={"ay"};
char *p;
int i;
int len;
printf("Enter String: ");
gets(str1);
p=strtok(str1," ");
if(p)
{
len=strlen(p);
for(i=0;i<len;i++)
{
p[i]=p[i+1];
}
len--;
printf("%s%s ",p,str2);
}
while(p=strtok(NULL," "))
{
len=strlen(p);
for(i=0;i<len;i++)
{
p[i]=p[i+1];
}
len--;
printf("%s%s ",p,str2);
}
getch();
}
AnswerHi Dear Cathy !
Here i made some corrections with your programme , actually your programme is almost near to the expected result but there you commit a mistake
take a look at this statements !
>>
for(i=0;i<len;i )
{
p[i]=p[i 1];
}
You have moved the characters 1 step forward but you failed to restore the 1st character in the array to the last position . In order to rectify this problem , ive used a temporary variable "temp" to store the first character in the array say p[0] , that after the successful shipment of all the characters to 1 position left , the last position say p[len-1] is replaced with temp.
Debugged code!
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char *str1;
char *str2={"ay"};
char *p;
int i;
int len;
char temp;
clrscr();
printf("Enter String: ");
gets(str1);
p=strtok(str1," ");
if(p)
{
len=strlen(p);
temp = p[0];
for(i=0;i<len;i )
{
p[i]=p[i 1];
}
p[len-1] = temp;
printf("ss ",p,str2);
}
while(p=strtok(NULL," "))
{
len=strlen(p);
temp = p[0];
for(i=0;i<len;i )
{
p[i]=p[i 1];
}
p[len-1] = temp;
printf("ss ",p,str2);
}
getch();
}
Thanks and Regards !
Prince M. Premnath
for support : princeatapi@yahoo.co.in