"I LOVE MY COUNTRY"
write a c program to get "COUNTRY MY LOVE I" as the output.

Use any other programming language. It is not mandatory to
use C.

Answers were Sorted based on User's Feedback



"I LOVE MY COUNTRY" write a c program to get "COUNTRY MY LOVE I" as the output...

Answer / sandeep

#include <stdio.h>

void rev(char *l, char *r);


int main(int argc, char *argv[])
{
char buf[] = "I LOVE MY COUNTRY";
char *end, *x, *y;

// Reverse the whole sentence first..
for(end=buf; *end; end++);
rev(buf,end-1);


// Now swap each word within sentence...
x = buf-1;
y = buf;

while(x++ < end)
{
if(*x == '\0' || *x == ' ')
{
rev(y,x-1);
y = x+1;
}
}

// Now print the final string....
printf("%s\n",buf);

return(0);
}


// Function to reverse a string in place...
void rev(char *l,char *r)
{
char t;
while(l < r)
{
t = *l;
*l++ = *r;
*r-- = t;
}
}

Is This Answer Correct ?    35 Yes 16 No

"I LOVE MY COUNTRY" write a c program to get "COUNTRY MY LOVE I" as the output...

Answer / shaik

#include<stdio.h>

void main ()
{
char str1[]="I LOVE MY COUNTRY",str2[20];
int count,i;
clrscr();
i=0;
While (str1[i]!='')
{
count++;
i++;
}
i=0;
while (str1[i]!='')
{
str2[count-1-i]=str1[i];
i++;
}
str2[i]='';
printf ("reversed string is %s",str2);
getch ();
} // guys check for the syntax typed from phone sry

Is This Answer Correct ?    11 Yes 7 No

"I LOVE MY COUNTRY" write a c program to get "COUNTRY MY LOVE I" as the output...

Answer / knk

i think it finets a tower of hanoi prob,its pgm shud work jus

Is This Answer Correct ?    9 Yes 6 No

"I LOVE MY COUNTRY" write a c program to get "COUNTRY MY LOVE I" as the output...

Answer / iravani

#include<iostream>
#include<vector>
using namespace std;
int main()
{
string s,s1;
int n,i,j,k;
vector<string>v,v1;
cin>>n;
getline(cin,s,'\n');
v.push_back(s);
for(i=0;i<n;i++){
getline(cin,s,'\n');
v.push_back(s);
}
for(i=0;i<v.size();i++){
for(j=0;j<v[i].size();j++){
while(v[i][j]!=' ' && j<v[i].size()){
s1+=v[i][j];
j++;
}
v1.push_back(s1);
s1.clear();
}
for(k=v1.size()-1;k>=0;k--){
cout<<v1[k]<<" ";
}
v1.clear();
cout<<endl;
}

u can give input as many string as u want..

Is This Answer Correct ?    8 Yes 5 No

"I LOVE MY COUNTRY" write a c program to get "COUNTRY MY LOVE I" as the output...

Answer / archana

#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
char samp[] = "I LOVE MY COUNTRY";
char *ch;
string st;
vector<string> vec;
ch = strtok(samp, " ");
while(ch != NULL) {
printf("%s\n", ch);
vec.push_back(ch);
ch = strtok(NULL, " ");
}
while(!vec.empty()) {
cout << vec.back() << " ";
vec.pop_back();
}
}

Is This Answer Correct ?    5 Yes 4 No

"I LOVE MY COUNTRY" write a c program to get "COUNTRY MY LOVE I" as the output...

Answer / rabi

#include<bits/stdc++.h>
using namespace std;
int main()
{
char s[100];
//string s;
gets(s);
int len=strlen(s);
int no;
for(int i=len-1;i>=0;i--)
{
if(i==0)
{
no=i;
while(s[no]!=' ')
{
cout<<s[no];
no++;
//if(s[i]==' ')
//break;
}
}
if(s[i]==' ')
{
no=i+1;
while(s[no]!=' ')
{
cout<<s[no];
no++;
if(s[no]=='' || s[no]==' ')
break;
}
cout<<" ";

}
}
}

Is This Answer Correct ?    1 Yes 1 No

"I LOVE MY COUNTRY" write a c program to get "COUNTRY MY LOVE I" as the output...

Answer / aniket singh

#include<iostream>
#include<string>
#include<cstring>
using namespace std;
int main(){
string str= "I love my country india";
string temp;
int st=0,i;

for(i=0;i<=str.size();i++){
if(str[i]==' '||str[i]==''){

temp = str.substr(st,(i-1)-st+1) + ' ' + temp;
st=i+1;
}
}
cout<<temp<<endl;
return 0;
}

Is This Answer Correct ?    1 Yes 1 No

"I LOVE MY COUNTRY" write a c program to get "COUNTRY MY LOVE I" as the output...

Answer / debmalya paul

package Stringmltp;
import java.util.Scanner;

public class country {

static Scanner in=new Scanner(System.in);

void rev()
{
String s=in.nextLine();
System.out.print(s.substring(10,17) +" "+ s.substring(7,9) +" "+ s.substring(2,6) +" "+ s.substring(0,1));

}
public static void main(String []args)
{
country cn=new country();
cn.rev();
}

}

Is This Answer Correct ?    2 Yes 3 No

"I LOVE MY COUNTRY" write a c program to get "COUNTRY MY LOVE I" as the output...

Answer / elle

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#define MAX 20 //define size
int main()
{
int len,j=0;
char *p, *line[MAX],*q;
p=(char *)malloc(MAX);
char *l="i love my country";

int i=0;

while(*l!='\0')
{
if(*l==' ')
//whenever a space is encountered, copy the string q into
//the array
{
*p='\0';
line[i]=(char *)malloc(MAX);
strcpy(line[i],q);
strcpy(p,"");
l++;
i++;
j=0;
}
else
{
if(j==0)
{
q=p;
}
*p=*l;
l++;
p++;
j++;
}
}

*p='\0';
line[i]=(char *)malloc(MAX);
strcpy(line[i],q);

printf("printing in reverse\n");
while(i>=0)
{
printf("%s ",line[i]);
i--;
}
}

Is This Answer Correct ?    2 Yes 4 No

"I LOVE MY COUNTRY" write a c program to get "COUNTRY MY LOVE I" as the output...

Answer / alen

using namespace std;

#include <iostream>

int main()
{
cout<<"\"COUNTRY MY LOVE I\"";
return 0;
}

Is This Answer Correct ?    9 Yes 19 No

Post New Answer

More C Interview Questions

regarding the scope of the varibles;identify the incorrect statement: a.automatic variables are automatically initialised to 0 b.static variables are are automatically initialised to 0 c.the address of a register variable is not accessiable d.static variables cannot be initialised with any expression

1 Answers   TCS,


What is the process to create increment and decrement stamen in c?

0 Answers  


How to find the usage of memory in a c program

1 Answers   Infosys,


without a terminator how can we print a message in a printf () function.

7 Answers   NIIT,


write a program to create a sparse matrix using dynamic memory allocation.

0 Answers  






Why c is faster than c++?

0 Answers  


How can I write data files which can be read on other machines with different word size, byte order, or floating point formats?

0 Answers  


How do I use void main?

0 Answers  


2. Write a function called hms_to_secs() that takes three int values&#8212;for hours, minutes, and seconds&#8212;as arguments, and returns the equivalent time in seconds.. Create a program that exercises this function by repeatedly obtaining a time value in hours, minutes, and seconds from the user (format 12:59:59), calling the function, and displaying the value of seconds it returns.

5 Answers   TCS,


Is exit(status) truly equivalent to returning the same status from main?

0 Answers  


/*what is the output for the code*/ void main() { int r; r=printf("naveen"); r=printf(); printf("%d",r); getch(); }

1 Answers   CDAC,


What is the time and space complexities of merge sort and when is it preferred over quick sort?

0 Answers   Amazon,


Categories